Use a specific variable if output contains "somevalue"

Brass Contributor

I'm creating a form using Powershell and now I'm at a point where I need help, to get further, like shown below:

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

#select region
$form_region = New-Object System.Windows.Forms.Form
$form_region.Text = 'Country'
$form_region.Size = New-Object System.Drawing.Size(300,200)
$form_region.StartPosition = 'CenterScreen'
$form_region.TopMost = $true

$okButton_region = New-Object System.Windows.Forms.Button
$okButton_region.Location = New-Object System.Drawing.Point (75,120)
$okButton_region.Size = New-Object System.Drawing.Size (75,23)
$okButton_region.Text = "OK"
$okButton_region.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form_region.AcceptButton = $okButton_region
$form_region.Controls.Add($okButton_region)

$cancelButton_region = New-Object System.Windows.Forms.Button
$cancelButton_region.Location = New-Object System.Drawing.Point (150,120)
$cancelButton_region.Size = New-Object System.Drawing.Size (75,23)
$cancelButton_region.Text = "Cancel"
$cancelButton_region.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form_region.CancelButton = $cancelButton_region
$form_region.Controls.Add($cancelButton_region)

$label_region = New-Object System.Windows.Forms.Label
$label_region.Location = New-Object System.Drawing.Point (10,20)
$label_region.Size = New-Object System.Drawing.Size (280,20)
$label_region.Text = "Please make a selection from the list below:"
$form_region.Controls.Add($label_region)

$Box_region = New-Object System.Windows.Forms.ListBox
$Box_region.Location = New-Object System.Drawing.Point (10,40)
$Box_region.Size = New-Object System.Drawing.Size (260,70)

[void] $Box_region.Items.AddRange(@("APAC","EU","LATAM","NA"))

$form_region.Controls.Add($Box_region)

do
    {
        $result_region = $form_region.ShowDialog()
        if ($Box_region.SelectedIndices.Count -lt 1 -and $result_region -eq [System.Windows.Forms.DialogResult]::OK)
            {
                [System.Windows.Forms.MessageBox]::Show("Nothing was selected, please select a path first","WARNING",0,[System.Windows.Forms.MessageBoxIcon]::Warning)
            }
    }until (($result_region -eq [System.Windows.Forms.DialogResult]::OK -and $Box_region.SelectedIndices.Count -ge 1) -or $result_region -ne [System.Windows.Forms.DialogResult]::OK)
    


    #Select Location
    if (($Box_region.SelectedItem -eq "EU") -and ($result_region -eq [System.Windows.Forms.DialogResult]::OK))
        {
            $form_location_EU = New-Object System.Windows.Forms.Form
            $form_location_EU.Text = 'Locations'
            $form_location_EU.Size = New-Object System.Drawing.Size(300,200)
            $form_location_EU.StartPosition = 'CenterScreen'
            $form_location_EU.TopMost = $true

            $okButton_EU = New-Object System.Windows.Forms.Button
            $okButton_EU.Location = New-Object System.Drawing.Point (75,120)
            $okButton_EU.Size = New-Object System.Drawing.Size (75,23)
            $okButton_EU.Text = "OK"
            $okButton_EU.DialogResult = [System.Windows.Forms.DialogResult]::OK
            $form_location_EU.AcceptButton = $okButton_EU
            $form_location_EU.Controls.Add($okButton_EU)

            $cancelButton_EU = New-Object System.Windows.Forms.Button
            $cancelButton_EU.Location = New-Object System.Drawing.Point (150,120)
            $cancelButton_EU.Size = New-Object System.Drawing.Size (75,23)
            $cancelButton_EU.Text = "Cancel"
            $cancelButton_EU.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
            $form_location_EU.CancelButton = $cancelButton_EU
            $form_location_EU.Controls.Add($cancelButton_EU)

            $label_EU = New-Object System.Windows.Forms.Label
            $label_EU.Location = New-Object System.Drawing.Point (10,20)
            $label_EU.Size = New-Object System.Drawing.Size (280,20)
            $label_EU.Text = "Please make a selection from the list below:"
            $form_location_EU.Controls.Add($label_EU)

            $Box_EU = New-Object System.Windows.Forms.ListBox
            $Box_EU.Location = New-Object System.Drawing.Point (10,40)
            $Box_EU.Size = New-Object System.Drawing.Size (260,70)

            [void] $Box_EU.Items.AddRange(@("AUT_Leonding_1","AUT_Leonding_2","AUT_Neidling","DEU_Karlsruhe","DEU_Luckenwalde","DEU_Mogendorf","FRA_Lyon","ITA_Rovereto"))

            $form_location_EU.Controls.Add($Box_EU)

            $AUT_Leonding_1 = "my.domain/A_Leonding_1/Users/_FunctionUser"
            $AUT_Leonding_2 = "my.domain/A_Leonding_2/Users/_FunctionUser"
            $AUT_Neidling = "my.domain/A_Neidling/Users/_FuntionUser"
            $DEU_Karlsruhe = "my.domain/D_Karlsruhe/Users/_FuntionUser"
            $DEU_Luckenwalde= "my.domain/D_Luckenwalde/Users2/_FuntionUser"
            $DEU_Mogendorf = "my.domain/D_Mogendorf/Users/_FunctionUser"
            $FRA_Lyon = "my.domain/FR_Lyon/Users/_FuntionUser"
            $ITA_Rovereto = "my.domain/IT_Rovereto/Users/_FunctionUser"

            do
                {
                    $result_location_EU = $form_location_EU.ShowDialog()
                    if ($Box_EU.SelectedIndices.Count -lt 1 -and $result_location_EU -eq [System.Windows.Forms.DialogResult]::OK)
                        {
                            [System.Windows.Forms.MessageBox]::Show("Nothing was selected, please select a location first","WARNING",0,[System.Windows.Forms.MessageBoxIcon]::Warning)
                        }
                }until (($result_location_EU -eq [System.Windows.Forms.DialogResult]::OK -and $Box_EU.SelectedIndices.Count -ge 1) -or $result_location_EU -ne [System.Windows.Forms.DialogResult]::OK)

In the listBox there are 5 possible Cities to choose from, if you pick "Karlsruhe" for example, the output will be "Karlsruhe" as well, so far so good...

Now the Problem:

If the output is "Karlsruhe" I want to select the corresponding variable "Karlsruhe", if "Luckenwalde" is chosen, I want to use the variable "Luckenwalde" and so on.

 

Is there a way to compare the output of "$x" with the name of the variable and select the corresponding one?

 

EDIT 1:

Is there a way to display the name of the variable in this context not the contains?

e.g.,

$Variable = "something"

if I would use " [void] $listBox.Items.Add($Variable) " , the containing value would be displayed and the button would say 'something', is there a way to use the Variable-name instead, so the button would say "Variable" instead of "something"?

 


Greetings

Yannik Schulz

0 Replies