如何在PowerShell中建立使用者選單?


當您編寫指令碼並希望允許使用者從多個值中選擇一個選項,並根據該選項執行命令時,通常可以使用**Switch**命令。為此,我們將在下面的指令碼中詢問使用者的選擇。

還有一種**.Net**方法可以建立使用者選單,我們將在檢視上面描述的示例之後再介紹它。

a. Switch 命令方法

對於基於 Switch 命令的使用者選擇,我們將首先向使用者顯示訊息,然後透過 Read-Host 命令提供給他選擇選項,如下所示。

Write-Host "============= Pick the Server environment=============="
Write-Host "`ta. 'P' for the Prod servers"
Write-Host "`tb. 'T' for the Test servers"
Write-Host "`tc. 'D for the Dev Servers'"
 Write-Host "`td. 'Q to Quit'"
Write-Host "========================================================"
$choice = Read-Host "`nEnter Choice"

一旦我們準備好藍圖,我們將使用 Switch 命令根據使用者選擇執行步驟。

switch ($choice) {
   'P'{
         Write-Host "`nYou have selected a Prod Environment"
   }
   'T'{
         Write-Host "`nYou have selected a Test Environment"
   }
   'D'{
         Write-Host "`nYou have selected a Dev Environment"
   }
   'Q'{Return}
}

如果您想限制使用者只能從給定值中選擇,則可以使用**do-until**迴圈。您還可以使用不同的 cmdlet 並根據您的標準設定條件。我們將在下面編寫整個指令碼以及使用者選擇限制。

do {
Write-Host "`n============= Pick the Server environment=============="
Write-Host "`ta. 'P' for the Prod servers"
Write-Host "`tb. 'T' for the Test servers"
Write-Host "`tc. 'D for the Dev Servers'"
Write-Host "`td. 'Q to Quit'"
Write-Host "========================================================"
$choice = Read-Host "`nEnter Choice"
} until (($choice -eq 'P') -or ($choice -eq 'T') -or ($choice -eq 'D') -or ($choice -eq 'Q') )
switch ($choice) {
   'P'{
       Write-Host "`nYou have selected a Prod Environment"
   }
   'T'{
      Write-Host "`nYou have selected a Test Environment"
   }
   'D'{
       Write-Host "`nYou have selected a Dev Environment"
    }
    'Q'{
      Return
   }
}

輸出

============= Pick the Server environment==============
         a. 'P' for the Prod servers
         b. 'T' for the Test servers
         c. 'D for the Dev Servers'
         d. 'Q to Quit'
 ========================================================
 Enter Choice: r
============= Pick the Server environment==============
         a. 'P' for the Prod servers
         b. 'T' for the Test servers
         c. 'D for the Dev Servers'
         d. 'Q to Quit'
 ========================================================
Enter Choice: T
You have selected a Test Environment

b. .NET 選單選項

使用**.Net**名稱空間,我們還可以自動化上述選擇選單,這是實際的標準流程。為此,我們將利用.Net框架的**System.Management.Automation.Host**名稱空間,然後需要使用它的[ChoiceDescription]類。其語法如下。

[ChoiceDescription]::new('Label', 'HelpMessage')

您可以從下面給出的URL瞭解更多關於上述類的資訊。

https://docs.microsoft.com/en-us/dotnet/api/system.management.automation.host.choicedescription?view=powershellsdk-7.0.0

using namespace System.Management.Automation.Host
$Prod = [ChoiceDescription]::new('&Prod', 'Environment:Prod')
$Test = [ChoiceDescription]::new('&Test', 'Environment:Test')
 $Dev = [ChoiceDescription]::new('&Dev', 'Environment:Dev')
$Envs = [ChoiceDescription[]]($prod,$Test,$Dev)
$choice = $host.ui.PromptForChoice("Select Environment", "Prod?, Dev?, Test?", $envs, 0)

在上面的例子中,我們為環境建立了不同的變數並將它們合併到名為$envs的陣列中,稍後為了顯示UI提示,我們需要使用PromptForChoice類或函式。

PromptForChoice類的語法。

PSHostUserInterface.PromptForChoice(String, String, Collection<ChoiceDescription>, Int32)

有關**PromptForChoice**類的更多資訊,請訪問以下URL。

https://docs.microsoft.com/en-us/dotnet/api/system.management.automation.host.pshostuserinterface.promptforchoice?view=powershellsdk-7.0.0

因此,整個指令碼將如下所示:

using namespace System.Management.Automation.Host $Prod = [ChoiceDescription]::new('&Prod', 'Environment:Prod') $Test = [ChoiceDescription]::new('&Test', 'Environment:Test') $Dev = [ChoiceDescription]::new('&Dev', 'Environment:Dev') $Envs = [ChoiceDescription[]]($prod,$Test,$Dev) $choice = $host.ui.PromptForChoice("Select Environment", "Prod?, Dev?, Test?", $envs, 0) switch ($choice) { 0{ Write-Host "`nYou have selected a Prod Environment" } 1{ Write-Host "`nYou have selected a Test Environment" } 2{ Write-Host "`nYou have selected a Dev Environment" } }

輸出

You have selected a Dev Environment

如果您注意到上面提到的兩種方法中兩個Switch命令之間的區別,在第一種情況下,我們使用了Read-Host引數來讀取選擇,然後將其傳遞給Switch命令,並根據選擇來選擇Switch命令條件,但在第二個示例中,我們使用了編號,它與使用者輸入的選擇相關。如果使用者選擇了第一個選項,“Prod”,則它轉到值'0',對於第二個值,它是'1',對於第三個值,它是'2',依此類推。

更新於:2020年7月3日

3K+ 次瀏覽

啟動你的職業生涯

完成課程獲得認證

開始
廣告