pf-dependencies.ps1
function Get-ScriptCommandDependencies { # based on https://mikefrobbins.com/2019/02/21/powershell-tokenizer-more-accurate-than-ast-in-certain-scenarios/ # see also https://mikefrobbins.com/2019/05/17/using-the-ast-to-find-module-dependencies-in-powershell-functions-and-scripts/ param ( [Parameter(ValueFromPipeline=$true)] $file, [Switch]$FailOnMissingCommand, [Switch]$IgnoreSameModule ) begin { $getCommandErrorAction = ($FailOnMissingCommand) ? 'Stop' : 'SilentlyContinue' } process { $file = $file | Get-Path $Token = $null $ignoredOutput = [System.Management.Automation.Language.Parser]::ParseFile($File, [ref]$Token, [ref]$null) Write-Verbose $ignoredOutput $commandTokenList = $Token | Where-Object {$_.TokenFlags -eq 'CommandName'} $commandNameList = $commandTokenList.Value.ToLowerInvariant() | Select-Object -Unique | Sort-Object $commandList = $commandNameList | ForEach-Object { Get-Command -name $_ -ErrorAction $getCommandErrorAction } $commandList = $commandList | Where-Object { $_.Module.Path } if ($IgnoreSameModule) { $commandList = $commandList | Where-Object { $cmdPath = $_.Module.Path $moduleFolder = split-path -Path $cmdPath -Parent -not (Test-Path_Parent -path $file -parent $moduleFolder) } } $commandList } } function Get-ScriptCommandDependencies:::Examples{ $result = Get-ChildItem -Path .\pf-AzPipelines -filter *.ps1 -Recurse | Get-ScriptCommandDependencies -IgnoreSameModule $result } function Get-ModuleDependencies { param ( [Parameter(ValueFromPipeline=$true)] $file, [Switch]$FailOnMissingCommand ) process { $commandList = $file | Get-ScriptCommandDependencies -IgnoreSameModule ` -FailOnMissingCommand:($FailOnMissingCommand.IsPresent) $commandList.Module } } function Get-ModuleDependencies:::Example { $result = Get-ChildItem -Path .\pf-AzPipelines -filter *.ps1 -Recurse | Get-ModuleDependencies $result } |