foreach loop with self creating variables

%3CLINGO-SUB%20id%3D%22lingo-sub-2165939%22%20slang%3D%22en-US%22%3Eforeach%20loop%20with%20self%20creating%20variables%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2165939%22%20slang%3D%22en-US%22%3E%3CP%3EI%20have%20following%20(part)%20of%20my%20script%3A%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24form_Users%20%3D%20New-Object%20System.Windows.Forms.Form%0A%24form_Users.Text%20%3D%20%22Users%20to%20add%22%0A%24form_Users.Size%20%3D%20New-Object%20System.Drawing.Size%20(300%2C200)%0A%24form_Users.StartPosition%20%3D%20%22CenterScreen%22%0A%0A%24okButton_Users%20%3D%20New-Object%20System.Windows.Forms.Button%0A%24okButton_Users.Location%20%3D%20New-Object%20System.Drawing.Point%20(75%2C120)%0A%24okButton_Users.Size%20%3D%20New-Object%20System.Drawing.Size%20(75%2C23)%0A%24okButton_Users.Text%20%3D%20%22OK%22%0A%24okButton_Users.DialogResult%20%3D%20%5BSystem.Windows.Forms.DialogResult%5D%3A%3AOK%0A%24form_Users.AcceptButton%20%3D%20%24okButton_Users%0A%24form_Users.Controls.Add(%24okButton_Users)%0A%0A%24cancelButton_Users%20%3D%20New-Object%20System.Windows.Forms.Button%0A%24cancelButton_Users.Location%20%3D%20New-Object%20System.Drawing.Point%20(150%2C120)%0A%24cancelButton_Users.Size%20%3D%20New-Object%20System.Drawing.Size%20(75%2C23)%0A%24cancelButton_Users.Text%20%3D%20%22Cancel%22%0A%24cancelButton_Users.DialogResult%20%3D%20%5BSystem.Windows.Forms.DialogResult%5D%3A%3ACancel%0A%24form_Users.CancelButton%20%3D%20%24cancelButton_Users%0A%24form_Users.Controls.Add(%24cancelButton_Users)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%24label_Users%20%3D%20New-Object%20System.Windows.Forms.Label%0A%24label_Users.Location%20%3D%20New-Object%20System.Drawing.Point%20(10%2C20)%0A%24label_Users.Size%20%3D%20New-Object%20System.Drawing.Size%20(280%2C20)%0A%24label_Users.Text%20%3D%20%22Please%20enter%20the%20names%20in%20the%20space%20below%3A%22%0A%24form_Users.Controls.Add(%24label_Users)%0A%0A%24textBox_Users%20%3D%20New-Object%20System.Windows.Forms.TextBox%0A%24textBox_Users.Location%20%3D%20New-Object%20System.Drawing.Point%20(10%2C40)%0A%24textBox_Users.Size%20%3D%20New-Object%20System.Drawing.Size%20(260%2C20)%0A%24form_Users.Controls.Add(%24textBox_Users)%0A%0A%24form_Users.TopMost%20%3D%20%24true%0A%0A%24form_Users.Add_Shown(%7B%24textBox_Users.Select()%7D)%0A%0A%24selected_Output%20%3D%20%24textBox_Users.Text%0A%24selected_Users%20%3D%20%24seelcted_Output%20-split%20%22%2C%22%0Aforeach(%24User%20in%20%24selected_Users)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%24selected_email%20%3D%20%22%24User%40mail.com%22%0A%20%20%20%20%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3EYou%20can%20input%20values%20in%20the%20Textbox%2C%20separated%20by%20commas%2C%20each%20of%20them%20is%20a%20single%20value%20afterwards.%3C%2FP%3E%3CP%3EIn%20my%20foreach%20loop%20I%20want%20to%20create%20an%20email%20for%20each%20user%20that%20got%20type%20in%20on%20the%20top%20for%20example%3A%20%3CA%20href%3D%22mailto%3Asurname.name%40domain.com%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Esurname.name%40domain.com%3C%2FA%3E%3C%2FP%3E%3CP%3EIf%20you%20have%205%20users%20typed%20in%20on%20the%20top%2C%20you%20get%205%20emails%2C%20so%20far%20so%20good%20BUT%20each%20Email%20needs%20to%20be%20accesible%20after%20the%20loop%20ends%2C%20so%20I%20need%20a%20variable%20for%20each%20Mail%20like%20%24Mail1%2C%20%24Mail2%2C%20%24Mail3%20and%20so%20on...%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAnd%20now%20to%20an%20even%20more%20complicated%20step%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20don't%20want%20to%20set%20the%20max%20number%20of%20persons%20typed%20in%20on%20top%20to%20like%205%2C%20it%20needs%20to%20be%20adjustable%2C%20so%20on%20Example%20no.1%20you%20only%20type%20in%202%20name%20and%20on%20example%20no.%202%20you%20type%20in%206%20names%20%3D%20it%20varies%20each%20time%3C%2FP%3E%3CP%3ESo%20I%20need%20to%20figure%20out%2C%20how%20many%20Mails%20got%20created%2C%20catch%20them%20after%20the%20loop%20and%20combine%20them%20into%201%20variable%20(%24AllMails)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EGreetings%26nbsp%3B%3C%2FP%3E%3CP%3EYannik%20Schulz%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2165939%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EForms%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Contributor

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: 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 Schulz

0 Replies