Assigning Variables to Items in Combo Box

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.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
bzowky
Posts: 7
Joined: Mon Sep 30, 2013 1:33 pm

Assigning Variables to Items in Combo Box

Post by bzowky » Mon Sep 30, 2013 3:21 pm

Good Afternoon All -

I'm trying to create a GUI which will allow techs to deploy applications via SCCM 2012 without using the console. I'm ~90% there, but just need to figure something out. Even though I have much experience with SCCM, I don't as much with Powershell so apologies if this is a newb question :).

My GUI consists of 3 separate things.
1. Enter a hostname (textbox)
2. Select an Application (Combobox with ~12 items)
3. Click "Deploy" (button)

This is what happens on the backend:
When the user clicks the "Deploy" button, the function below is run:

Code: Select all

Set-Location A00:
Add-CMDeviceCollectionDirectMembershipRule -CollectionId $CollectionID -ResourceId $(get-cmdevice -name $txtComputerName).ResourceID
The function uses the hostname entered in step one via variable $txtComputerName, as well as the variable $CollectionID which is a list of collections named after applications - with the same application it's named for pre-advertised to it. A collection ID format example is A0000035.

The issue I'm having is that I can't get the variables set for whichever combobox item is selected. Every time I try then test the script, I get: "Add-CMDeviceCollectionDirectMembershipRule : Cannot validate argument on parameter 'CollectionId'. The argument is null or empty. Supply an argument that
is not null or empty and then try the command again."

I first tried radio buttons (1 per application) and used the code below, but it didn't work either as I got the same error above - but - I like the combo box better anyways.

Code: Select all

If ($radioButton1.checked) {
$CollectionID="A0000035"
}
elseif ($radioButton2.checked)
{
$CollectionID="A0000034"
}
Other Minor Questions
- The script would rely on ConfigurationManager.ps1 (cmdlet) to be imported before it worked per machine. Can that be added to occur during launch of the app? The file isn't local to most machines this GUI will be used on but can be copied if need be. It is on the same server as the A00: drive, thoug, if that makes a difference.
- The command above is really two commands - the first to change the drive letter to A00: and the second to add the device. Is that the best way to do it or no?

Thanks for your help!

User avatar
davidc
Posts: 5913
Joined: Thu Aug 18, 2011 4:56 am

Re: Assigning Variables to Items in Combo Box

Post by davidc » Mon Sep 30, 2013 4:48 pm

You need the use the textbox's Text property:
PowerShell Code
Double-click the code block to select all.
$txtComputerName.Text
These articles may be of assistance:

http://www.sapien.com/blog/2011/06/13/p ... x-control/

http://www.sapien.com/blog/2011/06/21/p ... x-control/

David
David
SAPIEN Technologies, Inc.

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

Re: Assigning Variables to Items in Combo Box

Post by jvierra » Mon Sep 30, 2013 4:55 pm

David lost the $ in the example

$txtComputerName.Text

YOU can also assign values to radio buttons and read them back on an event. This allows us to make updates to only the UI and the code will pick up the correct value.

User avatar
davidc
Posts: 5913
Joined: Thu Aug 18, 2011 4:56 am

Re: Assigning Variables to Items in Combo Box

Post by davidc » Mon Sep 30, 2013 5:01 pm

Thank you for catching that.

David
David
SAPIEN Technologies, Inc.

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

Re: Assigning Variables to Items in Combo Box

Post by jvierra » Mon Sep 30, 2013 5:12 pm

Here is a simple demo of how radio buttons can be used.

Attached
Attachments
DEmo-RadioButtons.pff.txt
Remove TXT extension- it is a PFF file
(8.26 KiB) Downloaded 209 times

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

Re: Assigning Variables to Items in Combo Box

Post by jvierra » Mon Sep 30, 2013 5:14 pm

davidc wrote:Thank you for catching that.

David
Don't you ever go home? It is getting very late - you are getting very sleepy. Sleep, sleep, sleep.

Locked