Not all PowerShell cmdlets ship with PowerShell. Many are part of 3rd party modules which in turn ship when you install certain software, or use certain Windows versions.
In the previous part we worked on a script that dumps all the external commands used by a script. We consolidated the findings with this function:
function Get-ExternalCommand { param ( [Parameter(Mandatory)][string] $Path ) function Get-ContainedCommand { param ( [Parameter(Mandatory)][string] $Path, [string][ValidateSet('FunctionDefinition','Command')] $ItemType ) $Token = $Err = $null $ast = [Management.Automation.Language.Parser]::ParseFile($Path, [ref] $Token, [ref] $Err) $ast.FindAll({ $args[0].GetType().Name -eq "${ItemType}Ast" }, $true) } $functionNames = Get-ContainedCommand $Path -ItemType FunctionDefinition | Select-Object -ExpandProperty Name $commands = Get-ContainedCommand $Path -ItemType Command $commands | Where-Object { $commandName = $_.CommandElements[0].Extent.Text $commandName -notin $functionNames } | ForEach-Object { $_.GetCommandName() } | Sort-Object -Unique }
You can submit any PowerShell script path to this function and receive all external commands used by that script (just make sure you submit a valid path to a script file in $path):
PS C:\> Get-ExternalCommand -Path $Path ConvertFrom-StringData Get-Acl Get-ItemProperty Get-Service Get-WmiObject New-Object out-default Test-Path Where-Object write-host
psconf.eu – PowerShell Conference EU 2019 – June 4-7, Hannover Germany – visit www.psconf.eu There aren’t too many trainings around for experienced PowerShell scripters where you really still learn something new. But there’s one place you don’t want to miss: PowerShell Conference EU – with 40 renown international speakers including PowerShell team members and MVPs, plus 350 professional and creative PowerShell scripters. Registration is open at www.psconf.eu, and the full 3-track 4-days agenda becomes available soon. Once a year it’s just a smart move to come together, update know-how, learn about security and mitigations, and bring home fresh ideas and authoritative guidance. We’d sure love to see and hear from you!