views:

368

answers:

5

Hi,

Is their anyway to test the correctness of a powershell script without executing it. I know you could do something similar in unix, but can't find the equiv for powershell.

Essentially what I have is a script repository, where each script is tied to rule. If a rule fires, the script executes, but I need to be sure the script is valid, before its put in the repository.

Thanks.

Hi, Well, Unfortunately the -whatif command is not sufficient. Its the syntax of scripts I want to check from a C# runtime engine, which fires a script if a engine rule fires. Before the script fires, I write into it, IList values from the rule, and then I fire it. But I need to ensure it is syntactically correct, otherwise I'm going to have to rely on the rule writer to ensure they are correct, which I can't do, as many people will wrote rules, only one duff script will bring the engine down. I can't do that as the engine is an enterpise class product designed for six sigma uptime.

I think the way to do it is to use this, which is a v2 lib.

http://msdn.microsoft.com/en-us/library/system.management.automation.parseexception(VS.85).aspx

I think there is way of creating a ScriptBlock and putting a try catch around it to catch the ParseException. Can somebody tell me how to do it. This is becoming a real challenge.

Thanks.

Hey, Well I spoke to a guy in MS (he's sharepoint pm) and he spoke to one of the powershell guys, and he informed me that there is no real way in powershell v1 to check the syntax of the script without executing. So I guess the -whatif flag is the closest, but it not a static checker unfortumately. Good news is, in v2 there is a way to tokenize the input stream, via the Automation.PsParser class, Tokenize member. This will tokenize a script in accordance with the PS grammes, so if it craps out, its a crap script. Question is v2 doesn't have an release date as yet.

Thanks for help. Bob.

+1  A: 

So, are you asking for functional correctness or syntactic correctness?

Functionally you'd need to create a test case and environment to test in.

Syntactically you're looking for a static code checker for powershell.

chills42
To be precise: Testing can never assure correctness, only presence of errors. But formal proofs of program behaviour aren't exactly trivial to do :)
Joey
+3  A: 

The thing you want is prolly whatif / confirm. Its direct port from Unix world.

majkinetor
Hi Majkinetor,That its, and that was really quick. I've been looking for that on and off for about a year. Thanks. Bob.
scope_creep
Hi Majkinetor,As much as I was hoping that was it, it seems that this parameter is only applicable to certain commands, and not scripts as such. Since i'm calling it from c#, well its not fitting.
scope_creep
As I note in my answer, 'whatif' and 'confirm' only work with cmdlets, not for the whole script.
James Pogran
Not even that. If you check cmdlet SDK you can see that it is something cmdlet may choose to implement or not. You can design scripts to have this feature too.
majkinetor
@null: I guess you are talking about sandboxing. If you want this on powershell script level... well... I guess there will never be such a feature.If sandbox is what you need, you could be able to utilise/automate some of the external tools for that and parse their output. Google for sandboxie
majkinetor
A: 

Many PowerShell commands offer a -whatif parameter.

aphoria
Powershell cmdlets (note there is a difference in terminology) do, however the OP is asking about scripts
James Pogran
Well, since most scripts consist of PowerShell commmands (cmdlets)...There is nothing preventing a script from taking a -whatif parameter, you just have to supply the logic yourself.
aphoria
A: 

I think you need to quantify what 'valid' means to you. That is an ambiguous term and can mean alot of things.

If you mean something like perl's syntax check, there is no exact equivalent for a PowerShell script, with PowerShell V1. I'm not sure there is anything in V2 for that either.

The description you give makes it sound like you want something more than just a syntax/'will it run' type check. Like @JoHannes Rossell says, that is not trivial and is highly specific to what you are doing.

The previous mentioned 'whatif/confirm' methods in the other answers only work for PowerShell cmdlets, not scripts. They show what that cmdlet would do, not a syntax check.

james

James Pogran
+2  A: 

Check this post:

http://keithhill.spaces.live.com/Blog/cns!5A8D2641E0963A97!6036.entry

Shay Levy