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.

Code: Select all

Load-ComboBox -ComboBox $combobox4 -Items (1 .. 8)
In Powershell this works but I want to turn it into a variable

Code: Select all

Set-VM -Name $a$_ -DynamicMemory -MemoryStartupBytes 4GB

Code: Select all

$vmem = $combobox4.SelectedItem
$m = "GB"
I've tried a few different things $vmem"GB" $vmem$m

Code: Select all

Set-VM -ComputerName $s1 -Name $a$_ -DynamicMemory -MemoryStartupBytes $vmem+$m

User avatar
jvierra
Posts: 11270
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

Code: Select all

foreach ($_ in 1 .. $a)
{
New-VM -Name $a$_ -ComputerName $s1 -SwitchName vSwitch0 -BootDevice CD -Generation 1
Set-VM -ComputerName $s1 -Name $a$_ -DynamicMemory -MemoryMinimumBytes $vmem
Set-VMProcessor -ComputerName $s1 -VMName $a$_ -Count $numproc -Reserve 10 -Maximum 100 -RelativeWeight 100

User avatar
jvierra
Posts: 11270
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 8 times

User avatar
jvierra
Posts: 11270
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

Code: Select all

$items = 1..10 |
ForEach-Object{
[pscustomobject]@{
Name = '{0} Gb' -f $_
Value = $_ * 1Gb
}
}
$combobox4.DataSource = [System.Collections.ArrayList]$items
$combobox4.DisplayMember = 'Name'

User avatar
jvierra
Posts: 11270
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:

Code: Select all

$items = 1..10 |
ForEach-Object{
[pscustomobject]@{
Name = "$_`Gb"
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 » Thu May 18, 2017 9:38 am

Thank you. Should I omit

Code: Select all

$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

Code: Select all

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

User avatar
jvierra
Posts: 11270
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

Code: Select all

$vmem = $combobox4.SelectedValue

Locked