gpt4 book ai didi

powershell - 如何不传播 -WhatIf 和 -Confirm?

转载 作者:行者123 更新时间:2023-12-03 12:43:06 25 4
gpt4 key购买 nike

Powershell 的默认行为似乎是将 -WhatIf 和 -Confirm 传播到从 -WhatIf 或 -Confirm 调用的函数中调用的所有函数。

但是如果(双关语)我不想要呢?如果我只想保护一些在我的系统上执行实际更改的代码而不是我的其余代码怎么办?

下面是一个例子:

function Set-Something {
[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Medium')]
[OutputType([string])]

Param (
[Parameter(Mandatory = $false, ValueFromPipeline = $true)]
[string] $ComputerName = $env:COMPUTERNAME
)

Process {
New-Item -Path "beforeChange" -Type File | Out-Null
Remove-Item -Path "beforeChange" -ErrorAction SilentlyContinue

# Here is the bit of code that needs to be "protected"
if ($PSCmdlet.ShouldProcess($ComputerName, "Do something")) {
# Actually do something on $ComputerName
New-Item -Path "inChange" -Type File | Out-Null
Remove-Item -Path "inChange" -ErrorAction SilentlyContinue
}

New-Item -Path "afterChange" -Type File | Out-Null
Remove-Item -Path "afterChange" -ErrorAction SilentlyContinue
}
}

"-"*50 + "Set-Something -WhatIf" + "-"*50
Set-Something -WhatIf
"-"*50 + "Set-Something -Confirm" + "-"*50
Set-Something -Confirm

Set-Something 函数仅在 protected 位中执行系统更改内容。它还使用临时文件,但 New-Item 和 Remove-Item 不应触发 WhatIf 或 Confirm。不幸的是,这不是它的工作原理:
--------------------------------------------------Set-Something -WhatIf--------------------------------------------------
What if: Performing the operation "Create File" on target "Destination: C:\Users\GO7\beforeChange".
What if: Performing the operation "Do something" on target "GEOCED".
What if: Performing the operation "Create File" on target "Destination: C:\Users\GO7\afterChange".

我可以添加 -WhatIf:$false -Confirm:$false到每个支持 ShouldProcess 的函数,但这会很乏味。

所以到目前为止我所做的是:
  • 将 $WhatIfPreference 和 $ConfirmPreference 保存在我的函数顶部
  • 将这些首选项重置为其默认值
  • 恢复 protected 块之前保存的首选项
  • 再次在块内和块外重置首选项

  • 像这样:
    function Set-Something2 {
    [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Medium')]
    [OutputType([string])]

    Param (
    [Parameter(Mandatory = $false, ValueFromPipeline = $true)]
    [string] $ComputerName = $env:COMPUTERNAME
    )

    Begin {
    # Save the ShouldProcess parameters for later use
    $WhatIfPref = $WhatIfPreference
    $ConfirmPref = $ConfirmPreference

    # Reset ShouldProcess parameters to their default value
    $WhatIfPreference = 0
    $ConfirmPreference = 'High'
    }

    Process {
    New-Item -Path "beforeChange" -Type File | Out-Null
    Remove-Item -Path "beforeChange" -ErrorAction SilentlyContinue

    # We restore the previously saved ShouldProcess parameters just before the "protected" bit of code
    $WhatIfPreference = $WhatIfPref
    $ConfirmPreference = $ConfirmPref

    # Here is the bit of code that needs to be "protected"
    if ($PSCmdlet.ShouldProcess($ComputerName, "Do something")) {
    # Inside the protected bit, we reset ShouldProcess parameters to their default value
    $WhatIfPreference = 0
    $ConfirmPreference = 'High'

    # Actually do something on $ComputerName
    New-Item -Path "inChange" -Type File | Out-Null
    Remove-Item -Path "inChange" -ErrorAction SilentlyContinue
    }

    # After the protected bit, we reset ShouldProcess parameters to their default value
    $WhatIfPreference = 0
    $ConfirmPreference = 'High'

    New-Item -Path "afterChange" -Type File | Out-Null
    Remove-Item -Path "afterChange" -ErrorAction SilentlyContinue
    }
    }

    "-"*50 + "Set-Something2 -WhatIf" + "-"*50
    Set-Something2 -WhatIf
    "-"*50 + "Set-Something2 -Confirm" + "-"*50
    Set-Something2 -Confirm

    结果如我所愿:
    --------------------------------------------------Set-Something2 -WhatIf--------------------------------------------------
    What if: Performing the operation "Do something" on target "GEOCED".
    --------------------------------------------------Set-Something2 -Confirm--------------------------------------------------

    Confirm
    Are you sure you want to perform this action?
    Performing the operation "Do something" on target "GEOCED".
    [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):

    但那是我应该做的吗?我觉得不是很优雅...

    最佳答案

    您的问题似乎与范围有关。运行它并阅读文档:

    help about_Scopes

    您可以更改范围中的首选项变量(例如, $ConfirmPreference ),但这不会影响其在父范围中的值。这是一个简短的例子:
    PS C:\Scripts> $ConfirmPreference
    High
    PS C:\Scripts> Get-Content .\Test-Preference.ps1
    $ConfirmPreference = "Low"
    $ConfirmPreference
    PS C:\Scripts> .\Test-Preference.ps1
    Low
    PS C:\Scripts> $ConfirmPreference
    High

    正如您从这个简短示例中看到的那样,一旦范围(即脚本)终止,首选项变量会自动恢复为以前的值。

    如果你想在当前范围内执行一个新的范围,你可以使用 & 调用一个脚本块。运算符(operator)。例子:
    $ConfirmPreference

    # execute a scriptblock (create new scope, change preference variable)
    & {
    $ConfirmPreference = "Low"
    $ConfirmPreference
    }

    $ConfirmPreference

    如果将上述行放在脚本文件中并运行它,输出将是:
    High
    Low
    High

    关于powershell - 如何不传播 -WhatIf 和 -Confirm?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46183443/

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