Categories

Test-Script

I’m going to do a few posts on the PowerShell Community Extensions 2.0 beta i mentioned earlier.  It downloads and installs without a problem.

One immediate issue on loading is that it replaces my help function – big no no.  Bad PSCX! I don’t like having my configuration arbitrarily altered.  That I can deal with and prevent. 

In this post I’ll look at test-script. This is a neat cmdlet that accepts the path to a script as input, reads the script and returns any errors.  I found a copy of a script with some errors, no not one of mine, it came from an old Scripting Games competition.

 

for ($i in Get-ChildItem C:\Scripts)
(
    if {($i.CreationTime -gt ($(Get-Date).AddMonths(-10))) and ($i.Extension = "txt")}
    {
         Copy-Item $i.FullName C:\old
         $i.Name
         $x = $x + 1
    }
)

""
"Total Files: " + $y

 

Then put the script through the cmdlet

PS> Test-Script -Path c:\test\DebugMe.ps1
WARNING: Parse error on line:1 char:9 - Unexpected token 'in' in expression or statement.
WARNING: Parse error on line:1 char:12 - Unexpected token 'Get-ChildItem' in expression or statement.
WARNING: Parse error on line:1 char:26 - Unexpected token 'C:\Scripts' in expression or statement.
WARNING: Parse error on line:2 char:1 - Missing statement body in for loop.
WARNING: Parse error on line:3 char:60 - Unexpected token 'and' in expression or statement.
WARNING: Parse error on line:3 char:64 - Unexpected token '(' in expression or statement.
WARNING: Parse error on line:3 char:65 - Unexpected token 'i' in expression or statement.
WARNING: Parse error on line:3 char:67 - Unexpected token '.Extension' in expression or statement.
WARNING: Parse error on line:3 char:78 - Unexpected token '=' in expression or statement.
WARNING: Parse error on line:3 char:80 - Unexpected token 'txt' in expression or statement.
WARNING: Parse error on line:3 char:85 - Missing closing '}' in statement block.
WARNING: Parse error on line:3 char:86 - Missing closing ')' in expression.
WARNING: Parse error on line:3 char:86 - Unexpected token '}' in expression or statement.
False
PS>

 

We can also display lines around the error as well.

This is a useful addition to the PowerShell toolkit

Technorati Tags: ,,

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>