Forum Discussion

KickAss1996's avatar
KickAss1996
Copper Contributor
May 14, 2020

Run sub command under other line

Hello to everybody!

I need some help with ps script that i'm writing these days.

in my script i created a list and when the script start to run the list showen and you can choose from it.

my question is, how can i run a specific command that when I select one of the objects in the list the command effect only on the selection and not all over the list\script?

the goal at my team is to remove\wipe data on cellphones (they had carrier static IP).

for example this is the list:

[void] $listBox.Items.Add('Phone 21')
[void] $listBox.Items.Add('Phone 22')
[void] $listBox.Items.Add('Phone 23')
[void] $listBox.Items.Add('Phone 24')
[void] $listBox.Items.Add('Phone 25')
[void] $listBox.Items.Add('Phone 26')
[void] $listBox.Items.Add('Phone 31')
[void] $listBox.Items.Add('Phone 32')
[void] $listBox.Items.Add('Phone 33')
[void] $listBox.Items.Add('Phone 34')
[void] $listBox.Items.Add('Phone 35')
[void] $listBox.Items.Add('Phone 36')
[void] $listBox.Items.Add('Phone 41')
[void] $listBox.Items.Add('Phone 42')

Thank you for help!

  • Manfred101's avatar
    Manfred101
    Iron Contributor

    KickAss1996  Ckeck out the snippet below, just edit the $ScriptToExecute to whatever you want to do!

    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")  
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 
    [void] [System.Windows.Forms.Application]::EnableVisualStyles() 
    
    $Form = New-Object system.Windows.Forms.Form 
    $Form.Size = New-Object System.Drawing.Size(400,200) 
    $Form.Width = 400 
    $Form.Height = 500 
    $form.MaximizeBox = $false 
    $Form.StartPosition = "CenterScreen" 
    $Form.FormBorderStyle = 'Fixed3D' 
    $Form.Text = "My Application" 
    
    $listBox = New-Object System.Windows.Forms.ListBox
    $listBox.Location = New-Object System.Drawing.Point(10,40)
    $listBox.Size = New-Object System.Drawing.Size(260,20)
    $listBox.Height = 80
    
    [void] $listBox.Items.Add('Phone 10')
    [void] $listBox.Items.Add('Phone 11')
    [void] $listBox.Items.Add('Phone 12')
    [void] $listBox.Items.Add('Phone 13')
    [void] $listBox.Items.Add('Phone 14')
    [void] $listBox.Items.Add('Phone 15')
    [void] $listBox.Items.Add('Phone 16')
    
    $form.Controls.Add($listBox)
     
    $ScriptToExecute = {
        [System.Windows.MessageBox]::Show($listBox.SelectedItem)
    } 
    $Okbutton = New-Object System.Windows.Forms.Button 
    $Okbutton.Location = New-Object System.Drawing.Size(140,200) 
    $Okbutton.Size = New-Object System.Drawing.Size(120,30) 
    $Okbutton.Text = "Ok" 
    $Okbutton.Add_Click($ScriptToExecute) 
    
    $Form.Controls.Add($Okbutton)  
    $Form.ShowDialog() 

     

    Goodluck!

     

    Grtz, Manfred de Laat

  • Manfred101's avatar
    Manfred101
    Iron Contributor

    KickAss1996I don't know exactly what you mean but $listBox.SelectedItem will contain the value of the selected item.  Hope it helps!

     

    Grtz, Manfred de Laat

    • KickAss1996's avatar
      KickAss1996
      Copper Contributor

      Hi thank you for replayManfred101 i mean after i select something from the list the command of stop-computer for example will running only for i marked 

      • Manfred101's avatar
        Manfred101
        Iron Contributor

        KickAss1996 

        First: I don’t have a clue what you try to achieve. On your form there is one list whit multiple items and two buttons  If you select for example “Phone 21” and then “OK”. You want to run a command Stop-computer…? 

         

        Second: Working whit Windows Forms is so much fun! But it can be challenging as well. I used Powershell studio in the past for making my forms. This is a more visual approach and gives you great insights on how you can use all Windows form elements, what their properties are, what methods to use etc. They have a 45 day trail and if you use it alongside your scripts it will help you a lot in getting started whit Windows Forms.

Resources