I’m pretty excited about this new module. Originally I threw something very crappy together just to get a rough idea of which commands I was calling. But then I started polishing it up and polishing it up and I arrived at something I just had to post.

Dir . -i *.ps1,*.psm1 -r | Get-Dependency -Unresolved | Out-GridView

The above line of code will scan each ps1 and psm1 file in the current directory as well as subdirectories and send the information about any unresolved dependencies to the Out-GridView cmdlet for display. Another nice feature is that if you run the command from within PowerShell ISE and you don’t give it a path, it will check whichever file is currently open in the editor.

NAME
    Get-Dependency

SYNOPSIS
    Calculates the dependencies of a script file, block of PowerShell code, or
    an open PowerShell ISE document.

SYNTAX
    Get-Dependency [-Unresolved] [-Force] [-Verbose] [-Debug] [-ErrorAction []]
                   [-WarningAction []] [-ErrorVariable []]
                   [-WarningVariable []] [-OutVariable []] [-OutBuffer []]
                   []

    Get-Dependency [-Path] [] [-Unresolved] [-Force] [-Verbose] [-Debug]
                   [-ErrorAction []] [-WarningAction []]
                   [-ErrorVariable []] [-WarningVariable []] [-OutVariable []]
                   [-OutBuffer []] []

    Get-Dependency -LiteralPath [] [-Unresolved] [-Force] [-Verbose] [-Debug]
                   [-ErrorAction []] [-WarningAction
                   []] [-ErrorVariable []] [-WarningVariable []]
                   [-OutVariable []] [-OutBuffer []] []

DETAILED DESCRIPTION
    Before deploying a script or module, it is important to ensure that any
    external dependencies are resolved otherwise code that runs fine on your
    machine will bomb on someone else's. This function will scan a single
    level of dependencies from the specified script and by default returns
    any dependency that is 1) not a part of the built-in PowerShell command
    and variable configuration and 2) not defined in the script being analyzed.
    You can override this behavior and include these dependencies with the
    Force parameter.

PARAMETERS
    -Path
        Specifies the path to an item. Wildcards are permitted.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?       false
        Accept wildcard characters?  

    -LiteralPath
        Specifies the path to an item. Unlike Path, the value of LiteralPath is
        used exactly as it is typed. No characters are interpreted as wildcards.
        If the path includes escape characters, enclose it in single quotation marks.
        Single quotation marks tell Windows PowerShell not to interpret any
        characters as escape sequences.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?       false
        Accept wildcard characters?  

    -Unresolved
        When specified, only unresolved dependencies are returned.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?       false
        Accept wildcard characters?  

    -Force
        When specified, all dependencies are included, even if they are known to
        be defined locally or in the default PowerShell configuration.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?       false
        Accept wildcard characters?  

        This cmdlet supports the common parameters: -Verbose, -Debug,
        -ErrorAction, -ErrorVariable, -WarningAction, -WarningVariable,
        -OutBuffer and -OutVariable. For more information, type,
        "get-help about_commonparameters".

RETURN TYPE
    An array of PSObjects containing information about the external dependencies.

    -------------------------- EXAMPLE 1 --------------------------

    Get-Dependency | Out-GridView

RELATED LINKS
    Get-PSToken

imageI will post back with some more information later, but the basic idea is that this uses the new PSParser class to parse a PowerShell script into tokens. It then analyzes the tokens to figure out some basic facts about the script:

  • Which global variables are being referenced
  • Which modules does the script import that are not currently imported
  • Which commands are being referenced by the script?
    • Which commands are functions defined in the current script?
    • Which command are aliases and which commands do they resolve to?
    • Which commands are built-in functions/cmdlets/aliases/etc?

Once all of the above has been determined, it’s presented in a format that is easy to read.

I added mine to the custom menu of PowerShell ISE. :)

Dependency Module