Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proxy cmdlet support for safe cmdlet lookup #588

Merged
merged 1 commit into from
Aug 2, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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