gpt4 book ai didi

powershell - 当函数没有 WhatIf 参数时,抑制被调用函数/cmdlet 的 WhatIf 输出

转载 作者:行者123 更新时间:2023-12-03 09:56:54 26 4
gpt4 key购买 nike

TL;DR - 当函数/cmdlet 没有 -WhatIf 参数但我如何指定 -WhatIf:$false仍然对父调用中的 -WhatIf 使用react?

考虑以下稍微做作的示例:

Import-Module ActiveDirectory

Function Test-MyFunction {
[CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact='High')]
Param(
[Parameter(Mandatory=$true)]
[String] $Identity
)

$private:q = ($Identity -replace "'","''")
if (Get-ADServiceAccount -Filter "sAMAccountName -eq '${private:q}'") {
if (-not (Test-ADServiceAccount -Identity $Identity `
-WarningAction 'SilentlyContinue' `
-ErrorAction 'SilentlyContinue'))
{
if ($PSCmdlet.ShouldProcess($Identity, 'Install')) {
Install-ADServiceAccount -Identity $Identity
}
else {
Write-Verbose "Skipped processing '$Identity'"
}
}
else {
Write-Verbose "Already registered '$Identity'"
}
}
else {
Write-Warning "'$Identity' does not exist"
}
}

...以及可能的输出:

PS> Test-MyFunction -Verbose -Identity 'nonexistent$'
WARNING: 'nonexistent$' does not exist

PS> Test-MyFunction -Verbose -Identity 'registered$'
VERBOSE: Already registered 'registered$'

PS> Test-MyFunction -Verbose -Identity 'notregistered$'
Confirm
Are you sure you want to perform this action?
Performing the operation "Install" on target "notregistered$"
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): n
VERBOSE: Skipped processing 'notregistered$'

如果我将 -WhatIf 添加到这些调用中,您将得到以下内容:

PS> Test-MyFunction -WhatIf -Verbose -Identity 'nonexistent$'
WARNING: 'nonexistent$' does not exist

PS> Test-MyFunction -WhatIf -Verbose -Identity 'registered$'
What if: Performing the operation "Test" on target "CN=registered,CN=Managed Service Accounts,DC=contoso,DC=local"
What if: Performing the operation "Install" on target "registered$"
VERBOSE: Skipped processing 'registered$'

PS> Test-MyFunction -WhatIf -Verbose -Identity 'notregistered$'
What if: Performing the operation "Test" on target "CN=notregistered,CN=Managed Service Accounts,DC=contoso,DC=local"
What if: Performing the operation "Install" on target "notregistered$"
VERBOSE: Skipped processing 'notregistered$'

通过在 my 函数调用上指定 -WhatIf,它已将优先级传递给由我的函数调用的函数。这是设计使然,在大多数情况下,这是您想要的。

为了完整性,Test-ADServiceAccount 应该返回 $true$false,具体取决于是否已安装特定的托管服务本地计算机(至少,如果您抑制其警告/错误,它会这样做)。

但是,在这种情况下,由于 Test-ADServiceAccount 的编写方式,如果 my 上提供了 -WhatIf函数调用,它从不实际执行测试,所以总是返回 $false。这意味着我的 if/else 逻辑被破坏了——我的函数应该指示 Install-ADServiceAccount 会如果 -WhatIf 被删除,则为 registered$ 调用。

在正常情况下,我会简单地将 -WhatIf:$false 添加到被调用函数的末尾,但是 Test-ADServiceAccount 不会公开它自己的 -WhatIf 参数:

PS> Test-ADServiceAccount -WhatIf:$false -Identity 'registered$'
Test-ADServiceAccount : A parameter cannot be found that matches parameter name 'WhatIf'.

我很欣赏可能有更好的方法来编写函数,并且这里没有真正造成伤害,因为 Install-ADServiceAccount 无论如何都没有运行,但这个函数是作为 MCVE ,而不是需要明确改进的东西。我也很欣赏 Test-ADServiceAccount 可能不需要 -WhatIf 语义并且可能本身不应该做 $PSCmdlet.ShouldProcess (我假设它正在做),但 ActiveDirectory 不是我的模块,我认为我不会很快改变它的行为。

在一般情况下,当< em>my 函数使用其 -WhatIf 参数调用,被调用 函数不公开其自己的 -WhatIf 参数。

最佳答案

您可以在调用 Test-AdServiceAccount 之前将 $WhatIfPreference 设置为 $false。之后你可以恢复偏好

    function Function1 {
[CmdletBinding(SupportsShouldProcess = $true)]
param (
)

$origWhatIfPreference = $WhatIfPreference.IsPresent

$WhatIfPreference = $false
Function2
$WhatIfPreference = $origWhatIfPreference

if ($PSCmdlet.ShouldProcess("target", "command")) {
Write-Host "test"
}
}

function Function2 {
[CmdletBinding(SupportsShouldProcess = $true)]
param (
)
if ($PSCmdlet.ShouldProcess("f2: target", "f2: command")) {
Write-Host "f2: test"
}
}

在此 stackoverflow answer 中找到 $WhatIfPreference.IsPresent .

似乎 Test-AdServiceAccount 以动态方式检测 WhatIf 开关,例如通过 $WhatIfPreference = $PSCmdlet.SessionState.PSVariable.GetValue('WhatIfPreference')

更新:

根据@Jessens 的评论,您还可以将特定函数包装在脚本 block 中并调用它 invoke-command { $WhatIfPreference = $false; Function2;},在这里你不需要重建旧的 $WhatIfPreference 状态。

希望对您有所帮助。

关于powershell - 当函数没有 WhatIf 参数时,抑制被调用函数/cmdlet 的 WhatIf 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56301648/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com