VMware Cloud Community
emcclure
Enthusiast
Enthusiast
Jump to solution

Is it possible to have a number selection instead of typing out a response when doing Get-<whatever>?

I know I'm wording the title poorly.  But I'd like to know if it's possible when you do Get-Folder -Type VM (or any Get- command) and when the results come up it has a selectable number by the folder so a user would just type that instead of spelling out the folder.  If so is it also possible to error check it, so if there's only 5 options and someone selects 6 it will tell them to try again? 

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

It's a scope thing, when you define, or initialise a variable in a code block, the variable is not known outside that code block.
This goes for $entities as well as for $answer.

Try like this

$commands = {

   1..($entities.Count) | %{

   Write-Host "$_ - $($entities[$_ - 1].Name)"

   }

   $answer = 0

   while (1..($entities.Count) -notcontains $answer){

   $answer = Read-Host -Prompt "Select a folder (1-$($entities.Count))"

   }

 

   #Write-Host "You selected $($entities[$answer - 1].Name)"

   #Stuff below will ask you a question on if you want to continue or exit the script, depending on your answer it will loop back to the top of $commands or will disconnect from vCenter and exit the script

   $continue = New-Object System.Management.Automation.Host.ChoiceDescription '&continue', 'Continue with selection'

   $exit = New-Object System.Management.Automation.Host.ChoiceDescription '&return', 'return to top'

   $options = [System.Management.Automation.Host.ChoiceDescription[]]($continue, $exit)

   $title = 'Continue with selection or return to top?'

   $message = 'Do you want to continue with the current folder selection or make a different selection'

   $result = $host.ui.PromptForChoice($title, $message, $options, 0)

 

   if ($result -ne 0){

   &$commands

   }

   return $answer

}


$entities = Get-Folder -Type VM

$answer = &$commands


Get-VM -Location $entities[$answer - 1] | sort Name | Format-Table


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

View solution in original post

Reply
0 Kudos
12 Replies
LucD
Leadership
Leadership
Jump to solution

That is possible, but did you already have a look at Out-GridView?

$folder = Get-Folder -Type VM | Out-GridView -OutputMode Single -Title "Select a folder"

$folder


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

Reply
0 Kudos
emcclure
Enthusiast
Enthusiast
Jump to solution

Yes I've used that in a lot of scripts.  Not all of my scripts use that and I'm guessing if someone were to take one of my scripts and use it in a batch file it wouldn't work as well with a pop up window right?  I'm just trying to get it to where the scripts that have a lot of pop up windows are like that and ones that don't have that can just have the number selection.  Does that make sense?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

A batch script won't work with a prompt for a number either I think.


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

Reply
0 Kudos
emcclure
Enthusiast
Enthusiast
Jump to solution

Ok so still possible to do the way I asked?  Very convoluted?  Pain in the rear?  A lot of changes need to be made depending on what the output is?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I suspect this might be more what you had in mind?

$entities = Get-Folder -Type VM

1..($entities.Count) | %{

   Write-Host "$_ - $($entities[$_ - 1].Name)"

}

$answer = 0

while (1..($entities.Count) -notcontains $answer){

   $answer = Read-Host -Prompt "Select a folder (1-$($entities.Count))"

}

Write-Host "You selected $($entities[$answer - 1].Name)"


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

Reply
0 Kudos
emcclure
Enthusiast
Enthusiast
Jump to solution

Hi LucD,

Edit: So I figured out how to get it to return to the top, however I'd like to know how to exactly pass the info on.  So for example I have something like this:

$commands = {

$entities = Get-Folder -Type VM

1..($entities.Count) | %{

   Write-Host "$_ - $($entities[$_ - 1].Name)"

}

$answer = 0

while (1..($entities.Count) -notcontains $answer){

   $answer = Read-Host -Prompt "Select a folder (1-$($entities.Count))"

}

Write-Host "You selected $($entities[$answer - 1].Name)"

#Stuff below will ask you a question on if you want to continue or exit the script, depending on your answer it will loop back to the top of $commands or will disconnect from vCenter and exit the script

$continue = New-Object System.Management.Automation.Host.ChoiceDescription '&continue', 'Continue with selection'
$exit = New-Object System.Management.Automation.Host.ChoiceDescription '&return', 'return to top'

$options = [System.Management.Automation.Host.ChoiceDescription[]]($continue, $exit)

$title = 'Continue with selection or return to top?'
$message = 'Do you want to continue with the current folder selection or make a different selection'
$result = $host.ui.PromptForChoice($title, $message, $options, 0)

if ($result -eq "0"){
$moveon
  } else {
  &$commands
}
}

&$commands

$moveon = Get-Folder -Name $($entities[$answer - 1].Name) | Get-VM | sort Name | Format-Table

So I've tried a couple different things on the last line, I've used $answer, what you see above and $entities, but none pipe out the info from the folder.  So what do I put there to get the list of VM's in that folder?  Also wanted to know how to select multiple options if possible since a script may update multiple VM's, hosts, etc.

One last edit: I noticed when I run this and select a certain folder and I hit C to continue it shows me VM's that aren't in the folder I selected.  In fact it shows the same 2 VM's from whatever folder I select and none of the folders I select hold those VMs.  Rather odd and not sure why that's happening.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

It's a scope thing, when you define, or initialise a variable in a code block, the variable is not known outside that code block.
This goes for $entities as well as for $answer.

Try like this

$commands = {

   1..($entities.Count) | %{

   Write-Host "$_ - $($entities[$_ - 1].Name)"

   }

   $answer = 0

   while (1..($entities.Count) -notcontains $answer){

   $answer = Read-Host -Prompt "Select a folder (1-$($entities.Count))"

   }

 

   #Write-Host "You selected $($entities[$answer - 1].Name)"

   #Stuff below will ask you a question on if you want to continue or exit the script, depending on your answer it will loop back to the top of $commands or will disconnect from vCenter and exit the script

   $continue = New-Object System.Management.Automation.Host.ChoiceDescription '&continue', 'Continue with selection'

   $exit = New-Object System.Management.Automation.Host.ChoiceDescription '&return', 'return to top'

   $options = [System.Management.Automation.Host.ChoiceDescription[]]($continue, $exit)

   $title = 'Continue with selection or return to top?'

   $message = 'Do you want to continue with the current folder selection or make a different selection'

   $result = $host.ui.PromptForChoice($title, $message, $options, 0)

 

   if ($result -ne 0){

   &$commands

   }

   return $answer

}


$entities = Get-Folder -Type VM

$answer = &$commands


Get-VM -Location $entities[$answer - 1] | sort Name | Format-Table


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

Reply
0 Kudos
emcclure
Enthusiast
Enthusiast
Jump to solution

Hi LucD,

That works.  I'd also like to get it so the next thing that pops up would also have the number option and so forth.  Is that just as simple as reusing the same code and just making a couple changes, or is there a different way?  What if I wanted to select multiple options?  How would that work?  It appears the code as it is only allows one, but if I got to a point where I wanted to select multiple how would that be accomplished?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I would suggest to look at Out-GridView again, especially the OutputMode parameter that allows Single and Multiple.

Why reinvent the wheel?


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

Reply
0 Kudos
emcclure
Enthusiast
Enthusiast
Jump to solution

Hi LucD,

Oh I understand what you're saying.  Sometimes we work with people who don't clearly understand that, or have some other program that they want to use and they can't/won't/don't want to use a pop up window.  I was thinking about something on the Out-GridView though or maybe something similar to it.  Let's say I'm exporting an OVF somewhere.  With a script I have I can just drag the path to the PowerCLI window, however is it possible to open a window that allows a user to browse to a location instead of typing it in or opening it elsewhere and then copying it into the PowerCLI window?  Can that be done with Out-GridView?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

No, not with Out-GridView, that only displays entries from an array you feed into it.

For a real file selection option, you will need to use a Windows Forms FileSelector.

Something like this for example (you probably have to adapt the value assigned to the $initialDirectory variable)

$initialDirectory = 'D:\Repository\VMware'

[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null

$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog

$OpenFileDialog.initialDirectory = $initialDirectory

$OpenFileDialog.filter = "*.ova"

$OpenFileDialog.ShowDialog() | Out-Null

$OpenFileDialog.filename


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

Reply
0 Kudos
emcclure
Enthusiast
Enthusiast
Jump to solution

Hi LucD,

That works great.  I had to make one change as the .filter line was not liked.  It threw this error:

Exception setting "filter": "Filter string you provided is not valid. The filter string must contain a description of

the filter, followed by the vertical bar (|) and the filter pattern. The strings for different filtering options must

also be separated by the vertical bar. Example: "Text files (*.txt)|*.txt|All files (*.*)|*.*""

So once I put in something like this: $OpenFileDialog.filter = "ISO files (*.iso)|*.iso|All files (*.*)|*.*"""

I was good to go.

Thanks for your help.

Reply
0 Kudos