2015-04-01 38 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產生任何影響,並帶有時髦的自制確認邏輯