Created
March 16, 2021 15:06
-
-
Save SteveL-MSFT/4fbba123cab8e87df2836ae1ffe809d9 to your computer and use it in GitHub Desktop.
Find modules/commands used by script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param($file) | |
$tokens = $null | |
$err = $null | |
$file = Resolve-Path $file | |
$ast = [System.Management.Automation.Language.Parser]::ParseFile($file, [ref]$tokens, [ref]$err) | |
$commands = $ast.FindAll({$true},$true) | ? { $_ -is [System.Management.Automation.Language.CommandAst] } | % { $_.CommandElements[0].Value } | Sort-Object -Unique | |
$sources = [System.Collections.Generic.List[string]]::new() | |
$commands | % { | |
$c = Get-Command $_ -ErrorAction Ignore | |
if ($null -eq $c) { | |
Write-Error "$_ not found" | |
} | |
else { | |
$null = $sources.Add($c.Source) | |
} | |
} | |
$sources | Sort-Object -Unique |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment