Assign Inputlist options a value

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Dale Dale 4 weeks ago.

  • Author
    Posts
  • #56785
    Profile photo of Dale
    Dale
    Participant

    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?

    Thanks.

    ### 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")
    {$x=$objListBox.SelectedItem;$objForm.Close()}})
    $objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
    {$objForm.Close()}})

    $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"
    $OKButton.Add_Click({$x=$objListBox.SelectedItem;$objForm.Close()})
    $objForm.Controls.Add($OKButton)

    $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"
    $CancelButton.Add_Click({$objForm.Close()})
    $objForm.Controls.Add($CancelButton)

    $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:"
    $objForm.Controls.Add($objLabel)

    $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.Controls.Add($objListBox)

    $objForm.Topmost = $True

    $objForm.Add_Shown({$objForm.Activate()})
    [void] $objForm.ShowDialog()

    $x

    ###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"

  • #56812
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    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:

    $OKButton.Add_Click({
        #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"    
        #$objForm.Close()
    })
    

    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.

    • #56857
      Profile photo of Dale
      Dale
      Participant

      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.

      Thanks.

You must be logged in to reply to this topic.