繁体   English   中英

如何在 powershell 中使用“试运行”

[英]how to use 'dry-run' in powershell

我只是一个初学者,空运行对于制作要测试的参数非常有用,任何人都可以告诉我如何以简单的方式使用它吗? 我用谷歌搜索,但关于它的用途的结果很少。

非常感谢

在现有cmdlet上使用

明确提供-WhatIf参数:

rm foo.txt -WhatIf

结果:

What if: Performing operation "Remove File" on Target "C:\temp\foo.txt".

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

SupportsShouldProcess属性会自动将-WhatIf传播到受支持的cmdlet:

function do-stuff
{
    [CmdletBinding(SupportsShouldProcess=$True)]
    param([string]$file)
    Remove-Item $file
}

do-stuff foo.txt -WhatIf

结果:

What if: Performing operation "Remove File" on Target "C:\temp\foo.txt".

在自己的代码块上使用

显式使用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

结果:

What if: Performing operation "do-stuff" on Target "foo.txt".

同一模块中的嵌套函数上使用

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

结果:

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".

其他模块中的嵌套函数上使用

不幸的是,-WhatIf不会自动传播到另一个模块中定义的函数。 请参阅Powershell:如何获取-whatif传播到另一个模块中的cmdlet,以进行讨论和解决方法。

您可能在引用cmdlet上的-WhatIf-Confirm参数。 您可以在Get-Help about_commonParameters阅读有关它们的Get-Help about_commonParameters

风险管理参数说明

 -WhatIf[:{$true | $false}] 

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

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

 get-help about_preference_variables 

有效值:

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

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

 PS> remove-item date.csv -whatif 

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

 What if: Performing operation "Remove File" on Target "C:\\ps-test\\date.csv". 
 -Confirm[:{$true | $false}] 

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

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

 get-help about_preference_variables 

有效值:

$true-WhatIf:$true )。 -Confirm具有相同的效果。
$false-Confirm:$false )。 禁止自动确认,当$ConfirmPreference的值小于或等于cmdlet的估计风险时,将发生自动确认。

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

 PS C:\\ps-test> remove-item tmp*.txt -confirm 

此命令产生以下输出:

 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"): 

我认为您正在寻找的实际上是可以执行模型的模块 Pester(自首次发布以来随 Windows 10 一起提供)。 即假装运行部分代码以进行真正的隔离单元测试。

虽然这是一个很大的话题......

MS 脚本 - 什么是 Pester,我为什么要关心它?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM