PowerShell Problem Solver: Find Script Commands

Posted on November 12, 2014 by Jeff Hicks in PowerShell with 0 Comments

Recently I was involved in an exchange on Twitter. The discussion originally was about find a way to identify what cmdlets are available for a given version of PowerShell or operating system. But after a little back and forth, I discovered the real issue: how to identify what cmdlets or modules that are necessary to run a script. The issue for my tweet-pal centered on Desired State Configuration (DSC). He was having issues using some DSC resources and it was most likely due to missing cmdlets on the target node. A DSC resource is packaged as a module, which means it should be possible to look through the psm1 file and identify what cmdlets and modules it requires. Here are a few ways to approach this problem, and by approach I mean letting PowerShell do the work for us.

Method 1: Using regular expressions

The first approach is a simple brute-force technique using regular expressions. Most DSC resources will be getting or setting something. Assuming the module author is using full cmdlet names, I can define a regular expression pattern to search for text that looks like a cmdlet name.

The pattern looks for a word that starts with Get, New or any of the rest, followed by a dash and then any other word. To search, we need to identify the path to the DSC module file.

The path property will be to the module file. I’ll get the module contents.

And then I’ll use the regex object to find all matching values.

Because there will most likely be duplicates, I want a unique list. I have that found sorting works best for this. But now I can see what cmdlets are used in this resource.

Using the Get-Unique cmdlet in Windows PowerShell. (Image Credit: Jeff Hicks)

Using the Get-Unique cmdlet in Windows PowerShell. (Image Credit: Jeff Hicks)

I could then test for that command on my target computer.

Or perhaps test for all of them.

I filtered out the commands that are part of every DSC resource. Now I can easily see if there are any potential problems before I deploy my configuration. If you like this approach, then you can use a function that simplifies the process.

All you need to do is enter the name of a DSC resource.

One drawback to this approach is that you might also identify internally defined functions that match the regular expression pattern. There’s nothing wrong with functions as they are most likely calling regular PowerShell cmdlets. But another technique would be to parse the script file using PowerShell’s AST (abstract syntax tree).

Sponsored

Method 2: Using PowerShell AST

Without getting too deep into the technical .NET details, you can use the AST to analyze a block of PowerShell code. The AST will parse the commands and create a series of tokens. These tokens represent parts of a PowerShell command. Using the AST is not something I would expect of a typical IT pro, so I ‘ll give you a hand and offer function called Test-ScriptFile:

The script has comment-based help and hopefully plenty of inline comments, so I won’t repeat those here. To use, all you need to do is feed it the path to a script file. This can be any .ps1, .psm1 or even a .txt file. Fortunately, the DSC resource object’s Path property is conveniently named and I wrote Test-ScriptFile to take advantage of it.

The function not only identifies commands, it will resolve aliases and get command information, if it can be found locally. I recommend having a Windows 8.1 desktop with RSAT installed to get the most coverage. But as you can see from the screenshot, sometimes even that might not be enough.

Testing the script file in Windows PowerShell. (Image Credit: Jeff Hicks)

Testing the script file in Windows PowerShell. (Image Credit: Jeff Hicks)

Sponsored

Some of these commands may be internal functions, but at least I have some command names to research before using this resource in a configuration. Let me also point out that you can use this function for any script file, not just a DSC resource. This is a handy way of identifying what commands are in that script you just downloaded.

Using the function on a different script file in PowerShell. (Image Credit: Jeff Hicks)

Using the function on a different script file in PowerShell. (Image Credit: Jeff Hicks)

I also added parameter to only display unknown items.

Displaying unknown items in Windows PowerShell. (Image Credit: Jeff Hicks)

Displaying unknown items in Windows PowerShell. (Image Credit: Jeff Hicks)

Now I at least know what to search for when I open the file in my script editor.

These are a few ways you can identify what commands will be run in a PowerShell script. But the best technique is to review the script file, DSC Resources included, in a scripting editor before you begin using it.

Sponsored

Tagged with , , ,