2015-04-01 777 views
1

我在写PowerShell脚本,它依次执行多个PowerShell cmdlet。其中的一个cmdlet要求用户输入,如[是/否]。我想要永远传递值作为Y.有没有办法做到这一点?自动化PowerShell脚本中的用户输入

+0

请问您可以添加一个简单示例吗? – 2015-04-01 07:03:35

回答

4

听起来像你需要改变$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 #> 

你也可以做到对每个cmdlet的基础上, -Confirm:$false参数:

Set-ADUser -Description $desc -Confirm:$false 

请注意,这只适用于如果Cmdlet支持common parameter confirmation。它不会对自制cmdlet产生任何影响,并带有时髦的自制确认逻辑