Skip to content

Instantly share code, notes, and snippets.

@SteveL-MSFT
Created March 16, 2021 15:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SteveL-MSFT/4fbba123cab8e87df2836ae1ffe809d9 to your computer and use it in GitHub Desktop.
Save SteveL-MSFT/4fbba123cab8e87df2836ae1ffe809d9 to your computer and use it in GitHub Desktop.
Find modules/commands used by script
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