簡體   English   中英

powershell 強制參數和用戶輸入消息框

[英]powershell mandatory parameter and user input message box

早上好

有沒有辦法讓用戶在消息框中輸入強制參數?

Param(

  [Parameter(Mandatory=$True)][string]$parameter1,
  [Parameter(Mandatory=$True)][string]$parameter2,
  [Parameter(Mandatory=$True)][string]$parameter3

)

例如,我想要一個帶有文本字段的彈出框,用戶必須填寫強制參數所需的值

$confirm = [System.Windows.MessageBox]::Show('Please confirm you want to start)

感謝任何回答的人一百萬。

評論中鏈接的答案中所述,您可以使用[Microsoft.VisualBasic.Interaction]::InputBox() static 方法提示用戶輸入字符串。

現在是參數綁定行為。 這可能看起來有點違反直覺,但是如果您刪除Mandatory標志並提供一個調用您的 GUI 組件作為其默認參數值的表達式,則可以使用您自己的 GUI 提示覆蓋默認提示:

Param(
  [Parameter(Mandatory=$True)][string]$parameter1 = $([Microsoft.VisualBasic.Interaction]::InputBox("What should parameter1 be?", "Parameter1 input prompt"))
)

現在,如果用戶不提供參數參數,PowerShell 不會中斷自己的提示(因為參數不再是強制的),而是繼續計算默認值表達式,依次顯示輸入框提示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM