Support Forums

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: 24
Joined: Mon Dec 12, 2016 10:58 am

Combobox variable for memory set GB, MB, KB

Postby 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.
Load-ComboBox -ComboBox $combobox4 -Items (1 .. 8)


In Powershell this works but I want to turn it into a variable
Set-VM -Name $a$_ -DynamicMemory -MemoryStartupBytes 4GB


$vmem = $combobox4.SelectedItem
$m = "GB"


I've tried a few different things $vmem"GB" $vmem$m
Set-VM -ComputerName $s1 -Name $a$_ -DynamicMemory -MemoryStartupBytes $vmem+$m
User avatar
jvierra
Posts: 10816
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Postby 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: 24
Joined: Mon Dec 12, 2016 10:58 am

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

Postby 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
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: 10816
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Postby jvierra » Wed May 17, 2017 8:28 pm

$combobox.Displayname = 'name'
User avatar
obrienc
Posts: 24
Joined: Mon Dec 12, 2016 10:58 am

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

Postby 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 3 times
User avatar
jvierra
Posts: 10816
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Postby jvierra » Thu May 18, 2017 9:22 am

$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: 10816
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Postby jvierra » Thu May 18, 2017 9:26 am

You can also do it like this:
$items = 1..10 | 
ForEach-Object{
[pscustomobject]@{
Name = "$_`Gb"
Value = $_ * 1Gb
}
}
User avatar
obrienc
Posts: 24
Joined: Mon Dec 12, 2016 10:58 am

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

Postby obrienc » Thu May 18, 2017 9:38 am

Thank you. Should I omit
$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
Set-VM -ComputerName $s1 -Name $a$_ -DynamicMemory -MemoryMinimumBytes $vmem
User avatar
jvierra
Posts: 10816
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Postby jvierra » Thu May 18, 2017 9:48 am

$vmem = $combobox4.SelectedItem.Value
User avatar
obrienc
Posts: 24
Joined: Mon Dec 12, 2016 10:58 am

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

Postby 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
$vmem = $combobox4.SelectedValue

Return to “PowerShell GUIs”

Who is online

Users browsing this forum: No registered users and 1 guest

cron