Chapters/basic-controller-scripts-and-menus/basicmenu.ps1

#create a basic console based menu

#define a here string for the menu options
$menu = @"

        MyMenu
--------------------------
1. Get services
2. Get processes
3. Get System event logs
4. Check free disk space (MB)
5. Quit

Select a menu choice
"@


#Read-Host writes strings but we can specifically treat the result as
#an integer
[int]$r = Read-Host $menu

$Computername = Read-Host "Enter a computername or press Enter to use the localhost"
if ($Computername -notmatch "\w+") {
    $computername = $env:COMPUTERNAME
}

#code to execute
Switch ($r) {
    1 {
        Get-Service -computername $Computername
    }
    2 {
        Get-Process -computername $Computername
    }
    3 {
        Get-Eventlog -LogName System -Newest 25 -ComputerName $Computername
    }
    4 {
        $c = Get-CimInstance -ClassName win32_logicaldisk -ComputerName $computername -filter "deviceid='c:'"
        $c.FreeSpace/1mb
    }
    5 {
        Write-Host "Have a nice day" -ForegroundColor Green
    }
    default { 
        write-warning "$r is not a valid choice"
    }
}