Forum Discussion

Schulzi's avatar
Schulzi
Brass Contributor
Feb 25, 2021

foreach loop, 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: 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

  • Schulzi 

     

    You'd just need to use an array and populate that within the foreach loop:

    $selected_Users = $selected_Output -split ","
    $AllMail = @()
    foreach($User in $selected_Users)
    {
        $selected_email = "$User@mail.com"
        $AllMail += ,$selected_email
    }
    $AllMail

     which would leave you with $AllMail as an array containing all the e-mail addresses.

    If you wanted a single variable rather than an array, with a comma separated list, you'd do this at the end:

    $EmailList = $AllMail -join ","
  • Schulzi 

     

    You'd just need to use an array and populate that within the foreach loop:

    $selected_Users = $selected_Output -split ","
    $AllMail = @()
    foreach($User in $selected_Users)
    {
        $selected_email = "$User@mail.com"
        $AllMail += ,$selected_email
    }
    $AllMail

     which would leave you with $AllMail as an array containing all the e-mail addresses.

    If you wanted a single variable rather than an array, with a comma separated list, you'd do this at the end:

    $EmailList = $AllMail -join ","

Resources