forms
2 Topicsforeach loop with self creating variables
I have following (part) of my script: $form_Users = New-Object System.Windows.Forms.Form $form_Users.Text = "Users to add" $form_Users.Size = New-Object System.Drawing.Size (300,200) $form_Users.StartPosition = "CenterScreen" $okButton_Users = New-Object System.Windows.Forms.Button $okButton_Users.Location = New-Object System.Drawing.Point (75,120) $okButton_Users.Size = New-Object System.Drawing.Size (75,23) $okButton_Users.Text = "OK" $okButton_Users.DialogResult = [System.Windows.Forms.DialogResult]::OK $form_Users.AcceptButton = $okButton_Users $form_Users.Controls.Add($okButton_Users) $cancelButton_Users = New-Object System.Windows.Forms.Button $cancelButton_Users.Location = New-Object System.Drawing.Point (150,120) $cancelButton_Users.Size = New-Object System.Drawing.Size (75,23) $cancelButton_Users.Text = "Cancel" $cancelButton_Users.DialogResult = [System.Windows.Forms.DialogResult]::Cancel $form_Users.CancelButton = $cancelButton_Users $form_Users.Controls.Add($cancelButton_Users) $label_Users = New-Object System.Windows.Forms.Label $label_Users.Location = New-Object System.Drawing.Point (10,20) $label_Users.Size = New-Object System.Drawing.Size (280,20) $label_Users.Text = "Please enter the names in the space below:" $form_Users.Controls.Add($label_Users) $textBox_Users = New-Object System.Windows.Forms.TextBox $textBox_Users.Location = New-Object System.Drawing.Point (10,40) $textBox_Users.Size = New-Object System.Drawing.Size (260,20) $form_Users.Controls.Add($textBox_Users) $form_Users.TopMost = $true $form_Users.Add_Shown({$textBox_Users.Select()}) $form_Users.ShowDialog() $selected_Output = $textBox_Users.Text $selected_Users = $selected_Output -split "," foreach($User in $selected_Users) { $selected_email = "$User@mail.com" $selected_email } You can input values in the Textbox, separated by commas, each of them is a single value afterwards. In my foreach loop I want to create an email for each user that got type in on the top for example: mailto:surname.name@domain.com If you have 5 users typed in on the top, you get 5 emails, so far so good BUT each Email needs to be accesible after the loop ends, so I need a variable for each Mail like $Mail1, $Mail2, $Mail3 and so on... And now to an even more complicated step: I don't want to set the max number of persons typed in on top to like 5, it needs to be adjustable, so on Example no.1 you only type in 2 name and on example no. 2 you type in 6 names = it varies each time So I need to figure out, how many Mails got created, catch them after the loop and combine them into 1 variable ($AllMails) Greetings Yannik Schulz1KViews0likes0CommentsWildcards at end of Variable
do { 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 region 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(@("A_Leonding_2","A_Neidling","D_Karlsruhe","D_Luckenwalde","D_Mogendorf","FR_Lyon","IT_Rovereto")) $form_location_EU.Controls.Add($Box_EU) 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) if ($result_location_EU -eq [System.Windows.Forms.DialogResult]::OK) { $Name = $textBox_Mailbox_EU.Text $Email = $textBox_Mailbox_EU.Text -replace " ","." $Emailwithat = "$email@rosenbauer.com" $Location = $Box_EU.SelectedItems $inOrder = [System.Windows.Forms.MessageBox]::Show("Location:$Location`nName:$Name`nEmail:$Emailwithat,","Everything's alright?",4,[System.Windows.Forms.MessageBoxIcon]::Question) } } } }until($inOrder -eq "YES") As you can see above, I'm making use of [System.Windows.Forms.Dialogresult] in my script, i got multiple of these in the script, so if you forget to choose something, you will get an error and get back to try again At the end of the whole Script I want to make a dialogwindow, that asks if everything is correct, if you say NO, it will start the loop from top again BUT if you say cancel in one of the spots on another dialogwindow the whole script should get ended. If I try something like this at the end: until(($inOrder -eq "YES") -or ([System.Windows.Forms.DialogResult] -ne "YES")) The script also will get ended if you say "NO" on the "Everything's alright?" box, because it abviously is Not equal to "yes" Is there a way to repeat the script when pressing "NO" and ending it when "cancel" is pressed? PS: until(($inOrder -eq "YES") -or ([System.Windows.Forms.DialogResult] -eq "Cancel")) or until(($inOrder -eq "YES") -or ([System.Windows.Forms.DialogResult] -like "Cancel")) did not work for me One possibility probably would be to use a wildcard at the end of a variable, like this: $result_region #$result_Location_ $result_location_EU $result_location_LATAM $result_location_APAC $result_location_NA #$result_type_ $result_type_EU $result_type_LATAM $result_type_APAC $result_type_NA <# some more in the same chape... its always like $result_something_region #> so in my case a variable like: $result_type_* would be perfect, so that i can use the one, regardless how many others are there with the beginning "$result_type_" Greetings Yannik Schulz563Views0likes0Comments