我是一名學生,我需要在 Powershell ISE 中撰寫一個腳本,該腳本將為用戶提供一個選單,然后根據他們的選擇顯示作業系統資訊,或者在他們輸入“q”時退出
我有選單輸入/輸出回圈作業,但我還需要確保他們選擇了一個有效的選項,如果沒有,則重新提示他們。
無論用戶輸入的值是什么,它都會輸出無效訊息。
所以如果用戶輸入 1 它將回傳:
輸入無效,請從選單中選擇一個選項@{OsName=Microsoft Windows Server 2016 Standard} 按 Enter 繼續...:
我沒有編程背景,所以我對 powershell 和編碼的理解非常基礎,大多數搜索結果提供的示例超出了我的知識范圍!
function sysInfoMenu {
param (
[string] $Title = 'System Information'
)
Clear-Host
Write-Host "=============== $Title ==============="
"`n`n"
Write-Host "Please select from the following options:"
"`n"
Write-Host "Press 1 for: OS System"
Write-Host "Press 2 for: OS Type"
Write-Host "Press 3 for: OS Architecture (32 or 64 bit)"
Write-Host "Press 4 for: OS Version"
Write-Host "Press 5 for: OS Bios Version"
Write-Host "Press 6 for: Computer Brand"
Write-Host "Press 7 for: Computer Name"
Write-Host "Press 8 for: Current Domain"
Write-Host "Press 9 for: Computer Model"
Write-Host "Press 10 for: Current User"
Write-Host "Press 11 for: Timezone"
Write-Host "Press 'q' to quit."
}
do {
sysInfoMenu
$info = $null
$UserInput = Read-Host "Please select an option from the menu"
switch ($UserInput)
{
'1' {$info = Get-ComputerInfo -Property OSName}
'2' {$info = Get-ComputerInfo -Property OSType}
'3' {$info = Get-ComputerInfo -Property OSArchitecture}
'4' {$info = Get-ComputerInfo -Property OSVersion}
'5' {$info = Get-ComputerInfo -Property BiosVersion}
'6' {$info = Get-ComputerInfo -Property CsManufacturer}
'7' {$info = Get-ComputerInfo -Property CsName}
'8' {$info = Get-ComputerInfo -Property CsDomain}
'9' {$info = Get-ComputerInfo -Property CsModel}
'10' {$info = Get-ComputerInfo -Property CsUserName}
'11' {$info = Get-ComputerInfo -Property TimeZone}
}
if ($UserInput -lt 1 -gt 11 -ne 'q'){
Write-Host "Invalid input, please select an option from the menu"
}
Write-Host $info
pause
}
until ($UserInput -eq 'q')
uj5u.com熱心網友回復:
你有幾個結構性問題。
- 你沒有終止你的功能。
- 你沒有呼叫你的函式。
- 您一直在呼叫 Get-ComputerInfo,這很慢并且只需要執行一次。
- 您的選單需要在您的回圈中,以便您可以清除螢屏并擺脫以前的答案和/或錯誤訊息。
這是我評論的一些替代代碼,以幫助您學習。
function SysInfoMenu {
param (
[Parameter(Mandatory=$False)]
[string] $Title = 'System Information'
)
#Setup:
$ByeBye = $False #Set initial value.
#Hide the Progress Bar. Remove line if not wanted.
$ProgressPreference = "SilentlyContinue"
$CompInfo = Get-ComputerInfo #You only need to call once.
#Main Function Code:
do {
Clear-Host
Write-Host "=============== $Title ==============="
"`n`n"
Write-Host "Please select from the following options:"
"`n"
Write-Host "Press 1 for: OS System"
Write-Host "Press 2 for: OS Type"
Write-Host "Press 3 for: OS Architecture (32 or 64 bit)"
Write-Host "Press 4 for: OS Version"
Write-Host "Press 5 for: OS Bios Version"
Write-Host "Press 6 for: Computer Brand"
Write-Host "Press 7 for: Computer Name"
Write-Host "Press 8 for: Current Domain"
Write-Host "Press 9 for: Computer Model"
Write-Host "Press 10 for: Current User"
Write-Host "Press 11 for: Timezone"
Write-Host "Press 'q' to quit."
$UserInput = Read-Host "Please select an option from the menu"
#Note: use of Break to leave switch once match is found.
switch ($UserInput) {
'1' {$CompInfo.OSName ;Break}
'2' {$CompInfo.OSType ;Break}
'3' {$CompInfo.OSArchitecture ;Break}
'4' {$CompInfo.OSVersion ;Break}
'5' {$CompInfo.BiosVersion ;Break}
'6' {$CompInfo.CsManufacturer ;Break}
'7' {$CompInfo.CsName ;Break}
'8' {$CompInfo.CsDomain ;Break}
'9' {$CompInfo.CsModel ;Break}
'10' {$CompInfo.CsUserName ;Break}
'11' {$CompInfo.TimeZone ;Break}
'q' {$ByeBye = $True ;Break}
default {"Invalid input, please select an option from the menu"}
} #End Switch
if (-not $ByeBye) { pause }
} until ($ByeBye) #Exit via $ByeBye value!
} #End Function SysInfoMenu
SysInfoMenu #Call your function
uj5u.com熱心網友回復:
您的腳本幾乎沒問題,只是您的if病情有問題:
$UserInput -lt 1 -gt 11 -ne 'q'
這就是 PowerShell 評估陳述句的方式,以及它始終輸入if條件的原因。使用單詞test為$userInput:
'test' -lt 1 # => $false
$false -gt 11 # => $false
$false -ne 'q' # => $true
這是它的外觀,請注意使用-and:
'test' -lt 1 -and 'test' -gt 11 -and 'test' -ne 'q'
您可以使用-notmatch允許使用正則運算式的運算子的替代方法。
在下面的示例中,$UserInput -notmatch $regex將$true在任何用戶輸入時回傳,而不是我們在$validoptions陣列上看到的輸入(從1到11 q)。
$validoptions = 1..11 'q'
$regex = '^{0}$' -f ($validoptions -join '$|^')
# Regex: ^1$|^2$|^3$|^4$|^5$|^6$|^7$|^8$|^9$|^10$|^11$|^q$
$waitForUSer = {
'Press ENTER to Continue...'
Read-Host
}
do
{
sysInfoMenu
$UserInput = Read-Host "Please select an option from the menu"
if ($UserInput -notmatch $regex){
Write-Warning "Invalid input, please select an option from the menu"
& $waitForUSer
continue # => Go to Until, skip next lines
}
if($UserInput -eq 'q'){
continue # => Go to Until and end the loop
}
$info = switch ($UserInput)
{
'1' { 'Option 1' }
'2' { 'Option 2' }
'3' { 'Option 3' }
'4' { 'Option 4' }
'5' { 'Option 5' }
'6' { 'Option 6' }
'7' { 'Option 7' }
'8' { 'Option 8' }
'9' { 'Option 9' }
'10' { 'Option 10' }
'11' { 'Option 11' }
}
$info # => Show selection
& $waitForUSer
}
until ($UserInput -eq 'q')
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/376425.html
標籤:电源外壳
