VMware Cloud Community
bvi1006
Enthusiast
Enthusiast
Jump to solution

Creating a menu in Powershell after gathering the ESX host names

Hi,

I give up Smiley Happy .

I found this script which provides a menu in Powershell (no popups). I get all of my hosts, then run the function(s).

At the end of the script after I have chose the one I want,  I am trying to find/set a variable so that I can then use the host name for my next command.

I cannot figure out how to do this, as I do not understand what happens to my choice (which I think is in the variable $choice), but $choice ends up empty when I check it manually afterward.

Can someone please help me? At the bottom you will see my gathering of the hosts and calling of the function.

Thanks!

function Write-Choice {

  param($prompt, $choice)

  write-host -n "["

  write-host -n -f yellow $prompt

  write-host "]", $choice

  Write-Output $prompt

}

function Get-Selection {

  param(

  [Parameter(Mandatory=$true,ValueFromPipeline=$true,Position=0,HelpMessage="Choices")]

  [Object]

  $choice,

  [Parameter(HelpMessage="Intro message")]

  [string]

  $introMessage="Choose from the following options:",

  [Parameter(HelpMessage="Prompt message")]

  [string]

  $prompt="Select an option",

  [Parameter(HelpMessage="Window size")]

  [Int]

  $windowSize=10

  )

  $currentWindow = 0

  $maxWindow = [Math]::Floor($choice.length / $windowSize)

  while ($true) {

  Clear-Host

  $start = $currentWindow*$windowSize

  $thisWindowSize = [Math]::Min($windowSize, $choice.length - $start)

  if ($introMessage) {

  write-host -fore yellow $introMessage

  }

  $choices = @()

  for ($i = 1; $i -lt $thisWindowSize + 1; $i++) {

  $choices += Write-Choice $i $choice[$start + $i - 1]

  }

  if ($currentWindow -gt 0) {

  $choices += Write-Choice "P" "Previous Page Of Choices"

  }

  if ($currentWindow -lt $maxWindow) {

  $choices += Write-Choice "N" "Next Page Of Choices"

  }

  while ($true) {

  $input = read-host $prompt

  if ($choices -notcontains $input) {

  continue

  }

  if ($input -eq "N") {

  $currentWindow++

  break

  } elseif ($input -eq "P") {

  $currentWindow--

  break

  } else {

  Write-Output $choice[$start + [int]$input - 1]

  return

  }

  }

  }

}

# Get the hosts

$choosehost = get-vmhost | get-view | select name

# Call the function

Get-Selection $choosehost

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try like this

$selected = Get-Selection (Get-VMHost | Select -ExpandProperty Name)

In $selected you now the name of the slected ESXi host.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

Try like this

$selected = Get-Selection (Get-VMHost | Select -ExpandProperty Name)

In $selected you now the name of the slected ESXi host.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
bvi1006
Enthusiast
Enthusiast
Jump to solution

oh man, banged my head over that one for hours. Thanks sooooooooooo much Smiley Happy

0 Kudos