Use script variable inside textbox form

AdiR 26 Reputation points
2021-12-07T09:26:31.093+00:00

Hello,

I’m trying to create a form which contains a message where I want to use some data imported from a CSV file.
The script will read the CSV rows and print a message that will contain data from CSV.

The issue is that the variables inside message textbox is used as text

Regards,
Adrian

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

function show_menu {
 [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
 $form = New-Object System.Windows.Forms.Form
 $form.Text = 'menu'
 $form.Size = New-Object System.Drawing.Size(630,370)
 $form.StartPosition = 'CenterScreen'
 $form.FormBorderStyle = 'FixedSingle'
 #$form.Icon = [System.Drawing.Icon]::FromHandle((New-Object System.Drawing.Bitmap -Argument $stream).GetHIcon())

  $okButton = New-Object System.Windows.Forms.Button
 $okButton.Location = New-Object System.Drawing.Point(200,295)
 $okButton.Size = New-Object System.Drawing.Size(75,23)
 $okButton.Text = 'OK'
 $okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
 $okButton.Add_Click({ 
     $form.Tag = $textBox_recipient.Text;
     $form.Tag = $textBox_message.Text;
     $form.Close() 
 })
 $form.AcceptButton = $okButton
 $form.Controls.Add($okButton)

 $cancelButton = New-Object System.Windows.Forms.Button
 $cancelButton.Location = New-Object System.Drawing.Point(355,295)
 $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)

 $textBox_recipient = New-Object System.Windows.Forms.TextBox
 $textBox_recipient.Location = New-Object System.Drawing.Point(210,70)
 $textBox_recipient.Size = New-Object System.Drawing.Size(245, 20)
 $textBox_recipient.ReadOnly = $true
 $form.Controls.Add($textBox_recipient)

 $textBox_recipient_select = New-Object System.Windows.Forms.Button
 $textBox_recipient_select.Location = New-Object System.Drawing.Point(460, 70)
 $textBox_recipient_select.Size = New-Object System.Drawing.Size(100, 20)
 $textBox_recipient_select.Text = "Select CSV file"
 $textBox_recipient_select.add_Click({
     $ofd = New-Object system.windows.forms.Openfiledialog
     #$ofd.Filter = 'Supported file types (*.csv,*.xlsx)|*.csv,*.xlsx'
     $ofd.Filter = 'Supported file types (*.csv, *.xlsx)|*.csv; *.xlsx| All (*.*)|*.*'
     $script:recipient_filename = 'Not found'
     if ($ofd.ShowDialog() -eq 'Ok') {
         $script:recipient_filename = $textbox_recipient.Text = $ofd.FileName
     }
 })
 #$textBox_recipient.Text = "C:\Users\Ady\Desktop\test.csv"
 $form.Controls.Add($textBox_recipient_select)

 $label_message = New-Object System.Windows.Forms.Label
 $label_message.Location = New-Object System.Drawing.Point(10,150)
 $label_message.Size = New-Object System.Drawing.Size(200,20)
 $label_message.BackColor = [System.Drawing.Color]::FromName("Transparent")
 $label_message.Font = [System.Drawing.Font]::new("Microsoft Sans Serif", 10, [System.Drawing.FontStyle]::Bold)
 $label_message.Text = 'Message:'
 $form.Controls.Add($label_message)

 $textBox_message = New-Object System.Windows.Forms.TextBox
 $textBox_message.Multiline = $True
 $textBox_message.Scrollbars = "Vertical"
 $textBox_message.Location = New-Object System.Drawing.Point(210,150)
 $textBox_message.Size = New-Object System.Drawing.Size(350, 135)
 $textBox_message.Text = "Insert your text here. HTML format supported"
 $form.Controls.Add($textBox_message)

  $form.Topmost = $true
  $form.Add_Shown({ $textBox_recipient.Select(),$textBox_message.Select() })
  $result = $form.ShowDialog()

    if ($result -eq [System.Windows.Forms.DialogResult]::OK) {
        $script:filename = "$(($textBox_recipient).Text)"

        $recipients = Import-csv -Path "$filename"
        $total_recipient_nr = get-content "$filename" | select-string "@" | measure-object -line
        $recipient_nr = 0
        foreach ($recipient in $recipients) {
         if (++$recipient_nr % 31 -eq 0) {
                Start-Sleep -Seconds 30
                 echo "waiting 1 minute"
           }

           $script:user_email = $recipient.email
           $script:user_firstname = $recipient.firstname
           $script:user_lastname = $recipient.lastname
           $script:user_code = $recipient.code
           $script:message = $textBox_message.Text

           Write-Host $message
         }
    }
}

show_menu
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,274 questions
{count} votes

Accepted answer
  1. DaveK 1,846 Reputation points
    2021-12-07T20:14:50.337+00:00

    Is it possible for you to change how you reference your variables in your message text?

    Instead of using - Hi $user_firstname. This is your code: $user_code
    could you use - Hi {0}. This is your code: {1}

    Then in the code use a -f to substitute your values as required?

    $script:message = $script:message -f $recipient.firstname,$recipient.code
    write-host $script:message
    

    I've done something very similar to this for a reminder email for workers who just use a mobile smart phone to generate email reminders 5 days before their AD password expired so I defined the email body in HTML in the code using {0}, {1} for placeholders for name etc then used the -f to substitute my data. The way it works is the order of the variables after -f is the order of the variables so the first in the list is {0), the 2nd in the list {1} etc.

    Would this work or do you specifically need to use the variable names in the text box?

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful