【发布时间】:2021-03-23 08:34:04
【问题描述】:
我想在我的函数中包含Whatif 和Confirm,但我遇到了这些参数的问题。
我的函数结构如下:
function Get-Stuff {
[CmdletBinding(SupportsShouldProcess, ConfirmImpact='High')]
param ( {...} )
process {
if ($PSCmdlet.ShouldProcess($Name, "Delete user")) {
$result = Invoke-RestMethod @restBody
}
}
end {
Write-Output -InputObject $result
Remove-Variable -Name result
}
}
我养成了用Remove-Variable 清理结束块中的变量的习惯。当我现在使用-WhatIf 或-Confirm 参数(并拒绝它)时,我收到$result 变量为空的错误。
ErrorRecord : Cannot find a variable with the name 'result'.
我知道在这种情况下会跳过 RestMethod,但我会假设函数的其余部分不会进一步执行。
我现在的问题是,是添加一个 else 子句来结束函数的继续执行还是我错误地使用了这些参数?
【问题讨论】:
标签: powershell confirm