如何在powershell中使用'dry-run'

bow*_*ang 13 powershell

我只是一个初学者,干跑是非常有用的参数测试,任何身体可以告诉我如何使用它一个简单的方法?我用谷歌搜索,但其用途很少.

非常感谢你

Mic*_*ens 25

在现有cmdlet上使用

显式提供-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)

在函数或脚本中的现有cmdlet上使用

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以供讨论和解决此问题.


Joe*_*oey 4

您可能指的是cmdlet 上的-WhatIf-Confirm参数。您可以在以下位置阅读有关它们的信息Get-Help about_commonParameters

风险管理参数说明

-WhatIf[:{$true | $false}]
Run Code Online (Sandbox Code Playgroud)

显示描述命令效果的消息,而不是执行命令。

$WhatIfPreference WhatIf 参数覆盖当前命令的变量值。$WhatIfPreference由于该变量的默认值为 0(禁用),因此如果没有 WhatIf 参数,则不会执行 WhatIf 行为。有关更多信息,请键入以下命令:

get-help about_preference_variables
Run Code Online (Sandbox Code Playgroud)

有效值:

$true( -WhatIf:$true)。具有与 相同的效果-WhatIf
$false( -WhatIf:$false)。抑制当 $WhatIfPreference 变量的值为 1 时产生的自动 WhatIf 行为。

例如,以下命令使用命令WhatIf中的参数 Remove-Item

PS> remove-item date.csv -whatif
Run Code Online (Sandbox Code Playgroud)

Windows PowerShell 并没有删除该项目,而是列出了它将执行的操作以及将受影响的项目。该命令产生以下输出:

What if: Performing operation "Remove File" on
Target "C:\ps-test\date.csv".
Run Code Online (Sandbox Code Playgroud)
-Confirm[:{$true | $false}]
Run Code Online (Sandbox Code Playgroud)

执行命令之前提示您确认。

Confirm 参数会覆盖当前命令的 $ConfirmPreference 变量的值。默认值为高。有关更多信息,请键入以下命令:

get-help about_preference_variables
Run Code Online (Sandbox Code Playgroud)

有效值:

$true( -WhatIf:$true)。具有与 相同的效果-Confirm
$false( -Confirm:$false)。抑制自动确认,当 的值$ConfirmPreference小于或等于 cmdlet 的估计风险时会发生这种情况。

例如,以下命令将“Confirm”参数与“Remove-Item”命令一起使用。在删除项目之前,Windows PowerShell 会列出它将执行的操作以及将受影响的项目,并请求批准。

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"):
Run Code Online (Sandbox Code Playgroud)