Unable to use variable in script

Jelle Kamma (NL) 116 Reputation points
2023-04-05T13:23:19.5333333+00:00

Hi there, I am trying to create a form for my powershell scripts. It is pretty straightforward. I want users to be able to fill in two text boxes. Those two input fields need t be turned into variables that I can continue to use in the rest of the script. However I cannot find any way to use the actual input that is given by the users Can someone guide me how I can get a usable variable from the text input fields?

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

$form = New-Object System.Windows.Forms.Form
$form.Text = 'VM Deployment script'
$form.Size = New-Object System.Drawing.Size(400,300)
$form.StartPosition = 'CenterScreen'

$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(75,200)
$okButton.Size = New-Object System.Drawing.Size(100,23)
$okButton.Text = 'Start deployment'
$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(200,200)
$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)

##########################Project number variables#################################
$label1 = New-Object System.Windows.Forms.Label
$label1.Location = New-Object System.Drawing.Point(10,20)
$label1.Size = New-Object System.Drawing.Size(300,20)
$label1.Text = 'Please enter the  Project number in the space below:'
$form.Controls.Add($label1)

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

##########################Project name variables#################################
$label2 = New-Object System.Windows.Forms.Label
$label2.Location = New-Object System.Drawing.Point(10,80)
$label2.Size = New-Object System.Drawing.Size(300,20)
$label2.Text = 'Please enter the Project name in the space below:'
$form.Controls.Add($label2)

$textBox2 = New-Object System.Windows.Forms.TextBox
$textBox2.Location = New-Object System.Drawing.Point(10,100)
$textBox2.Size = New-Object System.Drawing.Size(300,20)
$form.Controls.Add($textBox2)


$form.Topmost = $false

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

if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
Rest of the script with variables
}

PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,159 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 45,111 Reputation points
    2023-04-05T15:15:13.34+00:00

    It's pretty easy:

    if ($result -eq [System.Windows.Forms.DialogResult]::OK)
    {
        $tb1 = $textBox1.Text
        $tb2 = $textbox2.text
        Write-Host "textBox1 = $tb1"
        Write-Host "textBox2 = $tb2"
    }
    
    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful