Powershell function menu infinite loop issue

I have been working on this function menu for a short bit and I have been stuck on trying to fix an issue that causes the menu to refresh but not allowing more than the one selected switch function to be run again and also, I am wanting to add Get-Date below the menu but every time I tried it would give me an error and last but not least I am unsure how to clear the screen by asking the user before looping to the menu again. Any and all help with the following code with greatly appreciated.

Do {
[int]$userMenuChoice = 0
while ( $userMenuChoice -lt 1 -or $userMenuChoice -gt 13 ) {
[email protected]"
1 Show all Network Connections and listening Ports
2 Show System Hostname
3 Show System IP Addresses
4 Show DNS Servers
5 Show Default Gateway
6 Show Amount of Available Memory
7 Show  all environment variables and the PATH environment variable
8 List Drivers Installed on System
9 List current running tasks
10 Kill a task
11 Display a text file on the screen
12 Display Get File Security Information 
13 Quit and Exit

Select a task by number or Q to quit
Write-Host "Project 1 Menu" -ForegroundColor Cyan
[int]$userMenuChoice = Read-Host $menu 
Switch ($userMenuChoice) {
"1" {
    Write-Host "Showing all Network Connections and Listening Ports" -ForegroundColor Green
    netsh interface ipv4 show interfaces
    Start-Sleep 5
    Get-NetTCPConnection -State listen
"2" {
    Write-Host "Showing System Hostname" -ForegroundColor Green

"3" {
    Write-Host "Show System IP Addresses" -ForegroundColor Green

"4" {
    Write-Host "Showing DNS Servers" -ForegroundColor Green
    Get-DnsClientServerAddress | Select-Object –ExpandProperty ServerAddresses
"5" {
    Write-Host "Showing Default Gateway" -ForegroundColor Green
    Get-NetIPConfiguration | Foreach IPv4DefaultGateway

"6" {
    Write-Host "Showing Amount of Available Memory" -ForegroundColor Green
    $freemem = Get-WmiObject -Class Win32_OperatingSystem

# Display free memory on PC/Server
"---------FREE MEMORY CHECK----------"
"System Name     : {0}" -f $freemem.csname
"Free Memory (MB): {0}" -f ([math]::round($freemem.FreePhysicalMemory / 1024, 2))
"Free Memory (GB): {0}" -f ([math]::round(($freemem.FreePhysicalMemory / 1024 / 1024), 2))

"7" {
    Write-Host "Showing all environment variable and the PATH environment variable" -ForegroundColor Green
    Get-ChildItem Env:
Write-Host "

Start-Sleep 5
Get-ChildItem Env:PATHEXT

"8" {
    Write-Host "Listing currently installed drivers" -ForegroundColor Green
    driverquery.exe /v /fo csv | ConvertFrom-CSV | Select-Object ‘Display Name’, ‘Start Mode’, ‘Paged Pool(bytes)’, Path

"9" {
    Write-Host "Listing currently running tasks" -ForegroundColor Green
    get-process | where-object {$_.mainwindowhandle -ne 0} 

"10" {
    Write-Host "Killing selected task" -ForegroundColor Green
    # Kill the process that the user selects
    $Process = Get-Process | Out-GridView -Title 'Please select a process to kill' -OutputMode Single;
    Stop-Process -InputObject $Process;

    # Prompt the user to abort or retry some operation, using custom input values
    do {
    $Result = @('Abort', 'Retry') | Out-GridView -Title 'Which action should be performed?' -OutputMode Single;
    } while ($Result -eq 'Retry');

"11" {
    Write-Host "Displaying selected text file" -ForegroundColor Green
    $FileDisplay = Read-Host -Prompt "Please input the file path to display the text from it";
    Get-Content $FileDisplay; 

"12" {
    Write-Host "Displaying Get-File Security Information" -ForegroundColor Green
    $FileDisplayS = Read-Host -Prompt "Please input the file path to display the acl of the folder or file";
Get-Acl $FileDisplayS; 

"13" {
    Write-Host "Quitting" -ForegroundColor Red

default {
    Write-Host "Please input a number for an option or press "Q" to quit." -ForegroundColor Yellow

  } #end switch 
} While ($userMenuChoice -ne 13 )

0 Comment



Captcha image