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

**URL:** <https://forums.powershell.org/t/after-first-run-code-doesnt-return-same-information-take-2/14544>\
**Category:** PowerShell Help\
**Created:** [June 17, 2020, 2:38am UTC](https://forums.powershell.org/t/after-first-run-code-doesnt-return-same-information-take-2/14544 "2020-06-17T02:38:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![araczek](https://avatars.discourse-cdn.com/v4/letter/a/e5b9ba/32.png) [@araczek](https://forums.powershell.org/u/araczek)\
**Post date:** [June 17, 2020, 2:38am UTC](https://forums.powershell.org/t/after-first-run-code-doesnt-return-same-information-take-2/14544/1 "2020-06-17T02:38:06Z")

</div>

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.

&nbsp;

```
#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
```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [June 17, 2020, 7:03am UTC](https://forums.powershell.org/t/after-first-run-code-doesnt-return-same-information-take-2/14544/2 "2020-06-17T07:03:15Z")

</div>

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 
        }
    }
}
```

---

<div class="post-metadata">

**Author:** ![araczek](https://avatars.discourse-cdn.com/v4/letter/a/e5b9ba/32.png) [@araczek](https://forums.powershell.org/u/araczek)\
**Post date:** [June 17, 2020, 7:30am UTC](https://forums.powershell.org/t/after-first-run-code-doesnt-return-same-information-take-2/14544/3 "2020-06-17T07:30:40Z")

</div>

OLaf-

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

&nbsp;

…AR

---

<div class="post-metadata">

**Author:** ![araczek](https://avatars.discourse-cdn.com/v4/letter/a/e5b9ba/32.png) [@araczek](https://forums.powershell.org/u/araczek)\
**Post date:** [June 17, 2020, 9:36am UTC](https://forums.powershell.org/t/after-first-run-code-doesnt-return-same-information-take-2/14544/4 "2020-06-17T09:36:56Z")

</div>

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:

&nbsp;

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [June 17, 2020, 1:54pm UTC](https://forums.powershell.org/t/after-first-run-code-doesnt-return-same-information-take-2/14544/5 "2020-06-17T13:54:25Z")

</div>

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://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](https://stackoverflow.com/questions/40642351/powershell-let-user-choice-from-list-of-january-through-december)

---

<div class="post-metadata">

**Author:** ![araczek](https://avatars.discourse-cdn.com/v4/letter/a/e5b9ba/32.png) [@araczek](https://forums.powershell.org/u/araczek)\
**Post date:** [June 18, 2020, 7:46am UTC](https://forums.powershell.org/t/after-first-run-code-doesnt-return-same-information-take-2/14544/6 "2020-06-18T07:46:08Z")

</div>

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.

&nbsp;

…ar

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:30pm UTC](https://forums.powershell.org/t/after-first-run-code-doesnt-return-same-information-take-2/14544/7 "2024-05-16T20:30:05Z")

</div>

Closed on May 16, 2024, 8:30pm UTC


