可以使用匹配偏好变量和类似以下语法的方式,通过传递大多数常见参数。例如,对于详细信息的特定情况,语法为
-Verbose:$VerbosePreference
。但也有几个例外情况:
- Debug:自动传递
$DebugPreference
的值,但指定
-Debug
开关会强制将
$DebugPreference
设置为
Inquire
。
- WhatIf:自动传递。
我已修改原始代码示例,如下所示:
[CmdletBinding(SupportsShouldProcess=$true)]
param(
[Switch]$FullPassThru
)
New-Module -Name TempModule -ScriptBlock {
function Test-ModuleVerbose
{
[CmdletBinding(SupportsShouldProcess=$true)]
param ()
Write-Host "1: Module: verbose parameter is bound : $($PSCmdlet.MyInvocation.BoundParameters['Verbose'])"
Write-Host "2: Module: verbose preference : $VerbosePreference"
Write-Verbose "Verbose"
Set-Item -Path Env:\DEMONSTRATE_PASS_THRU `
-Value 'You can safely delete this variable' `
-Verbose:$VerbosePreference
}
function Test-ModulePreferencePassThru
{
[CmdletBinding(SupportsShouldProcess=$true)]
param()
Write-Debug "DebugPreference: $DebugPreference"
Write-Warning "WarningPreference: $WarningPreference"
Write-Error "ErrorActionPreference: $ErrorActionPreference"
Set-Item -Path Env:\DEMONSTRATE_PASS_THRU `
-Value 'You can safely delete this variable' `
-Verbose:$VerbosePreference `
-WarningAction:$WarningPreference `
-ErrorAction:$ErrorActionPreference
}
} | Out-Null
function Test-Verbose
{
[CmdletBinding(SupportsShouldProcess=$true)]
param()
Write-Host ("Verbose preference: $VerbosePreference")
Test-ModuleVerbose -Verbose:$VerbosePreference
}
function Test-PreferencePassThru
{
[CmdletBinding(SupportsShouldProcess=$true)]
param()
Test-ModulePreferencePassThru -Verbose:$VerbosePreference
}
try
{
if ($FullPassThru -eq $false)
{
Test-Verbose
}
else
{
Test-ModulePreferencePassThru -Verbose:$VerbosePreference `
-WarningAction:$WarningPreference `
-ErrorAction:$ErrorActionPreference | Out-Null
}
}
finally
{
Remove-Item -Path Env:\DEMONSTRATE_PASS_THRU -Force | Out-Null
}
将上述内容保存为test.ps1。从控制台调用时:
PS C:\temp> .\test.ps1
Verbose preference: SilentlyContinue
1: Module: verbose parameter is bound : False
2: Module: verbose preference : SilentlyContinue
PS C:\temp> .\test.ps1 -Verbose
VERBOSE: Exporting function 'Test-ModuleVerbose'.
VERBOSE: Exporting function 'Test-ModulePreferencePassThru'.
VERBOSE: Importing function 'Test-ModulePreferencePassThru'.
VERBOSE: Importing function 'Test-ModuleVerbose'.
Verbose preference: Continue
1: Module: verbose parameter is bound : True
2: Module: verbose preference : Continue
VERBOSE: Verbose
VERBOSE: Performing the operation "Set Item" on target "Item: DEMONSTRATE_PASS_THRU Value: You can safely delete this variable".
另外,对于$DebugPreference
、$WarningPreference
和$ErrorActionPreference
,传递也是有效的:
PS C:\temp> $VerbosePreference = 'Continue'
PS C:\temp> $DebugPreference = 'Continue'
PS C:\temp> $WarningPreference = 'Continue'
PS C:\temp> $ErrorActionPreference = 'Continue'
PS C:\temp> .\test.ps1 -FullPassThru
VERBOSE: Exporting function 'Test-ModuleVerbose'.
VERBOSE: Exporting function 'Test-ModulePreferencePassThru'.
VERBOSE: Importing function 'Test-ModulePreferencePassThru'.
VERBOSE: Importing function 'Test-ModuleVerbose'.
DEBUG: DebugPreference: Continue
WARNING: WarningPreference: Continue
Test-ModulePreferencePassThru : ErrorActionPreference: Continue
At C:\OAASMain\Online\ContainerService\Tools\docker\test.ps1:72 char:9
+ Test-ModulePreferencePassThru -Verbose:$VerbosePreference `
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Test-ModulePreferencePassThru
VERBOSE: Performing the operation "Set Item" on target "Item: DEMONSTRATE_PASS_THRU Value: You can safely delete this variable".
-WhatIf
参数会自动传递:
PS C:\temp> .\test.ps1 -FullPassThru -WhatIf
What if: Performing the operation "Remove Item" on target "Item: DEMONSTRATE_PASS_THRU".
这也处理-WarningAction
和-ErrorAction
:
PS C:\temp> .\test.ps1 -FullPassThru -WarningAction Ignore -ErrorAction Stop
Test-ModulePreferencePassThru : ErrorActionPreference : Stop
At C:\OAASMain\Online\ContainerService\Tools\docker\test.ps1:72 char:9
+ Test-ModulePreferencePassThru -Verbose:$VerbosePreference `
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Test-ModulePreferencePassThru