Chapters/basic-controllers-scripts-and-menus/CimMenuSolution.ps1

#Requires -version 5.0

clear-host

$menu = @"
     
 System Information Menu
 
    1 LogicalDisks
    2 Services
    3 Operating system
    4 Computer system
    5 Processes
    6 Quit
 
"@


Write-Host $menu -ForegroundColor Yellow

$coll=@()

$item = New-Object System.Management.Automation.Host.ChoiceDescription]::new("&1 Disks")
$item.HelpMessage = "Get logical disk information"
#customize the object and add some PowerShell code to run
$item | Add-Member -MemberType ScriptMethod -Name Invoke -Value {
    Get-CimInstance -ClassName Win32_Logicaldisk -filter "drivetype=3" -ComputerName $computer
} -force

$coll+=$item

$item = New-Object System.Management.Automation.Host.ChoiceDescription]::new("&2 Services")
$item.HelpMessage = "Get service information"
$item | Add-Member -MemberType ScriptMethod -Name Invoke -Value {
    Get-CimInstance -ClassName Win32_service -ComputerName $computer
} -force

$coll+=$item

$item = New-Object System.Management.Automation.Host.ChoiceDescription]::new("&3 OS")
$item.HelpMessage = "Get operating system information"
$item | Add-Member -MemberType ScriptMethod -Name Invoke -Value {
    Get-CimInstance -ClassName win32_operatingsystem -ComputerName $computer |
    Select-Object -Property PSComputername,Caption,Version,InstallDate
} -force

$coll+=$item

$item = new-object System.Management.Automation.Host.ChoiceDescription]::new("&4 Computer")
$item.HelpMessage = "Get computer system information"
$item | Add-Member -MemberType ScriptMethod -Name Invoke -Value {
    Get-CimInstance -ClassName Win32_computersystem -ComputerName $computer | 
    Select Name,Model,Manufacturer,TotalPhysicalmemory
} -force

$coll+=$item

$item = new-object System.Management.Automation.Host.ChoiceDescription]::new("&5 Processes")
$item.HelpMessage = "Get process information"
$item | Add-Member -MemberType ScriptMethod -Name Invoke -Value {
    Get-CimInstance -ClassName Win32_process -ComputerName $computer
} -force

$coll+=$item

$item = new-object System.Management.Automation.Host.ChoiceDescription]::new("&6 Quit")
$item.HelpMessage = "Quit and exit"

$coll+=$item

[int]$r = $host.ui.PromptForChoice("Make a selection:","",$coll,5)

if ($r -eq 5) {
    Write-Host "`nHave a great day.`n" -ForegroundColor green
}
else {
    $computer = Read-Host "`nEnter a computername (leave blank for localhost)"
    if (-Not $computer) {
        #if no computer specified then default to the localhost
        $computer = $env:COMPUTERNAME
    }
    $coll[$r].invoke() | Out-Host

    pause
    #re-run the script
    & $MyInvocation.MyCommand
}



$coll+=$item



$item = new-object System.Management.Automation.Host.ChoiceDescription]::new("&4 Computer")

$item.HelpMessage = "Get computer system information"

$item | Add-Member -MemberType ScriptMethod -Name Invoke -Value {

    Get-CimInstance -ClassName Win32_computersystem -ComputerName $computer | 

    Select Name,Model,Manufacturer,TotalPhysicalmemory

} -force



$coll+=$item



$item = new-object System.Management.Automation.Host.ChoiceDescription]::new("&5 Processes")

$item.HelpMessage = "Get process information"

$item | Add-Member -MemberType ScriptMethod -Name Invoke -Value {

    Get-CimInstance -ClassName Win32_process -ComputerName $computer

} -force



$coll+=$item



$item = new-object System.Management.Automation.Host.ChoiceDescription]::new("&6 Quit")

$item.HelpMessage = "Quit and exit"



$coll+=$item



[int]$r = $host.ui.PromptForChoice("Make a selection:","",$coll,5)



if ($r -eq 5) {

    Write-Host "`nHave a great day.`n" -ForegroundColor green

}

else {

    $computer = Read-Host "`nEnter a computername (leave blank for localhost)"

    if (-Not $computer) {

        #if no computer specified then default to the localhost

        $computer = $env:COMPUTERNAME

    }

    $coll[$r].invoke() | Out-Host



    pause

    #re-run the script

    & $MyInvocation.MyCommand

}