After first run code doesn't return same information take 2

I have been testing a script that helps with various vCenter Server operations. Basically I created a menu with ‘switch’ and execute the code within the menu. In one menu option I list all the hosts in the vCenter server with get-vmhost. I get the proper formatting on the output as expected. On a second pass (staying in the script) the output seems to cut off the first couple of rows. I do not get a title row or the dashed underlines. In my larger script (of which the code below was pulled from) if I progress down the available options then go back to, say , the 1st option it is totally broke, like it never executes the get-vmhost. Restarting the script or ISE fixes the issue (only on first run). What is happening here? Been banging my head against the wall for a week on this and can’t find a reason. Any help in how I can troubleshoot this would be helpful. See below. In selection one I have a sleep -S 3 to be able to see the output in subsequent runs before the cls. I commented out my ‘get-decision’ function to take that out of the mix to see if that caused anything. What the heck is happening here? I’ve been at this problem for a week now.

 

#temp variables for testing
$user = uuuuuuu
$pwvsan = pwpwpwpw
$vserver = ip of vcenter

#Connect to vCenter
Set-PowerCLIConfiguration -InvalidCertificateAction ignore -confirm:$false > $null
connect-viserver -server $vServer -user $user -password  $pwvsan > $null

function get-decision {
  $EntRet = Read-Host "Press ENTER to return to the menu"
  if ($EntRet.length -eq "") {
    show-menu
  } else {
  get-decision}
}

function show-menu 
{
  $menu=@"
1 List VM Hosts
2 Power on host VMs
Q Quit
 
Select a task by number or Q to quit
"@
  cls
  start-sleep -m 250
  write-host
  Write-Host "*** VM Utility Menu ***" -ForegroundColor Cyan
  write-host
  write-host "You have selected vCenter Server: " $vserver -ForegroundColor Green 
  write-host "You have selected host: " $vhost -ForegroundColor Green 
  write-host
  
  $sel = Read-Host $menu
  
  Switch ($sel) {
    "1" {
      write-host
      Write-Host "List Host VM's..." -ForegroundColor Green
      Write-Host "View vCenter Hosts and statistics on" $Vserver -ForegroundColor Green
      $VChostsStat = get-vmhost
      $VChostsStat
      start-sleep -s 3
      #get-decision
      show-menu
    }
    "2" {
      write-host
      Write-Host "Power on host VMs..." -ForegroundColor Green
      #insert your code here
      }
      "Q" {
      Write-Host "Quitting!" -ForegroundColor Green
    }
    default {
      Write-Host "I don't understand what you want to do." -ForegroundColor Yellow
    }
  }
}
show-menu

If you want to show your menu more than once - meaning to come back to the menu when you already did a choice once - you will need a loop.

I used to use something like this. It even avoids error input as it ignores everything else than the predefined input.

while ($answer -ne 2) {
    Clear-Host
    $List = new-Object System.Management.Automation.Host.ChoiceDescription "&List VM Hosts", "List";
    $Power = new-Object System.Management.Automation.Host.ChoiceDescription "&Power on host VMs", "Power";
    $Quit = new-Object System.Management.Automation.Host.ChoiceDescription "&Quit", "Quit";
    $choices = [System.Management.Automation.Host.ChoiceDescription[]]($List, $Power, $Quit);
    $answer = $host.ui.PromptForChoice($caption, $message, $choices, 0)
    $answer

    Switch ($answer) {
        1 {
            Write-Host "You choose 'Power on host VMs...'" -ForegroundColor Green
            Start-Sleep -Seconds 3
            #insert your code here
            break 
        }
        2 {
            Write-Host "You choose to quit ... exit" -ForegroundColor Green
            Start-Sleep -Seconds 3
            #insert your code here
            break 
        }
        default {
            Write-Host "You choose the default option: 'List VM Hosts'" -ForegroundColor Yellow
            Start-Sleep -Seconds 3
            #insert your code here
            break 
        }
    }
}

OLaf-

Thank you, I will investigate your code (not an expert here).

 

…AR

Olaf-

Could you tell me what the new-object line components are? I understand the the & uses the next letter as the default. Also what do the

last values in quotes do for me (“List”, “Power”, “Quit”, etc). If it works that’s an alternative. What I was working on was a format like this:

*** VM Utility Menu ***

You have selected vCenter Server : x.x.x.x

You have selected host : x.x.x.x

1 View vCenter Hosts & Statistics

2 Shut down host vm’s

3 Power on host VM’s

4 Display VM guest statistics

5 Display VM snapshots

6 Display Datastore statistics

7 Change selected host to process

8 Backup Host and vSwitch configurations

Q Quit

Select a task by number or Q to quit:

 

I googled a little bit for you:

https://docs.microsoft.com/en-us/dotnet/api/system.management.automation.host.choicedescription?view=powershellsdk-7.0.0

https://stackoverflow.com/questions/40642351/powershell-let-user-choice-from-list-of-january-through-december

Okay, I figured out a way to make this work like I wanted. Whenever using the powercli cmdlets in the script after the first run it seems powershell loses the formatting. So after I do a ‘get-vmxxxxx’ command I then pipe to ‘Select’ , choose my elements to display and then pipe to ‘format-table’ or ft. This fixed all my problem areas!

Thank you for your help.

 

…ar