PowerShell 技能连载 - 研究 ConfirmImpact(第 2 部分:脚本作者视角)
在前面的部分中,已经解释了 PowerShell 将 $ConfirmPreference 自动变量作为其风险缓解系统的一部分使用:每当一个 PowerShell 命令自身的 “ConfirmImpact“ 高于或等于 $ConfirmPreference 中的设置时,PowerShell 将显示自动确认对话框。
作为函数或脚本作者,您可以使用属性 CmdletBinding() 设置函数或脚本的“影响级别”:
1 | function Remove-Something |
示例函数 Remove-Something 已将其 ConfirmImpact 声明为 “Medium“,因此当您运行它时,如果 $ConfirmPreference 设置为 “Medium“ 以上的级别,PowerShell 将触发自动确认。
二进制 cmdlet 也是一样。要找出二进制 cmdlet 的确认影响,您可以使用以下函数:
1 | filter Get-ConfirmInfo |
当您提交命令名称时,它将显示命令作者定义的 ConfirmImpact,以及命令是否支持模拟开关,例如 -WhatIf(在这个情况下,SupportsShouldProcess 显示 $true)。
1 | PS> 'Get-Random', 'Remove-Item', 'Stop-Process' | Get-ConfirmInfo |
由于 Get-ConfirmInfo 支持管道,因此您甚至可以检查您的 PowerShell 命令集并搜索高影响级别的命令:
1 | PS> Get-Command -Verb Remove | Get-ConfirmInfo | Where-Object ConfirmImpact -eq High |
PowerShell 技能连载 - 研究 ConfirmImpact(第 2 部分:脚本作者视角)
http://blog.vichamp.com/2023/02/24/investigating-confirmimpact-part-2-script-author-perspective/

