PowerShell Email with Text Input











up vote
2
down vote

favorite
1












I'm trying to write a script that will throw up four text input boxes and save their entered values into four variables, each representing Username, Phone PIN, Voicemail PIN and Phone extension - Once populated, it sends the email, which itself uses said variables as placeholders where each value must appear.



I've obscured and deleted some of the code for anonymity, however I hope it still reads logically.



The script partially works, but with a bizarre flaw that I cannot identify.



When initially executed, the script throws up the 4 text input boxes and successfully does a Write-Host, displaying the result of the data entered into the text input boxes (E.g. fred.bloggs, 1000, 1000, 1000) - Proving that the user-entered data IS being saved into the variables. It then sends the email, but ALL of the variable fields are blank when I receive the email. (I'm testing by putting my own username into the Username input box.)



If I run the script a second time, the script throws up the 4 boxes, does successful write-host showing the new values (E.g. fred.bloggs, 1001, 1001, 1001) saved against each variable, then sends the email - But, the email contains the values I entered when I first ran the script. (fred.blogs, 1000, 1000, 1000)



So I get blank variables in the email output when I run it the first time (E.g. If I close Powershell_ISE and re-open). And I get the previous values entered if I run it a second time. It seems like whenever I run the script, it always sends the email using the values of the previous attempt.



So it's like this:



Attempt 1:
Entered fred.bloggs, 1000, 1000, 1000 - Result: all blank in email variable fields



Attempt 2:
Entered fred.bloggs, 1001, 1001, 1001 - Result: Email variable fields populated with fred.bloggs, 1000, 1000, 1000



Attempt 3:
Entered fred.bloggs, 1002, 1002, 1002 - Result: Email variable fields populated with fred.bloggs, 1001, 1001, 1001



I suspect it's down to the way the text input box values are being saved against the variables, but I can't find the fault.



Code is as below. Any help would be immensely appreciated as I am very much learning from scratch with Powershell.



$EmailFrom = "obscured@obscured.com"
$EmailSubject = "Welcome to obscured + On-Boarding Details"
$SMTPServer = "obscured"
$SMTPPassword = Get-Content .mailpw.txt | ConvertTo-SecureString
$SMTPCred = New-Object System.Management.Automation.PSCredential "MailUser",
$SMTPPassword
$EmailBody = @"

Hi $StarterName,

Log in with obscured$StarterName

Your Personalised details:
Username: $StarterName<Br>
Email: $StarterName@obscured.com<Br>
Phone Extension: $PhoneExt (+obscured $PhoneExt) Dial 0 for external calls. <Br>
Phone Username: $StarterName<Br>
Phone PIN: $PhonePin <Br>
Voicemail PIN: $VMPin <Br>

"@

#INPUT BOX: STARTER USER NAME

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

$form = New-Object System.Windows.Forms.Form
$form.Text = 'Data Entry Form'
$form.Size = New-Object System.Drawing.Size(300,200)
$form.StartPosition = 'CenterScreen'

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

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

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = 'Enter Starter UserName:'
$form.Controls.Add($label)

$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(10,40)
$textBox.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox)

$form.Topmost = $true

$form.Add_Shown({$textBox.Select()})
$result = $form.ShowDialog()

if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
$StarterName = $textBox.Text
$StarterName
}

#INPUT BOX: PHONE EXTENSION

$form = New-Object System.Windows.Forms.Form
$form.Text = 'Data Entry Form'
$form.Size = New-Object System.Drawing.Size(300,200)
$form.StartPosition = 'CenterScreen'

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

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

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = 'Enter Starter Phone Extension'
$form.Controls.Add($label)

$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(10,40)
$textBox.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox)

$form.Topmost = $true

$form.Add_Shown({$textBox.Select()})
$result = $form.ShowDialog()

if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
$PhoneExt = $textBox.Text
$PhoneExt
}

#INPUT BOX: PHONE PIN

$form = New-Object System.Windows.Forms.Form
$form.Text = 'Data Entry Form'
$form.Size = New-Object System.Drawing.Size(300,200)
$form.StartPosition = 'CenterScreen'

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

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

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = 'Enter Starter Phone PIN:'
$form.Controls.Add($label)

$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(10,40)
$textBox.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox)

$form.Topmost = $true

$form.Add_Shown({$textBox.Select()})
$result = $form.ShowDialog()

if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
$PhonePIN = $textBox.Text
$PhonePIN
}

#INPUT BOX: VM PIN

$form = New-Object System.Windows.Forms.Form
$form.Text = 'Data Entry Form'
$form.Size = New-Object System.Drawing.Size(300,200)
$form.StartPosition = 'CenterScreen'

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

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

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = 'Enter Starter Voicemail PIN:'
$form.Controls.Add($label)

$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(10,40)
$textBox.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox)

$form.Topmost = $true

$form.Add_Shown({$textBox.Select()})
$result = $form.ShowDialog()

if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
$VMPin = $textBox.Text
$VMPin
}


Write-Host "Sending email to $StarterName@obscured.com" -ForegroundColor Green
Write-Host "PhoneExt: $PhoneExt" -ForegroundColor Green
Write-Host "Phone PIN: $PhonePIN" -ForegroundColor Green
Write-Host "Voicemail PIN: $VMPin" -ForegroundColor Green

Send-MailMessage -Credential $SMTPCred -To "$StarterName@obscured" -From
$EmailFrom -Subject $EmailSubject -SmtpServer $SMTPServer -Body $EmailBody -BodyAsHtml









share|improve this question


























    up vote
    2
    down vote

    favorite
    1












    I'm trying to write a script that will throw up four text input boxes and save their entered values into four variables, each representing Username, Phone PIN, Voicemail PIN and Phone extension - Once populated, it sends the email, which itself uses said variables as placeholders where each value must appear.



    I've obscured and deleted some of the code for anonymity, however I hope it still reads logically.



    The script partially works, but with a bizarre flaw that I cannot identify.



    When initially executed, the script throws up the 4 text input boxes and successfully does a Write-Host, displaying the result of the data entered into the text input boxes (E.g. fred.bloggs, 1000, 1000, 1000) - Proving that the user-entered data IS being saved into the variables. It then sends the email, but ALL of the variable fields are blank when I receive the email. (I'm testing by putting my own username into the Username input box.)



    If I run the script a second time, the script throws up the 4 boxes, does successful write-host showing the new values (E.g. fred.bloggs, 1001, 1001, 1001) saved against each variable, then sends the email - But, the email contains the values I entered when I first ran the script. (fred.blogs, 1000, 1000, 1000)



    So I get blank variables in the email output when I run it the first time (E.g. If I close Powershell_ISE and re-open). And I get the previous values entered if I run it a second time. It seems like whenever I run the script, it always sends the email using the values of the previous attempt.



    So it's like this:



    Attempt 1:
    Entered fred.bloggs, 1000, 1000, 1000 - Result: all blank in email variable fields



    Attempt 2:
    Entered fred.bloggs, 1001, 1001, 1001 - Result: Email variable fields populated with fred.bloggs, 1000, 1000, 1000



    Attempt 3:
    Entered fred.bloggs, 1002, 1002, 1002 - Result: Email variable fields populated with fred.bloggs, 1001, 1001, 1001



    I suspect it's down to the way the text input box values are being saved against the variables, but I can't find the fault.



    Code is as below. Any help would be immensely appreciated as I am very much learning from scratch with Powershell.



    $EmailFrom = "obscured@obscured.com"
    $EmailSubject = "Welcome to obscured + On-Boarding Details"
    $SMTPServer = "obscured"
    $SMTPPassword = Get-Content .mailpw.txt | ConvertTo-SecureString
    $SMTPCred = New-Object System.Management.Automation.PSCredential "MailUser",
    $SMTPPassword
    $EmailBody = @"

    Hi $StarterName,

    Log in with obscured$StarterName

    Your Personalised details:
    Username: $StarterName<Br>
    Email: $StarterName@obscured.com<Br>
    Phone Extension: $PhoneExt (+obscured $PhoneExt) Dial 0 for external calls. <Br>
    Phone Username: $StarterName<Br>
    Phone PIN: $PhonePin <Br>
    Voicemail PIN: $VMPin <Br>

    "@

    #INPUT BOX: STARTER USER NAME

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

    $form = New-Object System.Windows.Forms.Form
    $form.Text = 'Data Entry Form'
    $form.Size = New-Object System.Drawing.Size(300,200)
    $form.StartPosition = 'CenterScreen'

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

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

    $label = New-Object System.Windows.Forms.Label
    $label.Location = New-Object System.Drawing.Point(10,20)
    $label.Size = New-Object System.Drawing.Size(280,20)
    $label.Text = 'Enter Starter UserName:'
    $form.Controls.Add($label)

    $textBox = New-Object System.Windows.Forms.TextBox
    $textBox.Location = New-Object System.Drawing.Point(10,40)
    $textBox.Size = New-Object System.Drawing.Size(260,20)
    $form.Controls.Add($textBox)

    $form.Topmost = $true

    $form.Add_Shown({$textBox.Select()})
    $result = $form.ShowDialog()

    if ($result -eq [System.Windows.Forms.DialogResult]::OK)
    {
    $StarterName = $textBox.Text
    $StarterName
    }

    #INPUT BOX: PHONE EXTENSION

    $form = New-Object System.Windows.Forms.Form
    $form.Text = 'Data Entry Form'
    $form.Size = New-Object System.Drawing.Size(300,200)
    $form.StartPosition = 'CenterScreen'

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

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

    $label = New-Object System.Windows.Forms.Label
    $label.Location = New-Object System.Drawing.Point(10,20)
    $label.Size = New-Object System.Drawing.Size(280,20)
    $label.Text = 'Enter Starter Phone Extension'
    $form.Controls.Add($label)

    $textBox = New-Object System.Windows.Forms.TextBox
    $textBox.Location = New-Object System.Drawing.Point(10,40)
    $textBox.Size = New-Object System.Drawing.Size(260,20)
    $form.Controls.Add($textBox)

    $form.Topmost = $true

    $form.Add_Shown({$textBox.Select()})
    $result = $form.ShowDialog()

    if ($result -eq [System.Windows.Forms.DialogResult]::OK)
    {
    $PhoneExt = $textBox.Text
    $PhoneExt
    }

    #INPUT BOX: PHONE PIN

    $form = New-Object System.Windows.Forms.Form
    $form.Text = 'Data Entry Form'
    $form.Size = New-Object System.Drawing.Size(300,200)
    $form.StartPosition = 'CenterScreen'

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

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

    $label = New-Object System.Windows.Forms.Label
    $label.Location = New-Object System.Drawing.Point(10,20)
    $label.Size = New-Object System.Drawing.Size(280,20)
    $label.Text = 'Enter Starter Phone PIN:'
    $form.Controls.Add($label)

    $textBox = New-Object System.Windows.Forms.TextBox
    $textBox.Location = New-Object System.Drawing.Point(10,40)
    $textBox.Size = New-Object System.Drawing.Size(260,20)
    $form.Controls.Add($textBox)

    $form.Topmost = $true

    $form.Add_Shown({$textBox.Select()})
    $result = $form.ShowDialog()

    if ($result -eq [System.Windows.Forms.DialogResult]::OK)
    {
    $PhonePIN = $textBox.Text
    $PhonePIN
    }

    #INPUT BOX: VM PIN

    $form = New-Object System.Windows.Forms.Form
    $form.Text = 'Data Entry Form'
    $form.Size = New-Object System.Drawing.Size(300,200)
    $form.StartPosition = 'CenterScreen'

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

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

    $label = New-Object System.Windows.Forms.Label
    $label.Location = New-Object System.Drawing.Point(10,20)
    $label.Size = New-Object System.Drawing.Size(280,20)
    $label.Text = 'Enter Starter Voicemail PIN:'
    $form.Controls.Add($label)

    $textBox = New-Object System.Windows.Forms.TextBox
    $textBox.Location = New-Object System.Drawing.Point(10,40)
    $textBox.Size = New-Object System.Drawing.Size(260,20)
    $form.Controls.Add($textBox)

    $form.Topmost = $true

    $form.Add_Shown({$textBox.Select()})
    $result = $form.ShowDialog()

    if ($result -eq [System.Windows.Forms.DialogResult]::OK)
    {
    $VMPin = $textBox.Text
    $VMPin
    }


    Write-Host "Sending email to $StarterName@obscured.com" -ForegroundColor Green
    Write-Host "PhoneExt: $PhoneExt" -ForegroundColor Green
    Write-Host "Phone PIN: $PhonePIN" -ForegroundColor Green
    Write-Host "Voicemail PIN: $VMPin" -ForegroundColor Green

    Send-MailMessage -Credential $SMTPCred -To "$StarterName@obscured" -From
    $EmailFrom -Subject $EmailSubject -SmtpServer $SMTPServer -Body $EmailBody -BodyAsHtml









    share|improve this question
























      up vote
      2
      down vote

      favorite
      1









      up vote
      2
      down vote

      favorite
      1






      1





      I'm trying to write a script that will throw up four text input boxes and save their entered values into four variables, each representing Username, Phone PIN, Voicemail PIN and Phone extension - Once populated, it sends the email, which itself uses said variables as placeholders where each value must appear.



      I've obscured and deleted some of the code for anonymity, however I hope it still reads logically.



      The script partially works, but with a bizarre flaw that I cannot identify.



      When initially executed, the script throws up the 4 text input boxes and successfully does a Write-Host, displaying the result of the data entered into the text input boxes (E.g. fred.bloggs, 1000, 1000, 1000) - Proving that the user-entered data IS being saved into the variables. It then sends the email, but ALL of the variable fields are blank when I receive the email. (I'm testing by putting my own username into the Username input box.)



      If I run the script a second time, the script throws up the 4 boxes, does successful write-host showing the new values (E.g. fred.bloggs, 1001, 1001, 1001) saved against each variable, then sends the email - But, the email contains the values I entered when I first ran the script. (fred.blogs, 1000, 1000, 1000)



      So I get blank variables in the email output when I run it the first time (E.g. If I close Powershell_ISE and re-open). And I get the previous values entered if I run it a second time. It seems like whenever I run the script, it always sends the email using the values of the previous attempt.



      So it's like this:



      Attempt 1:
      Entered fred.bloggs, 1000, 1000, 1000 - Result: all blank in email variable fields



      Attempt 2:
      Entered fred.bloggs, 1001, 1001, 1001 - Result: Email variable fields populated with fred.bloggs, 1000, 1000, 1000



      Attempt 3:
      Entered fred.bloggs, 1002, 1002, 1002 - Result: Email variable fields populated with fred.bloggs, 1001, 1001, 1001



      I suspect it's down to the way the text input box values are being saved against the variables, but I can't find the fault.



      Code is as below. Any help would be immensely appreciated as I am very much learning from scratch with Powershell.



      $EmailFrom = "obscured@obscured.com"
      $EmailSubject = "Welcome to obscured + On-Boarding Details"
      $SMTPServer = "obscured"
      $SMTPPassword = Get-Content .mailpw.txt | ConvertTo-SecureString
      $SMTPCred = New-Object System.Management.Automation.PSCredential "MailUser",
      $SMTPPassword
      $EmailBody = @"

      Hi $StarterName,

      Log in with obscured$StarterName

      Your Personalised details:
      Username: $StarterName<Br>
      Email: $StarterName@obscured.com<Br>
      Phone Extension: $PhoneExt (+obscured $PhoneExt) Dial 0 for external calls. <Br>
      Phone Username: $StarterName<Br>
      Phone PIN: $PhonePin <Br>
      Voicemail PIN: $VMPin <Br>

      "@

      #INPUT BOX: STARTER USER NAME

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

      $form = New-Object System.Windows.Forms.Form
      $form.Text = 'Data Entry Form'
      $form.Size = New-Object System.Drawing.Size(300,200)
      $form.StartPosition = 'CenterScreen'

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

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

      $label = New-Object System.Windows.Forms.Label
      $label.Location = New-Object System.Drawing.Point(10,20)
      $label.Size = New-Object System.Drawing.Size(280,20)
      $label.Text = 'Enter Starter UserName:'
      $form.Controls.Add($label)

      $textBox = New-Object System.Windows.Forms.TextBox
      $textBox.Location = New-Object System.Drawing.Point(10,40)
      $textBox.Size = New-Object System.Drawing.Size(260,20)
      $form.Controls.Add($textBox)

      $form.Topmost = $true

      $form.Add_Shown({$textBox.Select()})
      $result = $form.ShowDialog()

      if ($result -eq [System.Windows.Forms.DialogResult]::OK)
      {
      $StarterName = $textBox.Text
      $StarterName
      }

      #INPUT BOX: PHONE EXTENSION

      $form = New-Object System.Windows.Forms.Form
      $form.Text = 'Data Entry Form'
      $form.Size = New-Object System.Drawing.Size(300,200)
      $form.StartPosition = 'CenterScreen'

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

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

      $label = New-Object System.Windows.Forms.Label
      $label.Location = New-Object System.Drawing.Point(10,20)
      $label.Size = New-Object System.Drawing.Size(280,20)
      $label.Text = 'Enter Starter Phone Extension'
      $form.Controls.Add($label)

      $textBox = New-Object System.Windows.Forms.TextBox
      $textBox.Location = New-Object System.Drawing.Point(10,40)
      $textBox.Size = New-Object System.Drawing.Size(260,20)
      $form.Controls.Add($textBox)

      $form.Topmost = $true

      $form.Add_Shown({$textBox.Select()})
      $result = $form.ShowDialog()

      if ($result -eq [System.Windows.Forms.DialogResult]::OK)
      {
      $PhoneExt = $textBox.Text
      $PhoneExt
      }

      #INPUT BOX: PHONE PIN

      $form = New-Object System.Windows.Forms.Form
      $form.Text = 'Data Entry Form'
      $form.Size = New-Object System.Drawing.Size(300,200)
      $form.StartPosition = 'CenterScreen'

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

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

      $label = New-Object System.Windows.Forms.Label
      $label.Location = New-Object System.Drawing.Point(10,20)
      $label.Size = New-Object System.Drawing.Size(280,20)
      $label.Text = 'Enter Starter Phone PIN:'
      $form.Controls.Add($label)

      $textBox = New-Object System.Windows.Forms.TextBox
      $textBox.Location = New-Object System.Drawing.Point(10,40)
      $textBox.Size = New-Object System.Drawing.Size(260,20)
      $form.Controls.Add($textBox)

      $form.Topmost = $true

      $form.Add_Shown({$textBox.Select()})
      $result = $form.ShowDialog()

      if ($result -eq [System.Windows.Forms.DialogResult]::OK)
      {
      $PhonePIN = $textBox.Text
      $PhonePIN
      }

      #INPUT BOX: VM PIN

      $form = New-Object System.Windows.Forms.Form
      $form.Text = 'Data Entry Form'
      $form.Size = New-Object System.Drawing.Size(300,200)
      $form.StartPosition = 'CenterScreen'

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

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

      $label = New-Object System.Windows.Forms.Label
      $label.Location = New-Object System.Drawing.Point(10,20)
      $label.Size = New-Object System.Drawing.Size(280,20)
      $label.Text = 'Enter Starter Voicemail PIN:'
      $form.Controls.Add($label)

      $textBox = New-Object System.Windows.Forms.TextBox
      $textBox.Location = New-Object System.Drawing.Point(10,40)
      $textBox.Size = New-Object System.Drawing.Size(260,20)
      $form.Controls.Add($textBox)

      $form.Topmost = $true

      $form.Add_Shown({$textBox.Select()})
      $result = $form.ShowDialog()

      if ($result -eq [System.Windows.Forms.DialogResult]::OK)
      {
      $VMPin = $textBox.Text
      $VMPin
      }


      Write-Host "Sending email to $StarterName@obscured.com" -ForegroundColor Green
      Write-Host "PhoneExt: $PhoneExt" -ForegroundColor Green
      Write-Host "Phone PIN: $PhonePIN" -ForegroundColor Green
      Write-Host "Voicemail PIN: $VMPin" -ForegroundColor Green

      Send-MailMessage -Credential $SMTPCred -To "$StarterName@obscured" -From
      $EmailFrom -Subject $EmailSubject -SmtpServer $SMTPServer -Body $EmailBody -BodyAsHtml









      share|improve this question













      I'm trying to write a script that will throw up four text input boxes and save their entered values into four variables, each representing Username, Phone PIN, Voicemail PIN and Phone extension - Once populated, it sends the email, which itself uses said variables as placeholders where each value must appear.



      I've obscured and deleted some of the code for anonymity, however I hope it still reads logically.



      The script partially works, but with a bizarre flaw that I cannot identify.



      When initially executed, the script throws up the 4 text input boxes and successfully does a Write-Host, displaying the result of the data entered into the text input boxes (E.g. fred.bloggs, 1000, 1000, 1000) - Proving that the user-entered data IS being saved into the variables. It then sends the email, but ALL of the variable fields are blank when I receive the email. (I'm testing by putting my own username into the Username input box.)



      If I run the script a second time, the script throws up the 4 boxes, does successful write-host showing the new values (E.g. fred.bloggs, 1001, 1001, 1001) saved against each variable, then sends the email - But, the email contains the values I entered when I first ran the script. (fred.blogs, 1000, 1000, 1000)



      So I get blank variables in the email output when I run it the first time (E.g. If I close Powershell_ISE and re-open). And I get the previous values entered if I run it a second time. It seems like whenever I run the script, it always sends the email using the values of the previous attempt.



      So it's like this:



      Attempt 1:
      Entered fred.bloggs, 1000, 1000, 1000 - Result: all blank in email variable fields



      Attempt 2:
      Entered fred.bloggs, 1001, 1001, 1001 - Result: Email variable fields populated with fred.bloggs, 1000, 1000, 1000



      Attempt 3:
      Entered fred.bloggs, 1002, 1002, 1002 - Result: Email variable fields populated with fred.bloggs, 1001, 1001, 1001



      I suspect it's down to the way the text input box values are being saved against the variables, but I can't find the fault.



      Code is as below. Any help would be immensely appreciated as I am very much learning from scratch with Powershell.



      $EmailFrom = "obscured@obscured.com"
      $EmailSubject = "Welcome to obscured + On-Boarding Details"
      $SMTPServer = "obscured"
      $SMTPPassword = Get-Content .mailpw.txt | ConvertTo-SecureString
      $SMTPCred = New-Object System.Management.Automation.PSCredential "MailUser",
      $SMTPPassword
      $EmailBody = @"

      Hi $StarterName,

      Log in with obscured$StarterName

      Your Personalised details:
      Username: $StarterName<Br>
      Email: $StarterName@obscured.com<Br>
      Phone Extension: $PhoneExt (+obscured $PhoneExt) Dial 0 for external calls. <Br>
      Phone Username: $StarterName<Br>
      Phone PIN: $PhonePin <Br>
      Voicemail PIN: $VMPin <Br>

      "@

      #INPUT BOX: STARTER USER NAME

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

      $form = New-Object System.Windows.Forms.Form
      $form.Text = 'Data Entry Form'
      $form.Size = New-Object System.Drawing.Size(300,200)
      $form.StartPosition = 'CenterScreen'

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

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

      $label = New-Object System.Windows.Forms.Label
      $label.Location = New-Object System.Drawing.Point(10,20)
      $label.Size = New-Object System.Drawing.Size(280,20)
      $label.Text = 'Enter Starter UserName:'
      $form.Controls.Add($label)

      $textBox = New-Object System.Windows.Forms.TextBox
      $textBox.Location = New-Object System.Drawing.Point(10,40)
      $textBox.Size = New-Object System.Drawing.Size(260,20)
      $form.Controls.Add($textBox)

      $form.Topmost = $true

      $form.Add_Shown({$textBox.Select()})
      $result = $form.ShowDialog()

      if ($result -eq [System.Windows.Forms.DialogResult]::OK)
      {
      $StarterName = $textBox.Text
      $StarterName
      }

      #INPUT BOX: PHONE EXTENSION

      $form = New-Object System.Windows.Forms.Form
      $form.Text = 'Data Entry Form'
      $form.Size = New-Object System.Drawing.Size(300,200)
      $form.StartPosition = 'CenterScreen'

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

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

      $label = New-Object System.Windows.Forms.Label
      $label.Location = New-Object System.Drawing.Point(10,20)
      $label.Size = New-Object System.Drawing.Size(280,20)
      $label.Text = 'Enter Starter Phone Extension'
      $form.Controls.Add($label)

      $textBox = New-Object System.Windows.Forms.TextBox
      $textBox.Location = New-Object System.Drawing.Point(10,40)
      $textBox.Size = New-Object System.Drawing.Size(260,20)
      $form.Controls.Add($textBox)

      $form.Topmost = $true

      $form.Add_Shown({$textBox.Select()})
      $result = $form.ShowDialog()

      if ($result -eq [System.Windows.Forms.DialogResult]::OK)
      {
      $PhoneExt = $textBox.Text
      $PhoneExt
      }

      #INPUT BOX: PHONE PIN

      $form = New-Object System.Windows.Forms.Form
      $form.Text = 'Data Entry Form'
      $form.Size = New-Object System.Drawing.Size(300,200)
      $form.StartPosition = 'CenterScreen'

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

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

      $label = New-Object System.Windows.Forms.Label
      $label.Location = New-Object System.Drawing.Point(10,20)
      $label.Size = New-Object System.Drawing.Size(280,20)
      $label.Text = 'Enter Starter Phone PIN:'
      $form.Controls.Add($label)

      $textBox = New-Object System.Windows.Forms.TextBox
      $textBox.Location = New-Object System.Drawing.Point(10,40)
      $textBox.Size = New-Object System.Drawing.Size(260,20)
      $form.Controls.Add($textBox)

      $form.Topmost = $true

      $form.Add_Shown({$textBox.Select()})
      $result = $form.ShowDialog()

      if ($result -eq [System.Windows.Forms.DialogResult]::OK)
      {
      $PhonePIN = $textBox.Text
      $PhonePIN
      }

      #INPUT BOX: VM PIN

      $form = New-Object System.Windows.Forms.Form
      $form.Text = 'Data Entry Form'
      $form.Size = New-Object System.Drawing.Size(300,200)
      $form.StartPosition = 'CenterScreen'

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

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

      $label = New-Object System.Windows.Forms.Label
      $label.Location = New-Object System.Drawing.Point(10,20)
      $label.Size = New-Object System.Drawing.Size(280,20)
      $label.Text = 'Enter Starter Voicemail PIN:'
      $form.Controls.Add($label)

      $textBox = New-Object System.Windows.Forms.TextBox
      $textBox.Location = New-Object System.Drawing.Point(10,40)
      $textBox.Size = New-Object System.Drawing.Size(260,20)
      $form.Controls.Add($textBox)

      $form.Topmost = $true

      $form.Add_Shown({$textBox.Select()})
      $result = $form.ShowDialog()

      if ($result -eq [System.Windows.Forms.DialogResult]::OK)
      {
      $VMPin = $textBox.Text
      $VMPin
      }


      Write-Host "Sending email to $StarterName@obscured.com" -ForegroundColor Green
      Write-Host "PhoneExt: $PhoneExt" -ForegroundColor Green
      Write-Host "Phone PIN: $PhonePIN" -ForegroundColor Green
      Write-Host "Voicemail PIN: $VMPin" -ForegroundColor Green

      Send-MailMessage -Credential $SMTPCred -To "$StarterName@obscured" -From
      $EmailFrom -Subject $EmailSubject -SmtpServer $SMTPServer -Body $EmailBody -BodyAsHtml






      powershell






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 9 hours ago









      David Shortall

      3115




      3115






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          4
          down vote













          Why do this is 4 dialog boxes vs a single GUI instance?



          What you are doing is really overkill for what you are after



          The reason you are getting the previous variable data in the ISE, is because you did not clear them before you used them again, they are still in memory.



          Populated variables do not auto clear. So, you have to explicitly empty them as well as exit, close garbage collect any stuff you instantiated or restart the ISE / dev environment which is just a do over of everything anyway.



          Whether you use a form or not, you should always clean up / clear delete stuff like that before it can be used again. This can happen with any programming language, so not a PS specific thing or ISE specific thing.



          All this sounds like you are new to PS GUI development or app dev in general. There are lots of videos on YouTube that cover PS GUI creation and useage with WPF and WinForms as well as lots of articles all over the web.



          You don't even need a customized form, unless you are doing some branding.
          You could just do this.



          Use a simplistic PS GUI (Using the Show-Command cmdlet) and input the info that when you click run, sends that to the Send-MailMessage cmdlet as a function. The only drawbacks here are, it's functional, not pretty, and you cannot order the params. Hence my branding point above.



          Example:



          function New-UserOnboardingEmail
          {
          param
          (
          [Parameter(Mandatory)]
          [string]$Username,

          [Parameter(Mandatory)]
          [string]$PhonePin,

          [Parameter(Mandatory)]
          [string]$VoicemailPin,

          [Parameter(Mandatory)]
          [string]$PhoneExt
          )


          $UserOnBoardDetails = "
          UserName : $Username`n
          PhonePin : $PhonePin`n
          VoiceMail : $VoicemailPin`n
          PhoneExt : $PhoneExt"

          Send-MailMessage `
          -From "$Admin@domain.com" `
          -To "$Username@domain.com" `
          -Subject 'Welcome to obscured + On-Boarding Details' `
          -Body: $UserOnBoardDetails `
          -SmtpServer $SmtpServer `
          -Encoding UTF8 `
          -Credential $Creds
          }

          Show-Command -Name New-UserOnboardingEmail


          As for doing this in one form, use https://poshgui.com, drag and drop form designer then just attach your code behind to make it take action.



          As for your code, you have to pass the value of the textbox entry to be able to use that on a button click event.



          Example using that online gui designer



          <# This form was created using POSHGUI.com  a free online gui designer for PowerShell
          .NAME
          Untitled
          #>

          Add-Type -AssemblyName System.Windows.Forms
          [System.Windows.Forms.Application]::EnableVisualStyles()

          #region begin GUI{

          $frmUserOnBoarding = New-Object system.Windows.Forms.Form
          $frmUserOnBoarding.ClientSize = '400,400'
          $frmUserOnBoarding.text = "New User On-boarding "
          $frmUserOnBoarding.TopMost = $false

          $lblUserName = New-Object system.Windows.Forms.Label
          $lblUserName.text = "UserName"
          $lblUserName.AutoSize = $true
          $lblUserName.width = 25
          $lblUserName.height = 10
          $lblUserName.location = New-Object System.Drawing.Point(17,22)
          $lblUserName.Font = 'Microsoft Sans Serif,10'

          $txtUserName = New-Object system.Windows.Forms.TextBox
          $txtUserName.multiline = $false
          $txtUserName.width = 100
          $txtUserName.height = 20
          $txtUserName.location = New-Object System.Drawing.Point(157,17)
          $txtUserName.Font = 'Microsoft Sans Serif,10'

          $lblPhonePin = New-Object system.Windows.Forms.Label
          $lblPhonePin.text = "PhonePin"
          $lblPhonePin.AutoSize = $true
          $lblPhonePin.width = 25
          $lblPhonePin.height = 10
          $lblPhonePin.location = New-Object System.Drawing.Point(17,60)
          $lblPhonePin.Font = 'Microsoft Sans Serif,10'

          $txtPhonePin = New-Object system.Windows.Forms.TextBox
          $txtPhonePin.multiline = $false
          $txtPhonePin.width = 100
          $txtPhonePin.height = 20
          $txtPhonePin.location = New-Object System.Drawing.Point(156,51)
          $txtPhonePin.Font = 'Microsoft Sans Serif,10'

          $lblVoicemailPin = New-Object system.Windows.Forms.Label
          $lblVoicemailPin.text = "VoiceMailPin"
          $lblVoicemailPin.AutoSize = $true
          $lblVoicemailPin.width = 25
          $lblVoicemailPin.height = 10
          $lblVoicemailPin.location = New-Object System.Drawing.Point(18,94)
          $lblVoicemailPin.Font = 'Microsoft Sans Serif,10'

          $txtVoicemailPin = New-Object system.Windows.Forms.TextBox
          $txtVoicemailPin.multiline = $false
          $txtVoicemailPin.width = 100
          $txtVoicemailPin.height = 20
          $txtVoicemailPin.location = New-Object System.Drawing.Point(157,88)
          $txtVoicemailPin.Font = 'Microsoft Sans Serif,10'

          $lblPhoneExt = New-Object system.Windows.Forms.Label
          $lblPhoneExt.text = "PhoneExt"
          $lblPhoneExt.AutoSize = $true
          $lblPhoneExt.width = 25
          $lblPhoneExt.height = 10
          $lblPhoneExt.location = New-Object System.Drawing.Point(20,126)
          $lblPhoneExt.Font = 'Microsoft Sans Serif,10'

          $txtPhoneExt = New-Object system.Windows.Forms.TextBox
          $txtPhoneExt.multiline = $false
          $txtPhoneExt.width = 100
          $txtPhoneExt.height = 20
          $txtPhoneExt.location = New-Object System.Drawing.Point(154,124)
          $txtPhoneExt.Font = 'Microsoft Sans Serif,10'

          $btnSubmit = New-Object system.Windows.Forms.Button
          $btnSubmit.text = "Submit"
          $btnSubmit.width = 60
          $btnSubmit.height = 30
          $btnSubmit.location = New-Object System.Drawing.Point(16,168)
          $btnSubmit.Font = 'Microsoft Sans Serif,10'

          $btnCancel = New-Object system.Windows.Forms.Button
          $btnCancel.text = "Cancel"
          $btnCancel.width = 60
          $btnCancel.height = 30
          $btnCancel.location = New-Object System.Drawing.Point(87,167)
          $btnCancel.Font = 'Microsoft Sans Serif,10'

          $frmUserOnBoarding.controls.AddRange(@($lblUserName,$txtUserName,$lblPhonePin,$txtPhonePin,$lblVoicemailPin,$txtVoicemailPin,$lblPhoneExt,$txtPhoneExt,$btnSubmit,$btnCancel))

          #region gui events {
          $btnSubmit.Add_Click({
          $UserName = $txtUserName.Text
          $PhonePin = $txtPhonePin.Text
          $VoicemailPin = $txtVoicemailPin.Text
          $PhoneExt = $txtPhoneExt.Text
          $frmUserOnBoarding.Close()})
          #endregion events }

          #endregion GUI }


          #Write your logic code here

          [void]$frmUserOnBoarding.ShowDialog()


          # Results from the submit button on the form
          $UserName
          $PhonePin
          $VoicemailPin
          $PhoneExt





          share|improve this answer






























            up vote
            1
            down vote













            I don't know Powershell, but I guess variables are interpolated into string when it is defined, not used.



            Move $EmailBody definition down, below data input code.



            Subsequent runs work, but with outdated values, because variables are persisted between executions by shell.






            share|improve this answer





















              Your Answer








              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "3"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














               

              draft saved


              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1374906%2fpowershell-email-with-text-input%23new-answer', 'question_page');
              }
              );

              Post as a guest
































              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              4
              down vote













              Why do this is 4 dialog boxes vs a single GUI instance?



              What you are doing is really overkill for what you are after



              The reason you are getting the previous variable data in the ISE, is because you did not clear them before you used them again, they are still in memory.



              Populated variables do not auto clear. So, you have to explicitly empty them as well as exit, close garbage collect any stuff you instantiated or restart the ISE / dev environment which is just a do over of everything anyway.



              Whether you use a form or not, you should always clean up / clear delete stuff like that before it can be used again. This can happen with any programming language, so not a PS specific thing or ISE specific thing.



              All this sounds like you are new to PS GUI development or app dev in general. There are lots of videos on YouTube that cover PS GUI creation and useage with WPF and WinForms as well as lots of articles all over the web.



              You don't even need a customized form, unless you are doing some branding.
              You could just do this.



              Use a simplistic PS GUI (Using the Show-Command cmdlet) and input the info that when you click run, sends that to the Send-MailMessage cmdlet as a function. The only drawbacks here are, it's functional, not pretty, and you cannot order the params. Hence my branding point above.



              Example:



              function New-UserOnboardingEmail
              {
              param
              (
              [Parameter(Mandatory)]
              [string]$Username,

              [Parameter(Mandatory)]
              [string]$PhonePin,

              [Parameter(Mandatory)]
              [string]$VoicemailPin,

              [Parameter(Mandatory)]
              [string]$PhoneExt
              )


              $UserOnBoardDetails = "
              UserName : $Username`n
              PhonePin : $PhonePin`n
              VoiceMail : $VoicemailPin`n
              PhoneExt : $PhoneExt"

              Send-MailMessage `
              -From "$Admin@domain.com" `
              -To "$Username@domain.com" `
              -Subject 'Welcome to obscured + On-Boarding Details' `
              -Body: $UserOnBoardDetails `
              -SmtpServer $SmtpServer `
              -Encoding UTF8 `
              -Credential $Creds
              }

              Show-Command -Name New-UserOnboardingEmail


              As for doing this in one form, use https://poshgui.com, drag and drop form designer then just attach your code behind to make it take action.



              As for your code, you have to pass the value of the textbox entry to be able to use that on a button click event.



              Example using that online gui designer



              <# This form was created using POSHGUI.com  a free online gui designer for PowerShell
              .NAME
              Untitled
              #>

              Add-Type -AssemblyName System.Windows.Forms
              [System.Windows.Forms.Application]::EnableVisualStyles()

              #region begin GUI{

              $frmUserOnBoarding = New-Object system.Windows.Forms.Form
              $frmUserOnBoarding.ClientSize = '400,400'
              $frmUserOnBoarding.text = "New User On-boarding "
              $frmUserOnBoarding.TopMost = $false

              $lblUserName = New-Object system.Windows.Forms.Label
              $lblUserName.text = "UserName"
              $lblUserName.AutoSize = $true
              $lblUserName.width = 25
              $lblUserName.height = 10
              $lblUserName.location = New-Object System.Drawing.Point(17,22)
              $lblUserName.Font = 'Microsoft Sans Serif,10'

              $txtUserName = New-Object system.Windows.Forms.TextBox
              $txtUserName.multiline = $false
              $txtUserName.width = 100
              $txtUserName.height = 20
              $txtUserName.location = New-Object System.Drawing.Point(157,17)
              $txtUserName.Font = 'Microsoft Sans Serif,10'

              $lblPhonePin = New-Object system.Windows.Forms.Label
              $lblPhonePin.text = "PhonePin"
              $lblPhonePin.AutoSize = $true
              $lblPhonePin.width = 25
              $lblPhonePin.height = 10
              $lblPhonePin.location = New-Object System.Drawing.Point(17,60)
              $lblPhonePin.Font = 'Microsoft Sans Serif,10'

              $txtPhonePin = New-Object system.Windows.Forms.TextBox
              $txtPhonePin.multiline = $false
              $txtPhonePin.width = 100
              $txtPhonePin.height = 20
              $txtPhonePin.location = New-Object System.Drawing.Point(156,51)
              $txtPhonePin.Font = 'Microsoft Sans Serif,10'

              $lblVoicemailPin = New-Object system.Windows.Forms.Label
              $lblVoicemailPin.text = "VoiceMailPin"
              $lblVoicemailPin.AutoSize = $true
              $lblVoicemailPin.width = 25
              $lblVoicemailPin.height = 10
              $lblVoicemailPin.location = New-Object System.Drawing.Point(18,94)
              $lblVoicemailPin.Font = 'Microsoft Sans Serif,10'

              $txtVoicemailPin = New-Object system.Windows.Forms.TextBox
              $txtVoicemailPin.multiline = $false
              $txtVoicemailPin.width = 100
              $txtVoicemailPin.height = 20
              $txtVoicemailPin.location = New-Object System.Drawing.Point(157,88)
              $txtVoicemailPin.Font = 'Microsoft Sans Serif,10'

              $lblPhoneExt = New-Object system.Windows.Forms.Label
              $lblPhoneExt.text = "PhoneExt"
              $lblPhoneExt.AutoSize = $true
              $lblPhoneExt.width = 25
              $lblPhoneExt.height = 10
              $lblPhoneExt.location = New-Object System.Drawing.Point(20,126)
              $lblPhoneExt.Font = 'Microsoft Sans Serif,10'

              $txtPhoneExt = New-Object system.Windows.Forms.TextBox
              $txtPhoneExt.multiline = $false
              $txtPhoneExt.width = 100
              $txtPhoneExt.height = 20
              $txtPhoneExt.location = New-Object System.Drawing.Point(154,124)
              $txtPhoneExt.Font = 'Microsoft Sans Serif,10'

              $btnSubmit = New-Object system.Windows.Forms.Button
              $btnSubmit.text = "Submit"
              $btnSubmit.width = 60
              $btnSubmit.height = 30
              $btnSubmit.location = New-Object System.Drawing.Point(16,168)
              $btnSubmit.Font = 'Microsoft Sans Serif,10'

              $btnCancel = New-Object system.Windows.Forms.Button
              $btnCancel.text = "Cancel"
              $btnCancel.width = 60
              $btnCancel.height = 30
              $btnCancel.location = New-Object System.Drawing.Point(87,167)
              $btnCancel.Font = 'Microsoft Sans Serif,10'

              $frmUserOnBoarding.controls.AddRange(@($lblUserName,$txtUserName,$lblPhonePin,$txtPhonePin,$lblVoicemailPin,$txtVoicemailPin,$lblPhoneExt,$txtPhoneExt,$btnSubmit,$btnCancel))

              #region gui events {
              $btnSubmit.Add_Click({
              $UserName = $txtUserName.Text
              $PhonePin = $txtPhonePin.Text
              $VoicemailPin = $txtVoicemailPin.Text
              $PhoneExt = $txtPhoneExt.Text
              $frmUserOnBoarding.Close()})
              #endregion events }

              #endregion GUI }


              #Write your logic code here

              [void]$frmUserOnBoarding.ShowDialog()


              # Results from the submit button on the form
              $UserName
              $PhonePin
              $VoicemailPin
              $PhoneExt





              share|improve this answer



























                up vote
                4
                down vote













                Why do this is 4 dialog boxes vs a single GUI instance?



                What you are doing is really overkill for what you are after



                The reason you are getting the previous variable data in the ISE, is because you did not clear them before you used them again, they are still in memory.



                Populated variables do not auto clear. So, you have to explicitly empty them as well as exit, close garbage collect any stuff you instantiated or restart the ISE / dev environment which is just a do over of everything anyway.



                Whether you use a form or not, you should always clean up / clear delete stuff like that before it can be used again. This can happen with any programming language, so not a PS specific thing or ISE specific thing.



                All this sounds like you are new to PS GUI development or app dev in general. There are lots of videos on YouTube that cover PS GUI creation and useage with WPF and WinForms as well as lots of articles all over the web.



                You don't even need a customized form, unless you are doing some branding.
                You could just do this.



                Use a simplistic PS GUI (Using the Show-Command cmdlet) and input the info that when you click run, sends that to the Send-MailMessage cmdlet as a function. The only drawbacks here are, it's functional, not pretty, and you cannot order the params. Hence my branding point above.



                Example:



                function New-UserOnboardingEmail
                {
                param
                (
                [Parameter(Mandatory)]
                [string]$Username,

                [Parameter(Mandatory)]
                [string]$PhonePin,

                [Parameter(Mandatory)]
                [string]$VoicemailPin,

                [Parameter(Mandatory)]
                [string]$PhoneExt
                )


                $UserOnBoardDetails = "
                UserName : $Username`n
                PhonePin : $PhonePin`n
                VoiceMail : $VoicemailPin`n
                PhoneExt : $PhoneExt"

                Send-MailMessage `
                -From "$Admin@domain.com" `
                -To "$Username@domain.com" `
                -Subject 'Welcome to obscured + On-Boarding Details' `
                -Body: $UserOnBoardDetails `
                -SmtpServer $SmtpServer `
                -Encoding UTF8 `
                -Credential $Creds
                }

                Show-Command -Name New-UserOnboardingEmail


                As for doing this in one form, use https://poshgui.com, drag and drop form designer then just attach your code behind to make it take action.



                As for your code, you have to pass the value of the textbox entry to be able to use that on a button click event.



                Example using that online gui designer



                <# This form was created using POSHGUI.com  a free online gui designer for PowerShell
                .NAME
                Untitled
                #>

                Add-Type -AssemblyName System.Windows.Forms
                [System.Windows.Forms.Application]::EnableVisualStyles()

                #region begin GUI{

                $frmUserOnBoarding = New-Object system.Windows.Forms.Form
                $frmUserOnBoarding.ClientSize = '400,400'
                $frmUserOnBoarding.text = "New User On-boarding "
                $frmUserOnBoarding.TopMost = $false

                $lblUserName = New-Object system.Windows.Forms.Label
                $lblUserName.text = "UserName"
                $lblUserName.AutoSize = $true
                $lblUserName.width = 25
                $lblUserName.height = 10
                $lblUserName.location = New-Object System.Drawing.Point(17,22)
                $lblUserName.Font = 'Microsoft Sans Serif,10'

                $txtUserName = New-Object system.Windows.Forms.TextBox
                $txtUserName.multiline = $false
                $txtUserName.width = 100
                $txtUserName.height = 20
                $txtUserName.location = New-Object System.Drawing.Point(157,17)
                $txtUserName.Font = 'Microsoft Sans Serif,10'

                $lblPhonePin = New-Object system.Windows.Forms.Label
                $lblPhonePin.text = "PhonePin"
                $lblPhonePin.AutoSize = $true
                $lblPhonePin.width = 25
                $lblPhonePin.height = 10
                $lblPhonePin.location = New-Object System.Drawing.Point(17,60)
                $lblPhonePin.Font = 'Microsoft Sans Serif,10'

                $txtPhonePin = New-Object system.Windows.Forms.TextBox
                $txtPhonePin.multiline = $false
                $txtPhonePin.width = 100
                $txtPhonePin.height = 20
                $txtPhonePin.location = New-Object System.Drawing.Point(156,51)
                $txtPhonePin.Font = 'Microsoft Sans Serif,10'

                $lblVoicemailPin = New-Object system.Windows.Forms.Label
                $lblVoicemailPin.text = "VoiceMailPin"
                $lblVoicemailPin.AutoSize = $true
                $lblVoicemailPin.width = 25
                $lblVoicemailPin.height = 10
                $lblVoicemailPin.location = New-Object System.Drawing.Point(18,94)
                $lblVoicemailPin.Font = 'Microsoft Sans Serif,10'

                $txtVoicemailPin = New-Object system.Windows.Forms.TextBox
                $txtVoicemailPin.multiline = $false
                $txtVoicemailPin.width = 100
                $txtVoicemailPin.height = 20
                $txtVoicemailPin.location = New-Object System.Drawing.Point(157,88)
                $txtVoicemailPin.Font = 'Microsoft Sans Serif,10'

                $lblPhoneExt = New-Object system.Windows.Forms.Label
                $lblPhoneExt.text = "PhoneExt"
                $lblPhoneExt.AutoSize = $true
                $lblPhoneExt.width = 25
                $lblPhoneExt.height = 10
                $lblPhoneExt.location = New-Object System.Drawing.Point(20,126)
                $lblPhoneExt.Font = 'Microsoft Sans Serif,10'

                $txtPhoneExt = New-Object system.Windows.Forms.TextBox
                $txtPhoneExt.multiline = $false
                $txtPhoneExt.width = 100
                $txtPhoneExt.height = 20
                $txtPhoneExt.location = New-Object System.Drawing.Point(154,124)
                $txtPhoneExt.Font = 'Microsoft Sans Serif,10'

                $btnSubmit = New-Object system.Windows.Forms.Button
                $btnSubmit.text = "Submit"
                $btnSubmit.width = 60
                $btnSubmit.height = 30
                $btnSubmit.location = New-Object System.Drawing.Point(16,168)
                $btnSubmit.Font = 'Microsoft Sans Serif,10'

                $btnCancel = New-Object system.Windows.Forms.Button
                $btnCancel.text = "Cancel"
                $btnCancel.width = 60
                $btnCancel.height = 30
                $btnCancel.location = New-Object System.Drawing.Point(87,167)
                $btnCancel.Font = 'Microsoft Sans Serif,10'

                $frmUserOnBoarding.controls.AddRange(@($lblUserName,$txtUserName,$lblPhonePin,$txtPhonePin,$lblVoicemailPin,$txtVoicemailPin,$lblPhoneExt,$txtPhoneExt,$btnSubmit,$btnCancel))

                #region gui events {
                $btnSubmit.Add_Click({
                $UserName = $txtUserName.Text
                $PhonePin = $txtPhonePin.Text
                $VoicemailPin = $txtVoicemailPin.Text
                $PhoneExt = $txtPhoneExt.Text
                $frmUserOnBoarding.Close()})
                #endregion events }

                #endregion GUI }


                #Write your logic code here

                [void]$frmUserOnBoarding.ShowDialog()


                # Results from the submit button on the form
                $UserName
                $PhonePin
                $VoicemailPin
                $PhoneExt





                share|improve this answer

























                  up vote
                  4
                  down vote










                  up vote
                  4
                  down vote









                  Why do this is 4 dialog boxes vs a single GUI instance?



                  What you are doing is really overkill for what you are after



                  The reason you are getting the previous variable data in the ISE, is because you did not clear them before you used them again, they are still in memory.



                  Populated variables do not auto clear. So, you have to explicitly empty them as well as exit, close garbage collect any stuff you instantiated or restart the ISE / dev environment which is just a do over of everything anyway.



                  Whether you use a form or not, you should always clean up / clear delete stuff like that before it can be used again. This can happen with any programming language, so not a PS specific thing or ISE specific thing.



                  All this sounds like you are new to PS GUI development or app dev in general. There are lots of videos on YouTube that cover PS GUI creation and useage with WPF and WinForms as well as lots of articles all over the web.



                  You don't even need a customized form, unless you are doing some branding.
                  You could just do this.



                  Use a simplistic PS GUI (Using the Show-Command cmdlet) and input the info that when you click run, sends that to the Send-MailMessage cmdlet as a function. The only drawbacks here are, it's functional, not pretty, and you cannot order the params. Hence my branding point above.



                  Example:



                  function New-UserOnboardingEmail
                  {
                  param
                  (
                  [Parameter(Mandatory)]
                  [string]$Username,

                  [Parameter(Mandatory)]
                  [string]$PhonePin,

                  [Parameter(Mandatory)]
                  [string]$VoicemailPin,

                  [Parameter(Mandatory)]
                  [string]$PhoneExt
                  )


                  $UserOnBoardDetails = "
                  UserName : $Username`n
                  PhonePin : $PhonePin`n
                  VoiceMail : $VoicemailPin`n
                  PhoneExt : $PhoneExt"

                  Send-MailMessage `
                  -From "$Admin@domain.com" `
                  -To "$Username@domain.com" `
                  -Subject 'Welcome to obscured + On-Boarding Details' `
                  -Body: $UserOnBoardDetails `
                  -SmtpServer $SmtpServer `
                  -Encoding UTF8 `
                  -Credential $Creds
                  }

                  Show-Command -Name New-UserOnboardingEmail


                  As for doing this in one form, use https://poshgui.com, drag and drop form designer then just attach your code behind to make it take action.



                  As for your code, you have to pass the value of the textbox entry to be able to use that on a button click event.



                  Example using that online gui designer



                  <# This form was created using POSHGUI.com  a free online gui designer for PowerShell
                  .NAME
                  Untitled
                  #>

                  Add-Type -AssemblyName System.Windows.Forms
                  [System.Windows.Forms.Application]::EnableVisualStyles()

                  #region begin GUI{

                  $frmUserOnBoarding = New-Object system.Windows.Forms.Form
                  $frmUserOnBoarding.ClientSize = '400,400'
                  $frmUserOnBoarding.text = "New User On-boarding "
                  $frmUserOnBoarding.TopMost = $false

                  $lblUserName = New-Object system.Windows.Forms.Label
                  $lblUserName.text = "UserName"
                  $lblUserName.AutoSize = $true
                  $lblUserName.width = 25
                  $lblUserName.height = 10
                  $lblUserName.location = New-Object System.Drawing.Point(17,22)
                  $lblUserName.Font = 'Microsoft Sans Serif,10'

                  $txtUserName = New-Object system.Windows.Forms.TextBox
                  $txtUserName.multiline = $false
                  $txtUserName.width = 100
                  $txtUserName.height = 20
                  $txtUserName.location = New-Object System.Drawing.Point(157,17)
                  $txtUserName.Font = 'Microsoft Sans Serif,10'

                  $lblPhonePin = New-Object system.Windows.Forms.Label
                  $lblPhonePin.text = "PhonePin"
                  $lblPhonePin.AutoSize = $true
                  $lblPhonePin.width = 25
                  $lblPhonePin.height = 10
                  $lblPhonePin.location = New-Object System.Drawing.Point(17,60)
                  $lblPhonePin.Font = 'Microsoft Sans Serif,10'

                  $txtPhonePin = New-Object system.Windows.Forms.TextBox
                  $txtPhonePin.multiline = $false
                  $txtPhonePin.width = 100
                  $txtPhonePin.height = 20
                  $txtPhonePin.location = New-Object System.Drawing.Point(156,51)
                  $txtPhonePin.Font = 'Microsoft Sans Serif,10'

                  $lblVoicemailPin = New-Object system.Windows.Forms.Label
                  $lblVoicemailPin.text = "VoiceMailPin"
                  $lblVoicemailPin.AutoSize = $true
                  $lblVoicemailPin.width = 25
                  $lblVoicemailPin.height = 10
                  $lblVoicemailPin.location = New-Object System.Drawing.Point(18,94)
                  $lblVoicemailPin.Font = 'Microsoft Sans Serif,10'

                  $txtVoicemailPin = New-Object system.Windows.Forms.TextBox
                  $txtVoicemailPin.multiline = $false
                  $txtVoicemailPin.width = 100
                  $txtVoicemailPin.height = 20
                  $txtVoicemailPin.location = New-Object System.Drawing.Point(157,88)
                  $txtVoicemailPin.Font = 'Microsoft Sans Serif,10'

                  $lblPhoneExt = New-Object system.Windows.Forms.Label
                  $lblPhoneExt.text = "PhoneExt"
                  $lblPhoneExt.AutoSize = $true
                  $lblPhoneExt.width = 25
                  $lblPhoneExt.height = 10
                  $lblPhoneExt.location = New-Object System.Drawing.Point(20,126)
                  $lblPhoneExt.Font = 'Microsoft Sans Serif,10'

                  $txtPhoneExt = New-Object system.Windows.Forms.TextBox
                  $txtPhoneExt.multiline = $false
                  $txtPhoneExt.width = 100
                  $txtPhoneExt.height = 20
                  $txtPhoneExt.location = New-Object System.Drawing.Point(154,124)
                  $txtPhoneExt.Font = 'Microsoft Sans Serif,10'

                  $btnSubmit = New-Object system.Windows.Forms.Button
                  $btnSubmit.text = "Submit"
                  $btnSubmit.width = 60
                  $btnSubmit.height = 30
                  $btnSubmit.location = New-Object System.Drawing.Point(16,168)
                  $btnSubmit.Font = 'Microsoft Sans Serif,10'

                  $btnCancel = New-Object system.Windows.Forms.Button
                  $btnCancel.text = "Cancel"
                  $btnCancel.width = 60
                  $btnCancel.height = 30
                  $btnCancel.location = New-Object System.Drawing.Point(87,167)
                  $btnCancel.Font = 'Microsoft Sans Serif,10'

                  $frmUserOnBoarding.controls.AddRange(@($lblUserName,$txtUserName,$lblPhonePin,$txtPhonePin,$lblVoicemailPin,$txtVoicemailPin,$lblPhoneExt,$txtPhoneExt,$btnSubmit,$btnCancel))

                  #region gui events {
                  $btnSubmit.Add_Click({
                  $UserName = $txtUserName.Text
                  $PhonePin = $txtPhonePin.Text
                  $VoicemailPin = $txtVoicemailPin.Text
                  $PhoneExt = $txtPhoneExt.Text
                  $frmUserOnBoarding.Close()})
                  #endregion events }

                  #endregion GUI }


                  #Write your logic code here

                  [void]$frmUserOnBoarding.ShowDialog()


                  # Results from the submit button on the form
                  $UserName
                  $PhonePin
                  $VoicemailPin
                  $PhoneExt





                  share|improve this answer














                  Why do this is 4 dialog boxes vs a single GUI instance?



                  What you are doing is really overkill for what you are after



                  The reason you are getting the previous variable data in the ISE, is because you did not clear them before you used them again, they are still in memory.



                  Populated variables do not auto clear. So, you have to explicitly empty them as well as exit, close garbage collect any stuff you instantiated or restart the ISE / dev environment which is just a do over of everything anyway.



                  Whether you use a form or not, you should always clean up / clear delete stuff like that before it can be used again. This can happen with any programming language, so not a PS specific thing or ISE specific thing.



                  All this sounds like you are new to PS GUI development or app dev in general. There are lots of videos on YouTube that cover PS GUI creation and useage with WPF and WinForms as well as lots of articles all over the web.



                  You don't even need a customized form, unless you are doing some branding.
                  You could just do this.



                  Use a simplistic PS GUI (Using the Show-Command cmdlet) and input the info that when you click run, sends that to the Send-MailMessage cmdlet as a function. The only drawbacks here are, it's functional, not pretty, and you cannot order the params. Hence my branding point above.



                  Example:



                  function New-UserOnboardingEmail
                  {
                  param
                  (
                  [Parameter(Mandatory)]
                  [string]$Username,

                  [Parameter(Mandatory)]
                  [string]$PhonePin,

                  [Parameter(Mandatory)]
                  [string]$VoicemailPin,

                  [Parameter(Mandatory)]
                  [string]$PhoneExt
                  )


                  $UserOnBoardDetails = "
                  UserName : $Username`n
                  PhonePin : $PhonePin`n
                  VoiceMail : $VoicemailPin`n
                  PhoneExt : $PhoneExt"

                  Send-MailMessage `
                  -From "$Admin@domain.com" `
                  -To "$Username@domain.com" `
                  -Subject 'Welcome to obscured + On-Boarding Details' `
                  -Body: $UserOnBoardDetails `
                  -SmtpServer $SmtpServer `
                  -Encoding UTF8 `
                  -Credential $Creds
                  }

                  Show-Command -Name New-UserOnboardingEmail


                  As for doing this in one form, use https://poshgui.com, drag and drop form designer then just attach your code behind to make it take action.



                  As for your code, you have to pass the value of the textbox entry to be able to use that on a button click event.



                  Example using that online gui designer



                  <# This form was created using POSHGUI.com  a free online gui designer for PowerShell
                  .NAME
                  Untitled
                  #>

                  Add-Type -AssemblyName System.Windows.Forms
                  [System.Windows.Forms.Application]::EnableVisualStyles()

                  #region begin GUI{

                  $frmUserOnBoarding = New-Object system.Windows.Forms.Form
                  $frmUserOnBoarding.ClientSize = '400,400'
                  $frmUserOnBoarding.text = "New User On-boarding "
                  $frmUserOnBoarding.TopMost = $false

                  $lblUserName = New-Object system.Windows.Forms.Label
                  $lblUserName.text = "UserName"
                  $lblUserName.AutoSize = $true
                  $lblUserName.width = 25
                  $lblUserName.height = 10
                  $lblUserName.location = New-Object System.Drawing.Point(17,22)
                  $lblUserName.Font = 'Microsoft Sans Serif,10'

                  $txtUserName = New-Object system.Windows.Forms.TextBox
                  $txtUserName.multiline = $false
                  $txtUserName.width = 100
                  $txtUserName.height = 20
                  $txtUserName.location = New-Object System.Drawing.Point(157,17)
                  $txtUserName.Font = 'Microsoft Sans Serif,10'

                  $lblPhonePin = New-Object system.Windows.Forms.Label
                  $lblPhonePin.text = "PhonePin"
                  $lblPhonePin.AutoSize = $true
                  $lblPhonePin.width = 25
                  $lblPhonePin.height = 10
                  $lblPhonePin.location = New-Object System.Drawing.Point(17,60)
                  $lblPhonePin.Font = 'Microsoft Sans Serif,10'

                  $txtPhonePin = New-Object system.Windows.Forms.TextBox
                  $txtPhonePin.multiline = $false
                  $txtPhonePin.width = 100
                  $txtPhonePin.height = 20
                  $txtPhonePin.location = New-Object System.Drawing.Point(156,51)
                  $txtPhonePin.Font = 'Microsoft Sans Serif,10'

                  $lblVoicemailPin = New-Object system.Windows.Forms.Label
                  $lblVoicemailPin.text = "VoiceMailPin"
                  $lblVoicemailPin.AutoSize = $true
                  $lblVoicemailPin.width = 25
                  $lblVoicemailPin.height = 10
                  $lblVoicemailPin.location = New-Object System.Drawing.Point(18,94)
                  $lblVoicemailPin.Font = 'Microsoft Sans Serif,10'

                  $txtVoicemailPin = New-Object system.Windows.Forms.TextBox
                  $txtVoicemailPin.multiline = $false
                  $txtVoicemailPin.width = 100
                  $txtVoicemailPin.height = 20
                  $txtVoicemailPin.location = New-Object System.Drawing.Point(157,88)
                  $txtVoicemailPin.Font = 'Microsoft Sans Serif,10'

                  $lblPhoneExt = New-Object system.Windows.Forms.Label
                  $lblPhoneExt.text = "PhoneExt"
                  $lblPhoneExt.AutoSize = $true
                  $lblPhoneExt.width = 25
                  $lblPhoneExt.height = 10
                  $lblPhoneExt.location = New-Object System.Drawing.Point(20,126)
                  $lblPhoneExt.Font = 'Microsoft Sans Serif,10'

                  $txtPhoneExt = New-Object system.Windows.Forms.TextBox
                  $txtPhoneExt.multiline = $false
                  $txtPhoneExt.width = 100
                  $txtPhoneExt.height = 20
                  $txtPhoneExt.location = New-Object System.Drawing.Point(154,124)
                  $txtPhoneExt.Font = 'Microsoft Sans Serif,10'

                  $btnSubmit = New-Object system.Windows.Forms.Button
                  $btnSubmit.text = "Submit"
                  $btnSubmit.width = 60
                  $btnSubmit.height = 30
                  $btnSubmit.location = New-Object System.Drawing.Point(16,168)
                  $btnSubmit.Font = 'Microsoft Sans Serif,10'

                  $btnCancel = New-Object system.Windows.Forms.Button
                  $btnCancel.text = "Cancel"
                  $btnCancel.width = 60
                  $btnCancel.height = 30
                  $btnCancel.location = New-Object System.Drawing.Point(87,167)
                  $btnCancel.Font = 'Microsoft Sans Serif,10'

                  $frmUserOnBoarding.controls.AddRange(@($lblUserName,$txtUserName,$lblPhonePin,$txtPhonePin,$lblVoicemailPin,$txtVoicemailPin,$lblPhoneExt,$txtPhoneExt,$btnSubmit,$btnCancel))

                  #region gui events {
                  $btnSubmit.Add_Click({
                  $UserName = $txtUserName.Text
                  $PhonePin = $txtPhonePin.Text
                  $VoicemailPin = $txtVoicemailPin.Text
                  $PhoneExt = $txtPhoneExt.Text
                  $frmUserOnBoarding.Close()})
                  #endregion events }

                  #endregion GUI }


                  #Write your logic code here

                  [void]$frmUserOnBoarding.ShowDialog()


                  # Results from the submit button on the form
                  $UserName
                  $PhonePin
                  $VoicemailPin
                  $PhoneExt






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 6 hours ago

























                  answered 7 hours ago









                  postanote

                  82513




                  82513
























                      up vote
                      1
                      down vote













                      I don't know Powershell, but I guess variables are interpolated into string when it is defined, not used.



                      Move $EmailBody definition down, below data input code.



                      Subsequent runs work, but with outdated values, because variables are persisted between executions by shell.






                      share|improve this answer

























                        up vote
                        1
                        down vote













                        I don't know Powershell, but I guess variables are interpolated into string when it is defined, not used.



                        Move $EmailBody definition down, below data input code.



                        Subsequent runs work, but with outdated values, because variables are persisted between executions by shell.






                        share|improve this answer























                          up vote
                          1
                          down vote










                          up vote
                          1
                          down vote









                          I don't know Powershell, but I guess variables are interpolated into string when it is defined, not used.



                          Move $EmailBody definition down, below data input code.



                          Subsequent runs work, but with outdated values, because variables are persisted between executions by shell.






                          share|improve this answer












                          I don't know Powershell, but I guess variables are interpolated into string when it is defined, not used.



                          Move $EmailBody definition down, below data input code.



                          Subsequent runs work, but with outdated values, because variables are persisted between executions by shell.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 4 hours ago









                          gronostaj

                          27.6k1368106




                          27.6k1368106






























                               

                              draft saved


                              draft discarded



















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1374906%2fpowershell-email-with-text-input%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest




















































































                              Popular posts from this blog

                              Список кардиналов, возведённых папой римским Каликстом III

                              Deduzione

                              Mysql.sock missing - “Can't connect to local MySQL server through socket”