【发布时间】:2015-04-01 07:01:08
【问题描述】:
我正在编写 powershell 脚本,它按顺序执行多个 powershell cmdlet。其中一个 cmdlet 要求用户输入,例如 [Y/N]。我想始终将值作为 Y 传递。有没有办法做到这一点?
【问题讨论】:
-
你能添加一个最小的例子吗?
标签: powershell powershell-cmdlet
我正在编写 powershell 脚本,它按顺序执行多个 powershell cmdlet。其中一个 cmdlet 要求用户输入,例如 [Y/N]。我想始终将值作为 Y 传递。有没有办法做到这一点?
【问题讨论】:
标签: powershell powershell-cmdlet
听起来您需要更改 $ConfirmPreference 变量。
来自Get-Help about_Preference_variables的输出:
$ConfirmPreference
------------------
Determines whether Windows PowerShell automatically prompts you for
confirmation before running a cmdlet or function.
When the value of the $ConfirmPreference variable (High, Medium, Low) is
less than or equal to the risk assigned to the cmdlet or function (High,
Medium, Low), Windows PowerShell automatically prompts you for confirmation
before running the cmdlet or function.
If the value of the $ConfirmPreference variable is None, Windows PowerShell
never automatically prompts you before running a cmdlet or function.
因此,为了抑制这些确认消息,只需执行以下操作:
$ConfirmPreference = "None"
<# script here #>
您也可以使用 -Confirm:$false 参数基于每个 cmdlet 执行此操作:
Set-ADUser -Description $desc -Confirm:$false
请注意,这仅在 Cmdlet 支持 common parameter confirmation 时才有效。它不会对具有时髦的自制确认逻辑的本土 cmdlet 产生任何影响
【讨论】: