Assign Inputlist options a value

Hello Guys,

PS newbie here. In the below code i am trying to assign the 4 list options a value (computername), so that i can select an option (which holds a value of the computername assigned) to use for the command at the bottom.

Although i don’t know how to actually make the list options hold any value, anyone lend a hand?


Load VB Text Window Framework and Prompt for Hostname###

[System.Reflection.Assembly]::LoadWithPartialName(‘Microsoft.VisualBasic’) | Out-Null
[void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)
[void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Drawing”)

$Computer =

$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = “Select a Computer”
$objForm.Size = New-Object System.Drawing.Size(300,200)
$objForm.StartPosition = “CenterScreen”

$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($.KeyCode -eq “Enter”)
$objForm.Add_KeyDown({if ($
.KeyCode -eq “Escape”)

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = “OK”

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = “Cancel”

$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,20)
$objLabel.Size = New-Object System.Drawing.Size(280,20)
$objLabel.Text = “Please select a computer:”

$objListBox = New-Object System.Windows.Forms.ListBox
$objListBox.Location = New-Object System.Drawing.Size(10,40)
$objListBox.Size = New-Object System.Drawing.Size(260,20)
$objListBox.Height = 80

[void] $objListBox.Items.Add(“Shannon”)
[void] $objListBox.Items.Add(“Dale”)
[void] $objListBox.Items.Add(“Ryan”)
[void] $objListBox.Items.Add(“Daniel”)


$objForm.Topmost = $True

[void] $objForm.ShowDialog()


###Enter message to send to Host and transmit

$Message = [Microsoft.VisualBasic.Interaction]::InputBox(“Enter Message”, “Message to send”, “$env:String”)

Invoke-WmiMethod -Class Win32_Process -ComputerName $Computer -Name Create -ArgumentList “C:\Windows\System32\msg.exe * $Message”

A GUI is a tough place to start as a newbie IMHO. It’s possible, but much harder to write a GUI in notepad or ISE. Sapien Powershell Studio has a WYSIWYG interface to drag and drop components and generate code. To answer your question, when you’re doing something in a .NET (or almost any) GUI, events and generated based on action. _onClick is typically the event that needs to be wired to OK to run the WMI command.

Create the dropdown:
$objListBox = New-Object System.Windows.Forms.ListBox
$objListBox.Location = New-Object System.Drawing.Size(10,40)
$objListBox.Size = New-Object System.Drawing.Size(260,20)
$objListBox.Height = 80

Fill the dropdown. The form has an _onLoad event where you should do this so that when the form is loaded that dropdown in populated.

[void] $objListBox.Items.Add("Computer123")

Wire up the event. Execute this script block when you click the button:

    #This should be a text field in the GUI, not a seperate inputbox
    #$Message = [Microsoft.VisualBasic.Interaction]::InputBox("Enter Message", "Message to send", "$env:String")

    Invoke-WmiMethod -Class Win32_Process -ComputerName $objListBox.SelectedItem -Name Create -ArgumentList "C:\Windows\System32\msg.exe *  $Message"    

Now, I don’t know why you’re trying to send messages to remote computers. There are better ways then trying to do a remote msg like a ballon popup or something. There are ways to use Visual Studio or even easier ways like Sapien Powershell Studio to generate GUI’s that automatically add events and have helper functions for filling dropdowns.

Hello Rob,

Thank you for the information, sorry i may have missed this out in the description, although i would like the list to have a display name (like my name for example), and then when that option is chosen, Powershell will see this as my computer’s hostname (which is the part i do not know how to do.

Excuse my ignorance if i am reading your response wrong.