Combobox variable for memory set GB, MB, KB

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.
User avatar
obrienc
Posts: 59
Joined: Mon Dec 12, 2016 10:58 am

Combobox variable for memory set GB, MB, KB

Post by obrienc » Wed May 17, 2017 6:19 pm

I have a req to load a combobox so someone can choose memory for a VM from a dropdown.
  1. Load-ComboBox -ComboBox $combobox4 -Items (1 .. 8)
In Powershell this works but I want to turn it into a variable
  1. Set-VM -Name $a$_ -DynamicMemory -MemoryStartupBytes 4GB
  1. $vmem = $combobox4.SelectedItem
  2.  
  3.     $m = "GB"
I've tried a few different things $vmem"GB" $vmem$m
  1. Set-VM -ComputerName $s1 -Name $a$_ -DynamicMemory -MemoryStartupBytes $vmem+$m

User avatar
jvierra
Posts: 11696
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Combobox variable for memory set GB, MB, KB

Post by jvierra » Wed May 17, 2017 7:56 pm

JUst add an object to the combo that has the string 1Gb and the value 1Gb and use the value.

$items = 1..10 | %{ [pscustomobject]@{Name=$_;Value=$_ * 1Gb}}

User avatar
obrienc
Posts: 59
Joined: Mon Dec 12, 2016 10:58 am

Re: Combobox variable for memory set GB, MB, KB

Post by obrienc » Wed May 17, 2017 8:22 pm

thanks for the reply. it broke something so I'm not sure if I was to take your sample and use it or figure out how to use it but i am a little lost.

The dropdown displays this @{Name=1; Value=1073741824} instead of an integer 1..8

ERROR: Set-VM : Cannot bind parameter 'MemoryMinimumBytes'. Cannot convert value "@{Name=4; Value=4294967296}" to type "System.Nullable`1[System.Int64]". Error:
ERROR: "Cannot process argument because the value of argument "obj" is null. Change the value of argument "obj" to a non-null value."

I use $_ and $a to append VM names in a loop = $combobox1.SelectedItem
  1. foreach ($_ in 1 .. $a)
  2.  
  3.         {
  4.  
  5.             New-VM -Name $a$_ -ComputerName $s1 -SwitchName vSwitch0 -BootDevice CD -Generation 1
  6.  
  7.             Set-VM -ComputerName $s1 -Name $a$_ -DynamicMemory -MemoryMinimumBytes $vmem
  8.  
  9.             Set-VMProcessor -ComputerName $s1 -VMName $a$_ -Count $numproc -Reserve 10 -Maximum 100 -RelativeWeight 100

User avatar
jvierra
Posts: 11696
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Combobox variable for memory set GB, MB, KB

Post by jvierra » Wed May 17, 2017 8:28 pm

$combobox.Displayname = 'name'

User avatar
obrienc
Posts: 59
Joined: Mon Dec 12, 2016 10:58 am

Re: Combobox variable for memory set GB, MB, KB

Post by obrienc » Thu May 18, 2017 8:22 am

I get a $combobox1.DisplayMember as a choice not a DisplayName. The dropdown doesn't display just an integer correctly. here is the form.
Attachments
TestForm.psf
(40.27 KiB) Downloaded 17 times

User avatar
jvierra
Posts: 11696
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Combobox variable for memory set GB, MB, KB

Post by jvierra » Thu May 18, 2017 9:22 am

  1. $items = 1..10 |
  2.  
  3.         ForEach-Object{
  4.  
  5.             [pscustomobject]@{
  6.  
  7.                 Name = '{0} Gb' -f $_
  8.  
  9.                 Value = $_ * 1Gb
  10.  
  11.             }
  12.  
  13.         }
  14.  
  15.     $combobox4.DataSource = [System.Collections.ArrayList]$items
  16.  
  17.     $combobox4.DisplayMember = 'Name'

User avatar
jvierra
Posts: 11696
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Combobox variable for memory set GB, MB, KB

Post by jvierra » Thu May 18, 2017 9:26 am

You can also do it like this:
  1. $items = 1..10 |
  2.  
  3.         ForEach-Object{
  4.  
  5.             [pscustomobject]@{
  6.  
  7.                 Name = "$_`Gb"
  8.  
  9.                 Value = $_ * 1Gb
  10.  
  11.             }
  12.  
  13.         }

User avatar
obrienc
Posts: 59
Joined: Mon Dec 12, 2016 10:58 am

Re: Combobox variable for memory set GB, MB, KB

Post by obrienc » Thu May 18, 2017 9:38 am

Thank you. Should I omit
  1. $vmem = $combobox4.SelectedItem
?

I still get an error that complains about ERROR: A parameter that is not valid was passed to the operation. I used $vmem as the variable

here
  1. Set-VM -ComputerName $s1 -Name $a$_ -DynamicMemory -MemoryMinimumBytes $vmem

User avatar
jvierra
Posts: 11696
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Combobox variable for memory set GB, MB, KB

Post by jvierra » Thu May 18, 2017 9:48 am

$vmem = $combobox4.SelectedItem.Value

User avatar
obrienc
Posts: 59
Joined: Mon Dec 12, 2016 10:58 am

Re: Combobox variable for memory set GB, MB, KB

Post by obrienc » Thu May 18, 2017 1:42 pm

It showed null. Set-VM : Cannot validate argument on parameter 'MemoryMinimumBytes'. The argument is null. Provide a valid value for the argument, and then try running the

MainForm.psf (39, 75): ERROR: At Line: 39 char: 75
ERROR: + ... -ComputerName $s1 -Name $a$_ -DynamicMemory -MemoryMinimumBytes $vmem

I tried
  1. $vmem = $combobox4.SelectedValue

Locked