Forum Discussion

Heldenbrand's avatar
Heldenbrand
Copper Contributor
Jan 18, 2022

Buttons and Event Handlers

I am trying to utilize Powershell Protools to create a windows form so that user input can be used to create an account in AD, I have the form, and want to test it. My issue is I cannot seem to find/figure out how to properly configure the buttons to do as they are labeled. See code below:

 

 

$Form1 = New-Object -TypeName System.Windows.Forms.Form
[System.Windows.Forms.DateTimePicker]$DateTimePicker1 = $null
[System.Windows.Forms.TextBox]$PHONE_NUMBER = $null
[System.Windows.Forms.TextBox]$USER_ID = $null
[System.Windows.Forms.TextBox]$RANK_STATUS = $null
[System.Windows.Forms.TextBox]$OFFICE = $null
[System.Windows.Forms.TextBox]$JOB_TITLE = $null
[System.Windows.Forms.TextBox]$EMAIL = $null
[System.Windows.Forms.TextBox]$LAST_NAME = $null
[System.Windows.Forms.TextBox]$USERNAME = $null
[System.Windows.Forms.TextBox]$MIDDLE_INITAL = $null
[System.Windows.Forms.TextBox]$FIRST_NAME = $null
function InitializeComponent
{
$DateTimePicker1 = (New-Object -TypeName System.Windows.Forms.DateTimePicker)
$PHONE_NUMBER = (New-Object -TypeName System.Windows.Forms.TextBox)
$USER_ID = (New-Object -TypeName System.Windows.Forms.TextBox)
$RANK_STATUS = (New-Object -TypeName System.Windows.Forms.TextBox)
$OFFICE = (New-Object -TypeName System.Windows.Forms.TextBox)
$JOB_TITLE = (New-Object -TypeName System.Windows.Forms.TextBox)
$EMAIL = (New-Object -TypeName System.Windows.Forms.TextBox)
$LAST_NAME = (New-Object -TypeName System.Windows.Forms.TextBox)
$USERNAME = (New-Object -TypeName System.Windows.Forms.TextBox)
$MIDDLE_INITAL = (New-Object -TypeName System.Windows.Forms.TextBox)
$FIRST_NAME = (New-Object -TypeName System.Windows.Forms.TextBox)
$Form1.SuspendLayout()
#
#DateTimePicker1
#
$DateTimePicker1.Location = (New-Object -TypeName System.Drawing.Point -ArgumentList @([System.Int32]128,[System.Int32]350))
$DateTimePicker1.Name = [System.String]'DateTimePicker1'
$DateTimePicker1.Size = (New-Object -TypeName System.Drawing.Size -ArgumentList @([System.Int32]163,[System.Int32]21))
$DateTimePicker1.TabIndex = [System.Int32]3
#
#PHONE_NUMBER
#
$PHONE_NUMBER.Location = (New-Object -TypeName System.Drawing.Point -ArgumentList @([System.Int32]212,[System.Int32]230))
$PHONE_NUMBER.Name = [System.String]'PHONE_NUMBER'
$PHONE_NUMBER.Size = (New-Object -TypeName System.Drawing.Size -ArgumentList @([System.Int32]161,[System.Int32]21))
$PHONE_NUMBER.TabIndex = [System.Int32]4
$PHONE_NUMBER.Text = [System.String]'Phone Number'
#
#USER_ID
#
$USER_ID.BackColor = [System.Drawing.SystemColors]::Window
$USER_ID.Font = (New-Object -TypeName System.Drawing.Font -ArgumentList @([System.String]'Tahoma',[System.Single]8.25))
$USER_ID.ForeColor = [System.Drawing.SystemColors]::WindowText
$USER_ID.ImeMode = [System.Windows.Forms.ImeMode]::NoControl
$USER_ID.Location = (New-Object -TypeName System.Drawing.Point -ArgumentList @([System.Int32]25,[System.Int32]230))
$USER_ID.Name = [System.String]'USER_ID'
$USER_ID.RightToLeft = [System.Windows.Forms.RightToLeft]::No
$USER_ID.Size = (New-Object -TypeName System.Drawing.Size -ArgumentList @([System.Int32]161,[System.Int32]21))
$USER_ID.TabIndex = [System.Int32]4
$USER_ID.Text = [System.String]'User ID'
#
#RANK_STATUS
#
$RANK_STATUS.BackColor = [System.Drawing.SystemColors]::Window
$RANK_STATUS.Font = (New-Object -TypeName System.Drawing.Font -ArgumentList @([System.String]'Tahoma',[System.Single]8.25))
$RANK_STATUS.ForeColor = [System.Drawing.SystemColors]::WindowText
$RANK_STATUS.ImeMode = [System.Windows.Forms.ImeMode]::NoControl
$RANK_STATUS.Location = (New-Object -TypeName System.Drawing.Point -ArgumentList @([System.Int32]212,[System.Int32]283))
$RANK_STATUS.Name = [System.String]'RANK_STATUS'
$RANK_STATUS.RightToLeft = [System.Windows.Forms.RightToLeft]::No
$RANK_STATUS.Size = (New-Object -TypeName System.Drawing.Size -ArgumentList @([System.Int32]161,[System.Int32]21))
$RANK_STATUS.TabIndex = [System.Int32]4
$RANK_STATUS.Text = [System.String]'Rank/Status CTR, CIV, NFG'
#
#OFFICE
#
$OFFICE.BackColor = [System.Drawing.SystemColors]::Window
$OFFICE.Font = (New-Object -TypeName System.Drawing.Font -ArgumentList @([System.String]'Tahoma',[System.Single]8.25))
$OFFICE.ForeColor = [System.Drawing.SystemColors]::WindowText
$OFFICE.ImeMode = [System.Windows.Forms.ImeMode]::NoControl
$OFFICE.Location = (New-Object -TypeName System.Drawing.Point -ArgumentList @([System.Int32]26,[System.Int32]283))
$OFFICE.Name = [System.String]'OFFICE'
$OFFICE.RightToLeft = [System.Windows.Forms.RightToLeft]::No
$OFFICE.Size = (New-Object -TypeName System.Drawing.Size -ArgumentList @([System.Int32]161,[System.Int32]21))
$OFFICE.TabIndex = [System.Int32]4
$OFFICE.Text = [System.String]'Office'
#
#JOB_TITLE
#
$JOB_TITLE.BackColor = [System.Drawing.SystemColors]::Window
$JOB_TITLE.Font = (New-Object -TypeName System.Drawing.Font -ArgumentList @([System.String]'Tahoma',[System.Single]8.25))
$JOB_TITLE.ForeColor = [System.Drawing.SystemColors]::WindowText
$JOB_TITLE.ImeMode = [System.Windows.Forms.ImeMode]::NoControl
$JOB_TITLE.Location = (New-Object -TypeName System.Drawing.Point -ArgumentList @([System.Int32]25,[System.Int32]173))
$JOB_TITLE.Name = [System.String]'JOB_TITLE'
$JOB_TITLE.RightToLeft = [System.Windows.Forms.RightToLeft]::No
$JOB_TITLE.Size = (New-Object -TypeName System.Drawing.Size -ArgumentList @([System.Int32]161,[System.Int32]21))
$JOB_TITLE.TabIndex = [System.Int32]4
$JOB_TITLE.Text = [System.String]'Job Title'
#
#EMAIL
#
$EMAIL.BackColor = [System.Drawing.SystemColors]::Window
$EMAIL.Font = (New-Object -TypeName System.Drawing.Font -ArgumentList @([System.String]'Tahoma',[System.Single]8.25))
$EMAIL.ForeColor = [System.Drawing.SystemColors]::WindowText
$EMAIL.ImeMode = [System.Windows.Forms.ImeMode]::NoControl
$EMAIL.Location = (New-Object -TypeName System.Drawing.Point -ArgumentList @([System.Int32]212,[System.Int32]173))
$EMAIL.Name = [System.String]'EMAIL'
$EMAIL.RightToLeft = [System.Windows.Forms.RightToLeft]::No
$EMAIL.Size = (New-Object -TypeName System.Drawing.Size -ArgumentList @([System.Int32]161,[System.Int32]21))
$EMAIL.TabIndex = [System.Int32]4
$EMAIL.Text = [System.String]'Email'
#
#LAST_NAME
#
$LAST_NAME.BackColor = [System.Drawing.SystemColors]::Window
$LAST_NAME.Font = (New-Object -TypeName System.Drawing.Font -ArgumentList @([System.String]'Tahoma',[System.Single]8.25))
$LAST_NAME.ForeColor = [System.Drawing.SystemColors]::WindowText
$LAST_NAME.ImeMode = [System.Windows.Forms.ImeMode]::NoControl
$LAST_NAME.Location = (New-Object -TypeName System.Drawing.Point -ArgumentList @([System.Int32]25,[System.Int32]118))
$LAST_NAME.Name = [System.String]'LAST_NAME'
$LAST_NAME.RightToLeft = [System.Windows.Forms.RightToLeft]::No
$LAST_NAME.Size = (New-Object -TypeName System.Drawing.Size -ArgumentList @([System.Int32]161,[System.Int32]21))
$LAST_NAME.TabIndex = [System.Int32]4
$LAST_NAME.Text = [System.String]'Last Name'
#
#USERNAME
#
$USERNAME.BackColor = [System.Drawing.SystemColors]::Window
$USERNAME.Font = (New-Object -TypeName System.Drawing.Font -ArgumentList @([System.String]'Tahoma',[System.Single]8.25))
$USERNAME.ForeColor = [System.Drawing.SystemColors]::WindowText
$USERNAME.ImeMode = [System.Windows.Forms.ImeMode]::NoControl
$USERNAME.Location = (New-Object -TypeName System.Drawing.Point -ArgumentList @([System.Int32]212,[System.Int32]118))
$USERNAME.Name = [System.String]'USERNAME'
$USERNAME.RightToLeft = [System.Windows.Forms.RightToLeft]::No
$USERNAME.Size = (New-Object -TypeName System.Drawing.Size -ArgumentList @([System.Int32]161,[System.Int32]21))
$USERNAME.TabIndex = [System.Int32]4
$USERNAME.Text = [System.String]'Username'
#
#MIDDLE_INITAL
#
$MIDDLE_INITAL.BackColor = [System.Drawing.SystemColors]::Window
$MIDDLE_INITAL.Font = (New-Object -TypeName System.Drawing.Font -ArgumentList @([System.String]'Tahoma',[System.Single]8.25))
$MIDDLE_INITAL.ForeColor = [System.Drawing.SystemColors]::WindowText
$MIDDLE_INITAL.ImeMode = [System.Windows.Forms.ImeMode]::NoControl
$MIDDLE_INITAL.Location = (New-Object -TypeName System.Drawing.Point -ArgumentList @([System.Int32]212,[System.Int32]61))
$MIDDLE_INITAL.Name = [System.String]'MIDDLE_INITAL'
$MIDDLE_INITAL.RightToLeft = [System.Windows.Forms.RightToLeft]::No
$MIDDLE_INITAL.Size = (New-Object -TypeName System.Drawing.Size -ArgumentList @([System.Int32]161,[System.Int32]21))
$MIDDLE_INITAL.TabIndex = [System.Int32]4
$MIDDLE_INITAL.Text = [System.String]'Middle Initial'
#
#FIRST_NAME
#
$FIRST_NAME.BackColor = [System.Drawing.SystemColors]::Window
$FIRST_NAME.Font = (New-Object -TypeName System.Drawing.Font -ArgumentList @([System.String]'Tahoma',[System.Single]8.25))
$FIRST_NAME.ForeColor = [System.Drawing.SystemColors]::WindowText
$FIRST_NAME.ImeMode = [System.Windows.Forms.ImeMode]::NoControl
$FIRST_NAME.Location = (New-Object -TypeName System.Drawing.Point -ArgumentList @([System.Int32]26,[System.Int32]61))
$FIRST_NAME.Name = [System.String]'FIRST_NAME'
$FIRST_NAME.RightToLeft = [System.Windows.Forms.RightToLeft]::No
$FIRST_NAME.Size = (New-Object -TypeName System.Drawing.Size -ArgumentList @([System.Int32]161,[System.Int32]21))
$FIRST_NAME.TabIndex = [System.Int32]4
$FIRST_NAME.Text = [System.String]'First Name'
#
#Form1
#
$Form1.ClientSize = (New-Object -TypeName System.Drawing.Size -ArgumentList @([System.Int32]430,[System.Int32]453))
$Form1.Controls.Add($PHONE_NUMBER)
$Form1.Controls.Add($DateTimePicker1)
$Form1.Controls.Add($USER_ID)
$Form1.Controls.Add($RANK_STATUS)
$Form1.Controls.Add($OFFICE)
$Form1.Controls.Add($JOB_TITLE)
$Form1.Controls.Add($EMAIL)
$Form1.Controls.Add($LAST_NAME)
$Form1.Controls.Add($USERNAME)
$Form1.Controls.Add($MIDDLE_INITAL)
$Form1.Controls.Add($FIRST_NAME)
$Form1.Text = [System.String]'Account Creation'
$Form1.ResumeLayout($false)
$Form1.PerformLayout()
Add-Member -InputObject $Form1 -Name base -Value $base -MemberType NoteProperty
Add-Member -InputObject $Form1 -Name DateTimePicker1 -Value $DateTimePicker1 -MemberType NoteProperty
Add-Member -InputObject $Form1 -Name PHONE_NUMBER -Value $PHONE_NUMBER -MemberType NoteProperty
Add-Member -InputObject $Form1 -Name USER_ID -Value $USER_ID -MemberType NoteProperty
Add-Member -InputObject $Form1 -Name RANK_STATUS -Value $RANK_STATUS -MemberType NoteProperty
Add-Member -InputObject $Form1 -Name OFFICE -Value $OFFICE -MemberType NoteProperty
Add-Member -InputObject $Form1 -Name JOB_TITLE -Value $JOB_TITLE -MemberType NoteProperty
Add-Member -InputObject $Form1 -Name EMAIL -Value $EMAIL -MemberType NoteProperty
Add-Member -InputObject $Form1 -Name LAST_NAME -Value $LAST_NAME -MemberType NoteProperty
Add-Member -InputObject $Form1 -Name USERNAME -Value $USERNAME -MemberType NoteProperty
Add-Member -InputObject $Form1 -Name MIDDLE_INITAL -Value $MIDDLE_INITAL -MemberType NoteProperty
Add-Member -InputObject $Form1 -Name FIRST_NAME -Value $FIRST_NAME -MemberType NoteProperty
}
. InitializeComponent

 

  • Heldenbrand's avatar
    Heldenbrand
    Copper Contributor

    Sorry, below is the script that references the script above, in case that makes any difference: 

    $Label8_Click = {
    }
    $CANCEL = {
    }
    $SUBMIT = {
    }
    Add-Type -AssemblyName System.Windows.Forms
    . (Join-Path $PSScriptRoot 'ad_forms.designer.ps1')
    $Form1.ShowDialog()

Resources