自动 powershell 脚本中的用户输入

User input in automated powershell scripts

我正在编写顺序执行多个 powershell cmdlet 的 powershell 脚本。其中一个 cmdlet 要求用户输入 [Y/N]。我想始终将值传递为 Y。有什么办法吗?

听起来您需要更改 $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 产生任何影响