Mic*_*ens 25
显式提供-WhatIf参数:
rm foo.txt -WhatIf
Run Code Online (Sandbox Code Playgroud)
结果:
What if: Performing operation "Remove File" on Target "C:\temp\foo.txt".
Run Code Online (Sandbox Code Playgroud)
SupportsShouldProcess属性自动传播-WhatIf到支持的cmdlet:
function do-stuff
{
[CmdletBinding(SupportsShouldProcess=$True)]
param([string]$file)
Remove-Item $file
}
do-stuff foo.txt -WhatIf
Run Code Online (Sandbox Code Playgroud)
结果:
What if: Performing operation "Remove File" on Target "C:\temp\foo.txt".
Run Code Online (Sandbox Code Playgroud)
明确使用ShouldProcess()方法来确定是否-WhatIf通过了:
function do-stuff
{
[CmdletBinding(SupportsShouldProcess=$True)]
param([string]$file)
if ($PSCmdlet.ShouldProcess($file)) {
Write-Host "Deleting file"
Remove-Item $file
}
}
do-stuff foo.txt -WhatIf
Run Code Online (Sandbox Code Playgroud)
结果:
What if: Performing operation "do-stuff" on Target "foo.txt".
Run Code Online (Sandbox Code Playgroud)
SupportsShouldProcess属性自动传播-WhatIf到嵌套函数:
function do-stuff
{
[CmdletBinding(SupportsShouldProcess=$True)]
param([string]$file)
if ($PSCmdlet.ShouldProcess($file)) {
Write-Host "Deleting file"
Remove-Item $file
}
inner "text"
}
function inner
{
[CmdletBinding(SupportsShouldProcess=$True)]
param([string]$s)
if ($PSCmdlet.ShouldProcess($s)) {
Write-Host "Inner task"
}
$s | out-file "temp9.txt"
}
do-stuff foo.txt -WhatIf
Run Code Online (Sandbox Code Playgroud)
结果:
What if: Performing operation "do-stuff" on Target "foo.txt".
What if: Performing operation "inner" on Target "text".
What if: Performing operation "Output to File" on Target "temp9.txt".
Run Code Online (Sandbox Code Playgroud)
不幸的是,-WhatIf不会自动传播到不同模块中定义的函数.请参阅Powershell:如何将-whatif传播到另一个模块中的cmdlet以供讨论和解决此问题.
您可能指的是cmdlet 上的-WhatIf和-Confirm参数。您可以在以下位置阅读有关它们的信息Get-Help about_commonParameters:
风险管理参数说明
Run Code Online (Sandbox Code Playgroud)-WhatIf[:{$true | $false}]显示描述命令效果的消息,而不是执行命令。
$WhatIfPreferenceWhatIf 参数覆盖当前命令的变量值。$WhatIfPreference由于该变量的默认值为0(禁用),因此如果没有 WhatIf 参数,则不会执行 WhatIf 行为。有关更多信息,请键入以下命令:Run Code Online (Sandbox Code Playgroud)get-help about_preference_variables有效值:
$true(-WhatIf:$true)。具有与 相同的效果-WhatIf。
$false(-WhatIf:$false)。抑制当 $WhatIfPreference 变量的值为 1 时产生的自动 WhatIf 行为。例如,以下命令使用命令
WhatIf中的参数Remove-Item:Run Code Online (Sandbox Code Playgroud)PS> remove-item date.csv -whatifWindows PowerShell 并没有删除该项目,而是列出了它将执行的操作以及将受影响的项目。该命令产生以下输出:
Run Code Online (Sandbox Code Playgroud)What if: Performing operation "Remove File" on Target "C:\ps-test\date.csv".Run Code Online (Sandbox Code Playgroud)-Confirm[:{$true | $false}]执行命令之前提示您确认。
Confirm 参数会覆盖当前命令的 $ConfirmPreference 变量的值。默认值为高。有关更多信息,请键入以下命令:
Run Code Online (Sandbox Code Playgroud)get-help about_preference_variables有效值:
$true(-WhatIf:$true)。具有与 相同的效果-Confirm。
$false(-Confirm:$false)。抑制自动确认,当 的值$ConfirmPreference小于或等于 cmdlet 的估计风险时会发生这种情况。例如,以下命令将“Confirm”参数与“Remove-Item”命令一起使用。在删除项目之前,Windows PowerShell 会列出它将执行的操作以及将受影响的项目,并请求批准。
Run Code Online (Sandbox Code Playgroud)PS C:\ps-test> remove-item tmp*.txt -confirm该命令产生以下输出:
Run Code Online (Sandbox Code Playgroud)Confirm Are you sure you want to perform this action? Performing operation "Remove File" on Target " C:\ps-test\tmp1.txt [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):
| 归档时间: |
|
| 查看次数: |
27083 次 |
| 最近记录: |