Run sub command under other line

%3CLINGO-SUB%20id%3D%22lingo-sub-1390474%22%20slang%3D%22en-US%22%3ERun%20sub%20command%20under%20other%20line%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1390474%22%20slang%3D%22en-US%22%3E%3CP%3EHello%20to%20everybody!%3C%2FP%3E%3CP%3EI%20need%20some%20help%20with%20ps%20script%20that%20i%3CSPAN%3E'm%20writing%20these%20days.%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3Ein%20my%20script%20i%20created%20a%20list%20and%20when%20the%20script%20start%20to%20run%20the%20list%20showen%20and%20you%20can%20choose%20from%20it.%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3Emy%26nbsp%3Bquestion%20is%2C%20how%20can%20i%20run%20a%26nbsp%3Bspecific%20command%20that%20when%20I%20select%20one%20of%20the%20objects%20in%20the%20list%20the%20command%20effect%20only%20on%20the%20selection%20and%20not%20all%20over%20the%20list%5Cscript%3F%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3Ethe%20goal%20at%20my%20team%20is%20to%20remove%5Cwipe%20data%20on%20cellphones%20(they%20had%20carrier%20static%20IP).%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3Efor%20example%20t%3C%2FSPAN%3E%3CSPAN%3Ehis%20is%20the%20list%3A%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3E%5Bvoid%5D%20%24listBox.Items.Add('Phone%2021')%3CBR%20%2F%3E%5Bvoid%5D%20%24listBox.Items.Add('Phone%2022')%3CBR%20%2F%3E%5Bvoid%5D%20%24listBox.Items.Add('Phone%2023')%3CBR%20%2F%3E%5Bvoid%5D%20%24listBox.Items.Add('Phone%2024')%3CBR%20%2F%3E%5Bvoid%5D%20%24listBox.Items.Add('Phone%2025')%3CBR%20%2F%3E%5Bvoid%5D%20%24listBox.Items.Add('Phone%2026')%3CBR%20%2F%3E%5Bvoid%5D%20%24listBox.Items.Add('Phone%2031')%3CBR%20%2F%3E%5Bvoid%5D%20%24listBox.Items.Add('Phone%2032')%3CBR%20%2F%3E%5Bvoid%5D%20%24listBox.Items.Add('Phone%2033')%3CBR%20%2F%3E%5Bvoid%5D%20%24listBox.Items.Add('Phone%2034')%3CBR%20%2F%3E%5Bvoid%5D%20%24listBox.Items.Add('Phone%2035')%3CBR%20%2F%3E%5Bvoid%5D%20%24listBox.Items.Add('Phone%2036')%3CBR%20%2F%3E%5Bvoid%5D%20%24listBox.Items.Add('Phone%2041')%3CBR%20%2F%3E%5Bvoid%5D%20%24listBox.Items.Add('Phone%2042')%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3EThank%20you%20for%20help!%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1390474%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EWindows%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EWindows%20Server%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1390548%22%20slang%3D%22en-US%22%3ERe%3A%20Run%20sub%20command%20under%20other%20line%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1390548%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F668434%22%20target%3D%22_blank%22%3E%40KickAss1996%3C%2FA%3E%3CSPAN%3EI%20don't%20know%20exactly%20what%20you%20mean%20but%3C%2FSPAN%3E%26nbsp%3B%3CSPAN%20class%3D%22hljs-variable%22%3E%24listBox%3C%2FSPAN%3E%3CSPAN%3E.SelectedItem%20will%20contain%20the%20value%20of%20the%20selected%20item.%26nbsp%3B%20Hope%20it%20helps!%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EGrtz%2C%20Manfred%20de%20Laat%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1394935%22%20slang%3D%22en-US%22%3ERe%3A%20Run%20sub%20command%20under%20other%20line%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1394935%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20thank%20you%20for%20replay%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F412431%22%20target%3D%22_blank%22%3E%40Manfred101%3C%2FA%3E%26nbsp%3Bi%20mean%20after%20i%20select%20something%20from%20the%20list%20the%20command%20of%20stop-computer%20for%20example%20will%20running%20only%20for%20i%20marked%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1395107%22%20slang%3D%22en-US%22%3ERe%3A%20Run%20sub%20command%20under%20other%20line%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1395107%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F668434%22%20target%3D%22_blank%22%3E%40KickAss1996%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EFirst%3A%20I%20don%E2%80%99t%20have%20a%20clue%20what%20you%20try%20to%20achieve.%20On%20your%20form%20there%20is%20one%20list%20whit%20multiple%20items%20and%20two%20buttons%26nbsp%3B%20If%20you%20select%20for%20example%20%E2%80%9CPhone%2021%E2%80%9D%20and%20then%20%E2%80%9COK%E2%80%9D.%20You%20want%20to%20run%20a%20command%20Stop-computer%E2%80%A6%3F%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESecond%3A%20Working%20whit%20Windows%20Forms%20is%20so%20much%20fun!%20But%20it%20can%20be%20challenging%20as%20well.%20I%20used%20Powershell%20studio%20in%20the%20past%20for%20making%20my%20forms.%20This%20is%20a%20more%20visual%20approach%20and%20gives%20you%20great%20insights%20on%20how%20you%20can%20use%20all%20Windows%20form%20elements%2C%20what%20their%20properties%20are%2C%20what%20methods%20to%20use%20etc.%20They%20have%20a%2045%20day%20trail%20and%20if%20you%20use%20it%20alongside%20your%20scripts%20it%20will%20help%20you%20a%20lot%20in%20getting%20started%20whit%20Windows%20Forms.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1395153%22%20slang%3D%22en-US%22%3ERe%3A%20Run%20sub%20command%20under%20other%20line%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1395153%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F412431%22%20target%3D%22_blank%22%3E%40Manfred101%3C%2FA%3E%26nbsp%3Bthe%20stop-computer%20was%20an%20example%2C%20i%20mean%20that's%20my%20goal%20is%20to%20run%20a%20command%20only%20on%20one%20item%20from%20the%20list%20do%20not%20matter%20which%20item%20i%20pick.%3C%2FP%3E%3CP%3Eif%20this%20is%20my%20list%3C%2FP%3E%3CP%3Ephone%201%3C%2FP%3E%3CP%3E2%3C%2FP%3E%3CP%3E3%3C%2FP%3E%3CP%3E3...%3C%2FP%3E%3CP%3Ei%20want%20when%20i%20click%20phone%201%20the%20command%20run%20only%20for%20phone%201%26nbsp%3B%3C%2FP%3E%3CP%3Eand%20if%20i%20click%20on%20phone%203%20the%20command%20run%20only%20for%20the%20phone%203%3C%2FP%3E%3CP%3Ethank%20you!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1395766%22%20slang%3D%22en-US%22%3ERe%3A%20Run%20sub%20command%20under%20other%20line%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1395766%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F668434%22%20target%3D%22_blank%22%3E%40KickAss1996%3C%2FA%3E%26nbsp%3BThe%20output%20in%20the%20code%20snippet%20below%20shows%20you%20a%20messagebox%20but%20you%20do%20whatever%20you%20want.%20Just%20create%20your%20own%20%24ScriptToExecute%20scriptblock!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EGoodluck!%3C%2FP%3E%3CP%3EGrtz%2C%20Manfred%20de%20Laat%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%5Bvoid%5D%20%5BSystem.Reflection.Assembly%5D%3A%3ALoadWithPartialName(%22System.Drawing%22)%20%20%0A%5Bvoid%5D%20%5BSystem.Reflection.Assembly%5D%3A%3ALoadWithPartialName(%22System.Windows.Forms%22)%20%0A%5Bvoid%5D%20%5BSystem.Windows.Forms.Application%5D%3A%3AEnableVisualStyles()%20%0A%0A%24Form%20%3D%20New-Object%20system.Windows.Forms.Form%20%0A%24Form.Size%20%3D%20New-Object%20System.Drawing.Size(400%2C200)%20%0A%24Form.Width%20%3D%20400%20%0A%24Form.Height%20%3D%20500%20%0A%24form.MaximizeBox%20%3D%20%24false%20%0A%24Form.StartPosition%20%3D%20%22CenterScreen%22%20%0A%24Form.FormBorderStyle%20%3D%20'Fixed3D'%20%0A%24Form.Text%20%3D%20%22My%20Application%22%20%0A%0A%24listBox%20%3D%20New-Object%20System.Windows.Forms.ListBox%0A%24listBox.Location%20%3D%20New-Object%20System.Drawing.Point(10%2C40)%0A%24listBox.Size%20%3D%20New-Object%20System.Drawing.Size(260%2C20)%0A%24listBox.Height%20%3D%2080%0A%0A%5Bvoid%5D%20%24listBox.Items.Add('Phone%2010')%0A%5Bvoid%5D%20%24listBox.Items.Add('Phone%2011')%0A%5Bvoid%5D%20%24listBox.Items.Add('Phone%2012')%0A%5Bvoid%5D%20%24listBox.Items.Add('Phone%2013')%0A%5Bvoid%5D%20%24listBox.Items.Add('Phone%2014')%0A%5Bvoid%5D%20%24listBox.Items.Add('Phone%2015')%0A%5Bvoid%5D%20%24listBox.Items.Add('Phone%2016')%0A%0A%24form.Controls.Add(%24listBox)%0A%20%0A%24ScriptToExecute%20%3D%20%7B%0A%20%20%20%20%5BSystem.Windows.MessageBox%5D%3A%3AShow(%24listBox.SelectedItem)%0A%7D%20%0A%24Okbutton%20%3D%20New-Object%20System.Windows.Forms.Button%20%0A%24Okbutton.Location%20%3D%20New-Object%20System.Drawing.Size(140%2C200)%20%0A%24Okbutton.Size%20%3D%20New-Object%20System.Drawing.Size(120%2C30)%20%0A%24Okbutton.Text%20%3D%20%22Ok%22%20%0A%24Okbutton.Add_Click(%24ScriptToExecute)%20%0A%0A%24Form.Controls.Add(%24Okbutton)%20%20%0A%24Form.ShowDialog()%20%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1396022%22%20slang%3D%22en-US%22%3ERe%3A%20Run%20sub%20command%20under%20other%20line%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1396022%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F668434%22%20target%3D%22_blank%22%3E%40KickAss1996%3C%2FA%3E%26nbsp%3B%20Ckeck%20out%20the%20snippet%20below%2C%20just%20edit%20the%26nbsp%3B%24ScriptToExecute%20to%20whatever%20you%20want%20to%20do!%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%5Bvoid%5D%20%5BSystem.Reflection.Assembly%5D%3A%3ALoadWithPartialName(%22System.Drawing%22)%20%20%0A%5Bvoid%5D%20%5BSystem.Reflection.Assembly%5D%3A%3ALoadWithPartialName(%22System.Windows.Forms%22)%20%0A%5Bvoid%5D%20%5BSystem.Windows.Forms.Application%5D%3A%3AEnableVisualStyles()%20%0A%0A%24Form%20%3D%20New-Object%20system.Windows.Forms.Form%20%0A%24Form.Size%20%3D%20New-Object%20System.Drawing.Size(400%2C200)%20%0A%24Form.Width%20%3D%20400%20%0A%24Form.Height%20%3D%20500%20%0A%24form.MaximizeBox%20%3D%20%24false%20%0A%24Form.StartPosition%20%3D%20%22CenterScreen%22%20%0A%24Form.FormBorderStyle%20%3D%20'Fixed3D'%20%0A%24Form.Text%20%3D%20%22My%20Application%22%20%0A%0A%24listBox%20%3D%20New-Object%20System.Windows.Forms.ListBox%0A%24listBox.Location%20%3D%20New-Object%20System.Drawing.Point(10%2C40)%0A%24listBox.Size%20%3D%20New-Object%20System.Drawing.Size(260%2C20)%0A%24listBox.Height%20%3D%2080%0A%0A%5Bvoid%5D%20%24listBox.Items.Add('Phone%2010')%0A%5Bvoid%5D%20%24listBox.Items.Add('Phone%2011')%0A%5Bvoid%5D%20%24listBox.Items.Add('Phone%2012')%0A%5Bvoid%5D%20%24listBox.Items.Add('Phone%2013')%0A%5Bvoid%5D%20%24listBox.Items.Add('Phone%2014')%0A%5Bvoid%5D%20%24listBox.Items.Add('Phone%2015')%0A%5Bvoid%5D%20%24listBox.Items.Add('Phone%2016')%0A%0A%24form.Controls.Add(%24listBox)%0A%20%0A%24ScriptToExecute%20%3D%20%7B%0A%20%20%20%20%5BSystem.Windows.MessageBox%5D%3A%3AShow(%24listBox.SelectedItem)%0A%7D%20%0A%24Okbutton%20%3D%20New-Object%20System.Windows.Forms.Button%20%0A%24Okbutton.Location%20%3D%20New-Object%20System.Drawing.Size(140%2C200)%20%0A%24Okbutton.Size%20%3D%20New-Object%20System.Drawing.Size(120%2C30)%20%0A%24Okbutton.Text%20%3D%20%22Ok%22%20%0A%24Okbutton.Add_Click(%24ScriptToExecute)%20%0A%0A%24Form.Controls.Add(%24Okbutton)%20%20%0A%24Form.ShowDialog()%20%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EGoodluck!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EGrtz%2C%20Manfred%20de%20Laat%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
New 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
Highlighted

@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

Highlighted

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 

Highlighted

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

Highlighted

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

Highlighted

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

 

 

 

Highlighted

@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