VMware Cloud Community
roneng
Enthusiast
Enthusiast

How to insert drop down menu in a powershell action?

Hi

I am trying to write a script that will deploy a VM from template.

It works just fine if i do it with constants or with Read-Host command.

But i would like to have a drop down menu for selecting the data stores, and here i got confused.

Does anybody know how to do that?

Thanks

0 Kudos
8 Replies
LucD
Leadership
Leadership

There are no native PowerShell cmdlets to give you dropdown menus.

But it's not too difficult to use Windows Forms from your PS scripts.

You can use something like PrimalForms from Sapien, and they also offer a free community version, to visually develop your forms and PrimalForms will create the PowerShell code for you.

A somewhat elaborate example of what you can do with forms is my GPS application.


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

0 Kudos
roneng
Enthusiast
Enthusiast

Ok

I managed to populate the drop down, and it holds the fields that i need, but i cant find a way to store the selected item in a variable.

Its always blank.

Is there a special variable for that , or some kind of a method?

0 Kudos
LucD
Leadership
Leadership

Could you perhaps post part of your code ?

Would make it a bit easier to check Smiley Wink


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

0 Kudos
roneng
Enthusiast
Enthusiast

Ok, attached.

I used some other people scripts in that

Thanks

0 Kudos
LucD
Leadership
Leadership

You can use

$comboBox1.SelectedItem

or

$comboBox1.SelectedIndex

to get the option that was selected.


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

0 Kudos
roneng
Enthusiast
Enthusiast

Thanks,

Tried it, still empty.

I guess something is really bad in my code.

0 Kudos
LucD
Leadership
Leadership

You have placed the GenerateForm function inside your main script.

Perhaps you can make things clearer with a layout like this

function GenerateForm{
...
}
if ($global:defaultviservers) {
...
   GenerateForm
   $store = $comboBox1.SelectedItem
   New-VM ...
   sleep 10
   Start-Vm ...
}
else{
   [http://System.Windows.Forms.MessageBox|http://System.Windows.Forms.MessageBox]::Show(...
}

Notice how I pick up the selected datastore name after the call to the GenerateForm function.


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

0 Kudos
roneng
Enthusiast
Enthusiast

ok, thanks

My problem was the context of the variables.

When i organized as you wrote it worked.

Thanks

0 Kudos