-1

Hello I am trying to create a powershell menu that has options within it to run at a press of an option. However I am stuck where if I run the ping option the response comes in for a brief half a second and not sure why it disappears right away. Any advice or help appreciated! Im new to powershell still trying to get the hang of it using examples and things researched. If any learning advice im open ears as well!

Function Menu 
{
    Clear-Host        
    Do
    {
        Clear-Host                                                                       
        Write-Host -Object 'Please choose an option'
        Write-Host     -Object '**********************'
        Write-Host -Object 'Scripts to run' -ForegroundColor Yellow
        Write-Host     -Object '**********************'
        Write-Host -Object '1.  Ping PC '
        Write-Host -Object ""
        Write-Host -Object '2.  Restart ELP Desktops '
        Write-Host -Object ''
        Write-Host -Object '3.  Restart NOVI Desktops '
        Write-Host -Object ''
        Write-Host -Object 'Q.  Quit'
        Write-Host -Object $errout
        $Menu = Read-Host -Prompt '(0-3 or Q to Quit)'
lit
  • 14,456
  • 10
  • 65
  • 119
  • switch ($Menu) { 1 { $Computer = Read-Host Please Enter Host name Get-WMIObject Win32_NetworkAdapterConfiguration -Computername $Computer | ` Where-Object {$_.IPEnabled -match "True"} | ` Select-Object -property DNSHostName,ServiceName,@{N="DNSServerSearchOrder"; E={"$($_.DNSServerSearchOrder)"}}, @{N='IPAddress';E={$_.IPAddress}}, @{N='DefaultIPGateway';E={$_.DefaultIPGateway}} | FT } 2 { – Hola_amigo06 Mar 10 '22 at 19:58
  • restart-computer -ComputerName } 3 { restart-computer -ComputerName -force } Q { Exit } default { $errout = 'Invalid option please try again........Try 0-3 or Q only' } } } until ($Menu -eq 'q') } # Launch The Menu Menu – Hola_amigo06 Mar 10 '22 at 19:59
  • Apologies did not let me edit easily the code block so had to post as comments for the rest – Hola_amigo06 Mar 10 '22 at 19:59

1 Answers1

0

You are shooting yourself in the foot by trying to show results without any form of pause and jumping backup to the Clear-Host command that wipes the screen.

In my experiment, which worked, I added the following function that I think I found from here:

function Pause {
    Write-Host -NoNewLine 'Press any key to continue...';
    $null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');
}

Then, at the end of the code for option 1, I added:

Pause

This allowed me to see the results before Clear-Host wipes it.

FYI:

I noticed that Format-Table has a -Property switch the same as Select-Object. Did an experiment and found it looks like you can drop the last pipe and replace Select-Object with Format-Table and get the same results. I couldn't see a different, so give it a try.

Darin
  • 1,423
  • 1
  • 10
  • 12
  • placed the pause and worked! I placed in line 33 thank you so much! I am using the template for menu but will look to try the select-object and see. I am still very knew at powershell so am thankful for the help! – Hola_amigo06 Mar 15 '22 at 15:39
  • Now I am looking to add a 4th option of check storage on input pc name but i think I broke this one " Invoke-Command $Computer = Read-Host Please Enter Host name -ScriptBlock{Get-WmiObject -Class Win32_logicalDisk -Filter "DeviceID='C:'" | Select SystemName, DeviceID, @{n='Size(GB)';e={$_.size / 1gb -as [int]}},@{n='Free(GB)';e={$_.Freespace / 1gb -as [int]}}} > C:\DiskInfo_output.txt " – Hola_amigo06 Mar 15 '22 at 17:49