Check version compatibility for Powershell scripts?

Status
Not open for further replies.
I have a lot of Powershell scripts that I run in different environments.
Now, I have lately started to use functionality that is available in v3 and up. However, when running these scripts where only v2 is available will make them break bad.

Of course, I could go through all scripts and add a check of get-host to see what version is running and then ending the script if it is not compatible. However, I'd rather like to have a way to automatically parse the script and then get a message on which line(s) that is not supported in v2 so that I more easily can go through the scripts and fix this. Testrunning them all is far to timeconsuming even though it will give me the same result (more or less), breaking with errors on the offending lines.

So, is there a way to check scripts for functionality that is not supported in >v2 other than just running the scripts and see if they break?
 

Cookie.Monster

Ars Scholae Palatinae
666
This responsibility is on the author and is one of the purposes of the #requires statement.

That being said, you might be able to use the AST, tokenize, or just parse raw text to look for v3 specific Cmdlets/parameters and language features.

This is why I generally try to aim at PowerShell 2, or explicitly note the compatibility issues in the comment based help system...

Lastly, perhaps you could take this opportunity to standardize on a newer PowerShell (v4)?

Good luck!
 
Tokenizer et al seems to be a plausible way to go. Thanks!

As for running pure v2, I'd rather not since there are stuff in >v3 that is both faster and nicer. Standardizing on v4 is also out of the question since I don't always have control over the systems where I run the scripts.

But tokenizer/AST looks like it could help me out. I'll test some ideas I have there and bring it back here!
 
Status
Not open for further replies.