Selecting Items from a List Box

I found that documentation - which could works for me

https://docs.microsoft.com/en-us/powershell/scripting/samples/selecting-items-from-a-list-box?view=powershell-7.1

I want to add something: i want to populate the list box with choices and depending of the choices made, i will run different commands

atl-dc-001 could be replaced by ping

atl-dc-002 could be replaced by tracert

If in the list box i select ping, when i press OK - i want an action to run and if i choose tracert i want a second action to run. I don’t find the right formula to make it works. Any idea?

Thanks

Ex:

[void] $listBox.Items.Add('atl-dc-001')

[void] $listBox.Items.Add(‘atl-dc-002’)

[void] $listBox.Items.Add(‘atl-dc-003’)

$form.Controls.Add($listBox) $form.Topmost = $true $result = $form.ShowDialog()

if ($result -eq [System.Windows.Forms.DialogResult]::OK)

{

$x = $listBox.SelectedItem

$x

}


 

Just use switch:

switch ( $listBox.SelectedItem ) {
    'tracert' {
        #Do stuff for tracert
    }
    'ping' {
        #Do stuff for ping
    }
}

Thanks Rob,

 

I think it smells very good ;o)