Run sub command under other line

Copper Contributor

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!

6 Replies

@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

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

@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.

@Manfred101 the stop-computer was an example, i mean that's my goal is to run a command only on one item from the list do not matter which item i pick.

if this is my list

phone 1

2

3

3...

i want when i click phone 1 the command run only for phone 1 

and if i click on phone 3 the command run only for the phone 3

thank you!

@KickAss1996 The output in the code snippet below shows you a messagebox but you do whatever you want. Just create your own $ScriptToExecute scriptblock!

 

Goodluck!

Grtz, Manfred de Laat

 

 

[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() 

 

 

 

@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