I'm a student and I need to write a script in Powershell ISE that will provide the user with a menu and then display OS information based on their selection or quit if they enter 'q'
I've got the menu input/output loop working but I'm also required to make sure they selected a valid option and reprompt them if not.
It outputs the invalid message no matter what the user input value is.
So if the user enters 1 it will return:
Invalid input, please select an option from the menu @{OsName=Microsoft Windows Server 2016 Standard} Press Enter to continue...:
I have no background in programming so my understanding of powershell and coding is very basic and most search results have provided examples that are beyond the scope of my knowledge!
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')