Skip to content

Commit

Permalink
Proxy cmdlet support for safe cmdlet lookup
Browse files Browse the repository at this point in the history
The pester module will fail to load when someone has a module already loaded that defines a cmdlet with the same name as one of the safe commands. An example of this is FormatPx, which defines Format-Table and Out-String proxy cmdlets (not functions, cmdlets). I have FormatPx loaded in my profile, and when I try to load Pester or invoke a Pester command using 5.1.14393, it fails. This change fixes the problem by adding the -All parameter to all Get-Command calls used to build up the list of safe cmdlets.
  • Loading branch information
KirkMunro authored Aug 2, 2016
1 parent 0083bb5 commit bfd73e6
Showing 1 changed file with 57 additions and 49 deletions.
106 changes: 57 additions & 49 deletions Pester.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -17,54 +17,62 @@ else
# Get-Command does. Since this is at import time, before any mocks have been defined, that's probably acceptable.
# If someone monkeys with Get-Command before they import Pester, they may break something.

# The -All parameter is required when calling Get-Command to ensure that PowerShell can find the command it is
# looking for. Otherwise, if you have modules loaded that define proxy cmdlets or that have cmdlets with the same
# name as the safe cmdlets, Get-Command will return null.
$safeCommandLookupParameters = @{
CommandType = [System.Management.Automation.CommandTypes]::Cmdlet
All = $true
ErrorAction = [System.Management.Automation.ActionPreference]::Stop
}
$script:SafeCommands = @{
'Add-Member' = Get-Command -Name Add-Member -Module Microsoft.PowerShell.Utility -CommandType Cmdlet -ErrorAction Stop
'Add-Type' = Get-Command -Name Add-Type -Module Microsoft.PowerShell.Utility -CommandType Cmdlet -ErrorAction Stop
'Compare-Object' = Get-Command -Name Compare-Object -Module Microsoft.PowerShell.Utility -CommandType Cmdlet -ErrorAction Stop
'Export-ModuleMember' = Get-Command -Name Export-ModuleMember -Module Microsoft.PowerShell.Core -CommandType Cmdlet -ErrorAction Stop
'ForEach-Object' = Get-Command -Name ForEach-Object -Module Microsoft.PowerShell.Core -CommandType Cmdlet -ErrorAction Stop
'Format-Table' = Get-Command -Name Format-Table -Module Microsoft.PowerShell.Utility -CommandType Cmdlet -ErrorAction Stop
'Get-ChildItem' = Get-Command -Name Get-ChildItem -Module Microsoft.PowerShell.Management -CommandType Cmdlet -ErrorAction Stop
'Get-Command' = Get-Command -Name Get-Command -Module Microsoft.PowerShell.Core -CommandType Cmdlet -ErrorAction Stop
'Get-Content' = Get-Command -Name Get-Content -Module Microsoft.PowerShell.Management -CommandType Cmdlet -ErrorAction Stop
'Get-Date' = Get-Command -Name Get-Date -Module Microsoft.PowerShell.Utility -CommandType Cmdlet -ErrorAction Stop
'Get-Item' = Get-Command -Name Get-Item -Module Microsoft.PowerShell.Management -CommandType Cmdlet -ErrorAction Stop
'Get-Location' = Get-Command -Name Get-Location -Module Microsoft.PowerShell.Management -CommandType Cmdlet -ErrorAction Stop
'Get-Member' = Get-Command -Name Get-Member -Module Microsoft.PowerShell.Utility -CommandType Cmdlet -ErrorAction Stop
'Get-Module' = Get-Command -Name Get-Module -Module Microsoft.PowerShell.Core -CommandType Cmdlet -ErrorAction Stop
'Get-PSDrive' = Get-Command -Name Get-PSDrive -Module Microsoft.PowerShell.Management -CommandType Cmdlet -ErrorAction Stop
'Get-Variable' = Get-Command -Name Get-Variable -Module Microsoft.PowerShell.Utility -CommandType Cmdlet -ErrorAction Stop
'Group-Object' = Get-Command -Name Group-Object -Module Microsoft.PowerShell.Utility -CommandType Cmdlet -ErrorAction Stop
'Join-Path' = Get-Command -Name Join-Path -Module Microsoft.PowerShell.Management -CommandType Cmdlet -ErrorAction Stop
'Measure-Object' = Get-Command -Name Measure-Object -Module Microsoft.PowerShell.Utility -CommandType Cmdlet -ErrorAction Stop
'New-Item' = Get-Command -Name New-Item -Module Microsoft.PowerShell.Management -CommandType Cmdlet -ErrorAction Stop
'New-Module' = Get-Command -Name New-Module -Module Microsoft.PowerShell.Core -CommandType Cmdlet -ErrorAction Stop
'New-Object' = Get-Command -Name New-Object -Module Microsoft.PowerShell.Utility -CommandType Cmdlet -ErrorAction Stop
'New-PSDrive' = Get-Command -Name New-PSDrive -Module Microsoft.PowerShell.Management -CommandType Cmdlet -ErrorAction Stop
'New-Variable' = Get-Command -Name New-Variable -Module Microsoft.PowerShell.Utility -CommandType Cmdlet -ErrorAction Stop
'Out-Null' = Get-Command -Name Out-Null -Module $outNullModule -CommandType Cmdlet -ErrorAction Stop
'Out-String' = Get-Command -Name Out-String -Module Microsoft.PowerShell.Utility -CommandType Cmdlet -ErrorAction Stop
'Pop-Location' = Get-Command -Name Pop-Location -Module Microsoft.PowerShell.Management -CommandType Cmdlet -ErrorAction Stop
'Push-Location' = Get-Command -Name Push-Location -Module Microsoft.PowerShell.Management -CommandType Cmdlet -ErrorAction Stop
'Remove-Item' = Get-Command -Name Remove-Item -Module Microsoft.PowerShell.Management -CommandType Cmdlet -ErrorAction Stop
'Remove-PSBreakpoint' = Get-Command -Name Remove-PSBreakpoint -Module Microsoft.PowerShell.Utility -CommandType Cmdlet -ErrorAction Stop
'Remove-PSDrive' = Get-Command -Name Remove-PSDrive -Module Microsoft.PowerShell.Management -CommandType Cmdlet -ErrorAction Stop
'Remove-Variable' = Get-Command -Name Remove-Variable -Module Microsoft.PowerShell.Utility -CommandType Cmdlet -ErrorAction Stop
'Resolve-Path' = Get-Command -Name Resolve-Path -Module Microsoft.PowerShell.Management -CommandType Cmdlet -ErrorAction Stop
'Select-Object' = Get-Command -Name Select-Object -Module Microsoft.PowerShell.Utility -CommandType Cmdlet -ErrorAction Stop
'Set-Content' = Get-Command -Name Set-Content -Module Microsoft.PowerShell.Management -CommandType Cmdlet -ErrorAction Stop
'Set-PSBreakpoint' = Get-Command -Name Set-PSBreakpoint -Module Microsoft.PowerShell.Utility -CommandType Cmdlet -ErrorAction Stop
'Set-StrictMode' = Get-Command -Name Set-StrictMode -Module Microsoft.PowerShell.Core -CommandType Cmdlet -ErrorAction Stop
'Set-Variable' = Get-Command -Name Set-Variable -Module Microsoft.PowerShell.Utility -CommandType Cmdlet -ErrorAction Stop
'Sort-Object' = Get-Command -Name Sort-Object -Module Microsoft.PowerShell.Utility -CommandType Cmdlet -ErrorAction Stop
'Split-Path' = Get-Command -Name Split-Path -Module Microsoft.PowerShell.Management -CommandType Cmdlet -ErrorAction Stop
'Start-Sleep' = Get-Command -Name Start-Sleep -Module Microsoft.PowerShell.Utility -CommandType Cmdlet -ErrorAction Stop
'Test-Path' = Get-Command -Name Test-Path -Module Microsoft.PowerShell.Management -CommandType Cmdlet -ErrorAction Stop
'Where-Object' = Get-Command -Name Where-Object -Module Microsoft.PowerShell.Core -CommandType Cmdlet -ErrorAction Stop
'Write-Error' = Get-Command -Name Write-Error -Module Microsoft.PowerShell.Utility -CommandType Cmdlet -ErrorAction Stop
'Write-Progress' = Get-Command -Name Write-Progress -Module Microsoft.PowerShell.Utility -CommandType Cmdlet -ErrorAction Stop
'Write-Verbose' = Get-Command -Name Write-Verbose -Module Microsoft.PowerShell.Utility -CommandType Cmdlet -ErrorAction Stop
'Write-Warning' = Get-Command -Name Write-Warning -Module Microsoft.PowerShell.Utility -CommandType Cmdlet -ErrorAction Stop
'Add-Member' = Get-Command -Name Add-Member -Module Microsoft.PowerShell.Utility @safeCommandLookupParameters
'Add-Type' = Get-Command -Name Add-Type -Module Microsoft.PowerShell.Utility @safeCommandLookupParameters
'Compare-Object' = Get-Command -Name Compare-Object -Module Microsoft.PowerShell.Utility @safeCommandLookupParameters
'Export-ModuleMember' = Get-Command -Name Export-ModuleMember -Module Microsoft.PowerShell.Core @safeCommandLookupParameters
'ForEach-Object' = Get-Command -Name ForEach-Object -Module Microsoft.PowerShell.Core @safeCommandLookupParameters
'Format-Table' = Get-Command -Name Format-Table -Module Microsoft.PowerShell.Utility @safeCommandLookupParameters
'Get-ChildItem' = Get-Command -Name Get-ChildItem -Module Microsoft.PowerShell.Management @safeCommandLookupParameters
'Get-Command' = Get-Command -Name Get-Command -Module Microsoft.PowerShell.Core @safeCommandLookupParameters
'Get-Content' = Get-Command -Name Get-Content -Module Microsoft.PowerShell.Management @safeCommandLookupParameters
'Get-Date' = Get-Command -Name Get-Date -Module Microsoft.PowerShell.Utility @safeCommandLookupParameters
'Get-Item' = Get-Command -Name Get-Item -Module Microsoft.PowerShell.Management @safeCommandLookupParameters
'Get-Location' = Get-Command -Name Get-Location -Module Microsoft.PowerShell.Management @safeCommandLookupParameters
'Get-Member' = Get-Command -Name Get-Member -Module Microsoft.PowerShell.Utility @safeCommandLookupParameters
'Get-Module' = Get-Command -Name Get-Module -Module Microsoft.PowerShell.Core @safeCommandLookupParameters
'Get-PSDrive' = Get-Command -Name Get-PSDrive -Module Microsoft.PowerShell.Management @safeCommandLookupParameters
'Get-Variable' = Get-Command -Name Get-Variable -Module Microsoft.PowerShell.Utility @safeCommandLookupParameters
'Group-Object' = Get-Command -Name Group-Object -Module Microsoft.PowerShell.Utility @safeCommandLookupParameters
'Join-Path' = Get-Command -Name Join-Path -Module Microsoft.PowerShell.Management @safeCommandLookupParameters
'Measure-Object' = Get-Command -Name Measure-Object -Module Microsoft.PowerShell.Utility @safeCommandLookupParameters
'New-Item' = Get-Command -Name New-Item -Module Microsoft.PowerShell.Management @safeCommandLookupParameters
'New-Module' = Get-Command -Name New-Module -Module Microsoft.PowerShell.Core @safeCommandLookupParameters
'New-Object' = Get-Command -Name New-Object -Module Microsoft.PowerShell.Utility @safeCommandLookupParameters
'New-PSDrive' = Get-Command -Name New-PSDrive -Module Microsoft.PowerShell.Management @safeCommandLookupParameters
'New-Variable' = Get-Command -Name New-Variable -Module Microsoft.PowerShell.Utility @safeCommandLookupParameters
'Out-Null' = Get-Command -Name Out-Null -Module $outNullModule @safeCommandLookupParameters
'Out-String' = Get-Command -Name Out-String -Module Microsoft.PowerShell.Utility @safeCommandLookupParameters
'Pop-Location' = Get-Command -Name Pop-Location -Module Microsoft.PowerShell.Management @safeCommandLookupParameters
'Push-Location' = Get-Command -Name Push-Location -Module Microsoft.PowerShell.Management @safeCommandLookupParameters
'Remove-Item' = Get-Command -Name Remove-Item -Module Microsoft.PowerShell.Management @safeCommandLookupParameters
'Remove-PSBreakpoint' = Get-Command -Name Remove-PSBreakpoint -Module Microsoft.PowerShell.Utility @safeCommandLookupParameters
'Remove-PSDrive' = Get-Command -Name Remove-PSDrive -Module Microsoft.PowerShell.Management @safeCommandLookupParameters
'Remove-Variable' = Get-Command -Name Remove-Variable -Module Microsoft.PowerShell.Utility @safeCommandLookupParameters
'Resolve-Path' = Get-Command -Name Resolve-Path -Module Microsoft.PowerShell.Management @safeCommandLookupParameters
'Select-Object' = Get-Command -Name Select-Object -Module Microsoft.PowerShell.Utility @safeCommandLookupParameters
'Set-Content' = Get-Command -Name Set-Content -Module Microsoft.PowerShell.Management @safeCommandLookupParameters
'Set-PSBreakpoint' = Get-Command -Name Set-PSBreakpoint -Module Microsoft.PowerShell.Utility @safeCommandLookupParameters
'Set-StrictMode' = Get-Command -Name Set-StrictMode -Module Microsoft.PowerShell.Core @safeCommandLookupParameters
'Set-Variable' = Get-Command -Name Set-Variable -Module Microsoft.PowerShell.Utility @safeCommandLookupParameters
'Sort-Object' = Get-Command -Name Sort-Object -Module Microsoft.PowerShell.Utility @safeCommandLookupParameters
'Split-Path' = Get-Command -Name Split-Path -Module Microsoft.PowerShell.Management @safeCommandLookupParameters
'Start-Sleep' = Get-Command -Name Start-Sleep -Module Microsoft.PowerShell.Utility @safeCommandLookupParameters
'Test-Path' = Get-Command -Name Test-Path -Module Microsoft.PowerShell.Management @safeCommandLookupParameters
'Where-Object' = Get-Command -Name Where-Object -Module Microsoft.PowerShell.Core @safeCommandLookupParameters
'Write-Error' = Get-Command -Name Write-Error -Module Microsoft.PowerShell.Utility @safeCommandLookupParameters
'Write-Progress' = Get-Command -Name Write-Progress -Module Microsoft.PowerShell.Utility @safeCommandLookupParameters
'Write-Verbose' = Get-Command -Name Write-Verbose -Module Microsoft.PowerShell.Utility @safeCommandLookupParameters
'Write-Warning' = Get-Command -Name Write-Warning -Module Microsoft.PowerShell.Utility @safeCommandLookupParameters
}

# Not all platforms have Get-WmiObject (Nano)
Expand All @@ -73,11 +81,11 @@ $script:SafeCommands = @{
# exist
if ( Get-Command -ea SilentlyContinue Get-CimInstance )
{
$script:SafeCommands['Get-CimInstance'] = Get-Command -Name Get-CimInstance -Module CimCmdlets -CommandType Cmdlet -ErrorAction Stop
$script:SafeCommands['Get-CimInstance'] = Get-Command -Name Get-CimInstance -Module CimCmdlets @safeCommandLookupParameters
}
elseif ( Get-command -ea SilentlyContinue Get-WmiObject )
{
$script:SafeCommands['Get-WmiObject'] = Get-Command -Name Get-WmiObject -Module Microsoft.PowerShell.Management -CommandType Cmdlet -ErrorAction Stop
$script:SafeCommands['Get-WmiObject'] = Get-Command -Name Get-WmiObject -Module Microsoft.PowerShell.Management @safeCommandLookupParameters
}
else
{
Expand Down

0 comments on commit bfd73e6

Please sign in to comment.