Updated PowerShell Script Profiler

Last year I wrote a sidebar for the Scripting Guy, Ed Wilson and an update to his PowerShell Best Practices book. I wrote a script using the new parser in PowerShell 3.0 to that would analyze a script and prepare a report showing what commands it would run, necessary parameters, and anything that might pose a danger. I also wrote an article with the script for the Hey Scripting Guy blog.

During my PowerShell training class last week we were talking about how to look at someone else’s code and figure out what it might do. I also demonstrated my script. During the coure of that demonstration I realized I needed to make a few revisions. Here is version 2 of Get-ASTScriptProfile.ps1.

One thing I discovered was that the AST didn’t like parsing a file without an absolute and resolved path. I was running into errors because the script was on a PSDrive like Scripts: that resolved to C:\Scripts. The solution was to resolve it, in the event of using a path like .\file.ps1 and then convert the path.

I also discovered that if the script contains functions with a name like Main or Control, the parser, at the least the way I am using it, will detect it as an application like main.cpl. There’s a reason you should give your function a meaningful and standard name and try to avoid using a name that *might* be misinterepted. A function called Notepad probably isn’t a good idea. I modified the help to indicate this potential misinterpretation. I’m not sure there’s much else I can do.

The few other changes I made were for the sake of flexibility and clarity. I now include the PSVersion information in the report. Parameter information is now displayed as a list to avoid losing any data. I added a validation parameter for the script file so that you can only analzye a .ps1, .psm1 or .txt file.

Finally, I added a parameter so that you can specify the folder for the output file. The default is Documents\WindowsPowerShell. You don’t specify the filename, just the path like C:\Scripts or $env:temp.

This will result in a help topic report like this:

script-profile-report
So download the new version and try it out on your scripts. Find some files online and try it out on them. Let me know what you think.