Yas*_*con 5 powershell automation
我正在尝试使用 PS 停止 iis 网站,但也尝试跳过烦人的确认框
如果我运行以下脚本并接受确认,它将很好地停止
SAPS PowerShell -Verb RunAs -ArgumentList "Stop-IISSite -Name 'website'"
Run Code Online (Sandbox Code Playgroud)
但是,如果我添加 cmdlet 将确认设置为 false,它不会给我任何错误或确认,但不会停止该站点。
SAPS PowerShell -Verb RunAs -ArgumentList "Stop-IISSite -Name 'website' -Force -Confirm:$false"
Run Code Online (Sandbox Code Playgroud)
知道为什么吗?
$false当您将字符串传递给 时, 正在展开ArgumentList。使用单引号或转义$in 中使用的内容$false,以便在子进程执行之前它不会扩展:
# Use double-quotes and escape the $ if you have a variable that *does*
# need to be expanded in the current session executes,
# such as the -Name
"Stop-IISSite -Name $name -Force -Confirm:`$false"
# Otherwise single-quotes will prevent any variable expansion from
# the current session
'Stop-IISSite -Name ''website'' -Force -Confirm:$false'
Run Code Online (Sandbox Code Playgroud)
您现在执行此操作的方式会导致命令在子 PowerShell 进程中呈现,如下所示:
Stop-IISSite -Name $name -Force -Confirm:false
Run Code Online (Sandbox Code Playgroud)
请注意缺少$after -Confirm:,但强制设置[switch]状态需要[bool]类型。$false是 a [bool],但是当以这样的字符串呈现时,它将被计算为文字字符串false。
我很惊讶你没有收到错误,因为-Confirm:false抛出了类型转换错误。$ErrorActionPreference如果您已设置为SilentlyContinue或其他位置,则可以解释这一点Ignore,但更有可能的是,错误在窗口关闭之前很快显示在子进程窗口中。
如果您想了解更多信息,本文将更详细地解释字符串中的变量扩展。
| 归档时间: |
|
| 查看次数: |
3278 次 |
| 最近记录: |