locked
PowerShell Script Form Not Working RRS feed

  • Question

  • Hi Everyone,

    I have had a look on these forums and the wider internet but cant find anything relating to my issue. I have a form that I use to gather information in order to create new users.

    However when I right click and run with PowerShell, I get a bunch of errors, seemingly for every object in the form. You can find my form code below, please let me know if there is a better way of creating forms for my scripts or if there is a simple fix for this.

    #First variables
    $global:orgunit = $null
    $global:locationnumber = $null
    $form = $null
    $result = $null
    
    
    $global:form = New-Object System.Windows.Forms.Form 
    $form.Text = "New User Creation Form"
    $form.Size = New-Object System.Drawing.Size(500,700) 
    $form.StartPosition = "CenterScreen"
    
    #OK BUTTON
    $global:OKButton = New-Object System.Windows.Forms.Button
    $OKButton.Location = New-Object System.Drawing.Point(175,630)
    $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)
    
    #CANCEL BUTTON
    $global:CancelButton = New-Object System.Windows.Forms.Button
    $CancelButton.Location = New-Object System.Drawing.Point(250,630)
    $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)
    
    
    
    #######DISPLAY NAME#######
    
    $locationnumber = 20
    
    #DISPLAY NAME HEADER
    $global:displaynamelabel = New-Object System.Windows.Forms.Label
    $displaynamelabel.Location = New-Object System.Drawing.Point(10,$locationnumber) 
    $displaynamelabel.Size = New-Object System.Drawing.Size(280,20) 
    $displaynamelabel.Text = "Display Name:"
    $form.Controls.Add($displaynamelabel) 
    
    #DISPLAY NAME INPUT
    $global:displayname = New-Object System.Windows.Forms.TextBox 
    $displayname.Location = New-Object System.Drawing.Point(10,($locationnumber + 20)) 
    $displayname.Size = New-Object System.Drawing.Size(460,20) 
    $form.Controls.Add($displayname) 
    
    
    #######FIRST NAME#######
    
    $locationnumber = 70
    
    #FIRST NAME LABEL
    $global:firstnamelabel = New-Object System.Windows.Forms.Label
    $firstnamelabel.Location = New-Object System.Drawing.Point(10,$locationnumber) 
    $firstnamelabel.Size = New-Object System.Drawing.Size(280,20) 
    $firstnamelabel.Text = "First Name:"
    $form.Controls.Add($firstnamelabel) 
    
    #FIRST NAME INPUT
    $global:firstname = New-Object System.Windows.Forms.TextBox 
    $firstname.Location = New-Object System.Drawing.Point(10,($locationnumber + 20)) 
    $firstname.Size = New-Object System.Drawing.Size(180,20) 
    $form.Controls.Add($firstname) 
    
    
    ########LAST NAME#######
    
    $locationnumber = 120
    
    #LAST NAME LABEL
    $global:lastnamelabel = New-Object System.Windows.Forms.Label
    $lastnamelabel.Location = New-Object System.Drawing.Point(10,$locationnumber) 
    $lastnamelabel.Size = New-Object System.Drawing.Size(280,20) 
    $lastnamelabel.Text = "Last Name:"
    $form.Controls.Add($lastnamelabel) 
    
    #LAST NAME INPUT
    $global:lastname = New-Object System.Windows.Forms.TextBox 
    $lastname.Location = New-Object System.Drawing.Point(10,($locationnumber + 20)) 
    $lastname.Size = New-Object System.Drawing.Size(180,20) 
    $form.Controls.Add($lastname) 
    
    
    
    #######UserPrincipalName#######
    
    $locationnumber = 170
    
    #USERPRINCNAMEHEADER
    $global:userprincipalnamelabel = New-Object System.Windows.Forms.Label
    $userprincipalnamelabel.Location = New-Object System.Drawing.Point(10,$locationnumber) 
    $userprincipalnamelabel.Size = New-Object System.Drawing.Size(280,20) 
    $userprincipalnamelabel.Text = "Logon Name:"
    $form.Controls.Add($userprincipalnamelabel) 
    
    #USERPRINCNAMEHEADER
    $global:userprincipalname = New-Object System.Windows.Forms.TextBox 
    $userprincipalname.Location = New-Object System.Drawing.Point(10,($locationnumber + 20)) 
    $userprincipalname.Size = New-Object System.Drawing.Size(180,20) 
    $form.Controls.Add($userprincipalname) 
    
    
    #######JOB TITLE#######
    
    $locationnumber = 220
    
    #JOB TITLE LABEL
    $global:jobtitlelabel = New-Object System.Windows.Forms.Label
    $jobtitlelabel.Location = New-Object System.Drawing.Point(10,$locationnumber) 
    $jobtitlelabel.Size = New-Object System.Drawing.Size(280,20) 
    $jobtitlelabel.Text = "Job Title:"
    $form.Controls.Add($jobtitlelabel) 
    
    #JOB TITLE 
    $global:jobtitle = New-Object System.Windows.Forms.TextBox 
    $jobtitle.Location = New-Object System.Drawing.Point(10,($locationnumber + 20)) 
    $jobtitle.Size = New-Object System.Drawing.Size(180,20) 
    $form.Controls.Add($jobtitle) 
    
    
    
    #######PASSWORD#######
    
    [string]$initialpassword = ([char[]](Get-Random -Input $(47..57 + 65..90 + 97..122) -Count 7)) + (Get-Random -Minimum 0 -Maximum 10)
    $passwordwithspacesremoved = $initialpassword.Replace(' ','')
    $finalpassword = $passwordwithspacesremoved
    $securepassword = ConvertTo-SecureString -AsPlainText $passwordwithspacesremoved -Force
    
    
    $locationnumber = 270
    
    #PASSWORD LABEL
    $global:PasswordLabel = New-Object System.Windows.Forms.Label
    $PasswordLabel.Location = New-Object System.Drawing.Point(10,$locationnumber) 
    $PasswordLabel.Size = New-Object System.Drawing.Size(280,20) 
    $PasswordLabel.Text = "Password (Make note of this!):"
    $form.Controls.Add($PasswordLabel) 
    
    #PASSWORD
    $global:Password = New-Object System.Windows.Forms.TextBox 
    $Password.Location = New-Object System.Drawing.Point(10,($locationnumber + 20)) 
    $Password.Size = New-Object System.Drawing.Size(180,20) 
    $password.Text = $finalpassword
    $form.Controls.Add($Password) 
    
    
    
    #######DESCRIPTION#######
    
    
    $locationnumber = 320
    
    #DESCRIPTION LABEL
    $global:DescriptionLabel = New-Object System.Windows.Forms.Label
    $DescriptionLabel.Location = New-Object System.Drawing.Point(10,$locationnumber) 
    $DescriptionLabel.Size = New-Object System.Drawing.Size(280,20) 
    $DescriptionLabel.Text = "Description (Same as Job Title):"
    $form.Controls.Add($DescriptionLabel) 
    
    #DESCRIPTION
    $global:Description = New-Object System.Windows.Forms.TextBox 
    $Description.Location = New-Object System.Drawing.Point(10,($locationnumber + 20)) 
    $Description.Size = New-Object System.Drawing.Size(460,20) 
    $form.Controls.Add($Description) 
    
    
    #######OFFICE#######
    
    
    $locationnumber = 370
    
    $global:officeHeader = New-Object System.Windows.Forms.Label
    $officeHeader.Location = New-Object System.Drawing.Point(10,$locationnumber) 
    $officeHeader.Size = New-Object System.Drawing.Size(280,20) 
    $officeHeader.Text = "Please Select an Office:"
    $form.Controls.Add($officeHeader)
    
    [array]$arrayOffices=@("Adelaide","Belfast","Birmingham","Bradford","Bristol","Coventry","Manchester","Nelson","Pembrokeshire","Rossendale","Sheffield","Sydney") 
    
    $global:officeComboBox = New-Object System.Windows.Forms.ComboBox 
    $officeComboBox.Location = New-Object System.Drawing.Size(10,($locationnumber + 20)) 
    $officeComboBox.Size = New-Object System.Drawing.Size(180,20) 
    $officeComboBox.DropDownHeight = 200 
    $Form.Controls.Add($officeComboBox) 
    
    foreach ($office in $arrayOffices) {
       $officeComboBox.Items.Add($office)
    }
    
    #######GROUP SETUP#######
    
    $locationnumber = 420
    
    #GROUP LABEL
    $global:GroupLabel = New-Object System.Windows.Forms.Label
    $GroupLabel.Location = New-Object System.Drawing.Point(10,$locationnumber) 
    $GroupLabel.Size = New-Object System.Drawing.Size(280,20) 
    $GroupLabel.Text = "Set Up The User Like This User (Full Name):"
    $form.Controls.Add($GroupLabel) 
    
    #GROUP
    $global:Group = New-Object System.Windows.Forms.TextBox 
    $Group.Location = New-Object System.Drawing.Point(10,($locationnumber + 20)) 
    $Group.Size = New-Object System.Drawing.Size(180,20) 
    $form.Controls.Add($Group) 
    $guideuser = $Group.Text
    
    
    #######OU SELECTION#######
    
    $locationnumber = 470
    
    $global:OUHeader = New-Object System.Windows.Forms.Label
    $OUHeader.Location = New-Object System.Drawing.Point(10,$locationnumber) 
    $OUHeader.Size = New-Object System.Drawing.Size(280,20) 
    $OUHeader.Text = "Please Select the an Organizational Unit:"
    $form.Controls.Add($OUHeader)
    
    [array]$arrayOU=@("Process Industries","Production & Print","Proteus","Mail Order","Multi-Channel Solutions(SMCSL)","One Iota","Priam","Wholesale & Distribution","Finance & Admin","Management","Marketing","Training")
    
    $global:OUComboBox = New-Object System.Windows.Forms.ComboBox 
    $OUComboBox.Location = New-Object System.Drawing.Size(10,($locationnumber + 20)) 
    $OUComboBox.Size = New-Object System.Drawing.Size(180,20) 
    $OUComboBox.DropDownHeight = 200 
    $Form.Controls.Add($OUComboBox) 
    
    foreach ($OU in $arrayOU) {
       $OUComboBox.Items.Add($OU)
    }
    
    
    switch ($OUComboBox){
        "Process Industries"{$OrgUnit = "sanderson.lan/Business Units/Manufacturing/Process Industries"}
        "Production & Print"{$OrgUnit = "sanderson.lan/Business Units/Manufacturing/Production & Print"}
        "Proteus"{$OrgUnit= "sanderson.lan/Business Units/Manufacturing/Proteus"}
        "Mail Order"{$OrgUnit = "sanderson.lan/Business Units/Multi-Channel/Mail Order"}
        "Multi-Channel Solutions(SMCSL)"{$OrgUnit = "sanderson.lan/Business Units/Multi-Channel/Multi-Channel Solutions (SMCSL)"}
        "One Iota"{$OrgUnit = "sanderson.lan/Business Units/Multi-Channel/One Iota"}
        "Priam"{$OrgUnit = "sanderson.lan/Business Units/Multi-Channel/Priam"}
        "Wholesale & Distribution"{$OrgUnit = "sanderson.lan/Business Units/Multi-Channel/Wholesale & Distribution"}
        "Finance & Admin"{$OrgUnit = "sanderson.lan/Business Units/Sanderson Group/Finance & Admin"}
        "Management"{$OrgUnit = "sanderson.lan/Business Units/Sanderson Group/Management"}
        "Marketing"{$OrgUnit = "sanderson.lan/Business Units/Sanderson Group/Marketing"}
        "Training"{$OrgUnit = "sanderson.lan/Business Units/Sanderson Group/Training"}
    }
    
    #######DATABASE SELECTION#######
    
    $locationnumber = 520
    
    $global:DataBaseHeader = New-Object System.Windows.Forms.Label
    $DataBaseHeader.Location = New-Object System.Drawing.Point(10,$locationnumber) 
    $DataBaseHeader.Size = New-Object System.Drawing.Size(280,20) 
    $DataBaseHeader.Text = "Please Select a mailbox database:"
    $form.Controls.Add($DataBaseHeader)
    
    [array]$arrayDataBase=@("User Mailboxes","Leavers")
    
    $global:DataBaseComboBox = New-Object System.Windows.Forms.ComboBox 
    $DataBaseComboBox.Location = New-Object System.Drawing.Size(10,($locationnumber + 20)) 
    $DataBaseComboBox.Size = New-Object System.Drawing.Size(180,20) 
    $DataBaseComboBox.DropDownHeight = 200 
    $Form.Controls.Add($DataBaseComboBox) 
    
    foreach ($Database in $arrayDataBase) {
       $DataBaseComboBox.Items.Add($Database)
    }
    
    #######RESET PASSWORD CHECKBOX#######
    $global:resetpasswordcheckbox = new-object System.Windows.Forms.checkbox
    $resetpasswordcheckbox.Location = new-object System.Drawing.Size(30,560)
    $resetpasswordcheckbox.Size = new-object System.Drawing.Size(200,45)
    $resetpasswordcheckbox.Text = "Reset Password Upon First Logon?"
    $resetpasswordcheckbox.Checked = $false
    $form.Controls.Add($resetpasswordcheckbox)  
    
    
    Clear-Host 
    
    if($ADInstallTester -eq $true){
        Write-Host "Active Directory module already installed" -ForegroundColor Yellow -BackgroundColor Black
    }elseif($ADInstallTester -eq $false){
        Write-Host "Installing Active Directory module" -ForegroundColor Yellow -BackgroundColor Black
    }
    
    
    
    #########################################################################################################################################################################
    ##CONSOLIDATING ALL VARIABLES##                                                                                                                                         #
    #########################################################################################################################################################################
    
    $form.Topmost = $True
    
    $form.Add_Shown({$displayname.Select()})
    $global:result = $form.ShowDialog()
    
    if ($result -eq [System.Windows.Forms.DialogResult]::OK)
    {
        #DONE
        $global:name = $displayname.Text
        $name
        #DONE
        $global:firstname = $firstname.Text
        $firstname
        #DONE
        $global:lastname = $lastname.Text
        $lastname
        #DONE
        $global:loginname = $userprincipalname.Text
        $loginname
        $global:samname = $loginname + "@sanderson.lan"
        #DONE
        $global:jobtitle = $jobtitle.Text
        $jobtitle
        #DONE
        $global:finalpassword
        #DONE
        $global:Description = $Description.Text
        $Description
        #DONE
        $global:office = $officeComboBox.Text
        $office
        #DONE
        $global:guideuser = $Group.Text
        $guideuser
        #DONE
        $global:OrganizationalUnit = $OUComboBox.Text
        $OrganizationalUnit
        #DONE
        $global:Database = $DataBaseComboBox.Text
        $Database
        #DONE
        $global:resetpassword = $resetpasswordcheckbox.Checked
        $resetpassword
    
    }

    Kind regards,

    Mark Harwood

    Thursday, July 6, 2017 10:51 AM

Answers

  • Works for me if I remove the line where you nullify the form and load the assembly.

    Add-Type -AssemblyName System.Windows.Forms
    #First variables
    $global:orgunit = $null
    $global:locationnumber = $null
    #$form = $null
    $result = $null
    
    
    $global:form = New-Object System.Windows.Forms.Form 
    $form.Text = "New User Creation Form"
    $form.Size = New-Object System.Drawing.Size(500,700) 
    $form.StartPosition = "CenterScreen"
    
    #OK BUTTON
    $global:OKButton = New-Object System.Windows.Forms.Button
    $OKButton.Location = New-Object System.Drawing.Point(175,630)
    $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)
    
    #CANCEL BUTTON
    $global:CancelButton = New-Object System.Windows.Forms.Button
    $CancelButton.Location = New-Object System.Drawing.Point(250,630)
    $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)
    
    
    
    #######DISPLAY NAME#######
    
    $locationnumber = 20
    
    #DISPLAY NAME HEADER
    $global:displaynamelabel = New-Object System.Windows.Forms.Label
    $displaynamelabel.Location = New-Object System.Drawing.Point(10,$locationnumber) 
    $displaynamelabel.Size = New-Object System.Drawing.Size(280,20) 
    $displaynamelabel.Text = "Display Name:"
    $form.Controls.Add($displaynamelabel) 
    
    #DISPLAY NAME INPUT
    $global:displayname = New-Object System.Windows.Forms.TextBox 
    $displayname.Location = New-Object System.Drawing.Point(10,($locationnumber + 20)) 
    $displayname.Size = New-Object System.Drawing.Size(460,20) 
    $form.Controls.Add($displayname) 
    
    
    #######FIRST NAME#######
    
    $locationnumber = 70
    
    #FIRST NAME LABEL
    $global:firstnamelabel = New-Object System.Windows.Forms.Label
    $firstnamelabel.Location = New-Object System.Drawing.Point(10,$locationnumber) 
    $firstnamelabel.Size = New-Object System.Drawing.Size(280,20) 
    $firstnamelabel.Text = "First Name:"
    $form.Controls.Add($firstnamelabel) 
    
    #FIRST NAME INPUT
    $global:firstname = New-Object System.Windows.Forms.TextBox 
    $firstname.Location = New-Object System.Drawing.Point(10,($locationnumber + 20)) 
    $firstname.Size = New-Object System.Drawing.Size(180,20) 
    $form.Controls.Add($firstname) 
    
    
    ########LAST NAME#######
    
    $locationnumber = 120
    
    #LAST NAME LABEL
    $global:lastnamelabel = New-Object System.Windows.Forms.Label
    $lastnamelabel.Location = New-Object System.Drawing.Point(10,$locationnumber) 
    $lastnamelabel.Size = New-Object System.Drawing.Size(280,20) 
    $lastnamelabel.Text = "Last Name:"
    $form.Controls.Add($lastnamelabel) 
    
    #LAST NAME INPUT
    $global:lastname = New-Object System.Windows.Forms.TextBox 
    $lastname.Location = New-Object System.Drawing.Point(10,($locationnumber + 20)) 
    $lastname.Size = New-Object System.Drawing.Size(180,20) 
    $form.Controls.Add($lastname) 
    
    
    
    #######UserPrincipalName#######
    
    $locationnumber = 170
    
    #USERPRINCNAMEHEADER
    $global:userprincipalnamelabel = New-Object System.Windows.Forms.Label
    $userprincipalnamelabel.Location = New-Object System.Drawing.Point(10,$locationnumber) 
    $userprincipalnamelabel.Size = New-Object System.Drawing.Size(280,20) 
    $userprincipalnamelabel.Text = "Logon Name:"
    $form.Controls.Add($userprincipalnamelabel) 
    
    #USERPRINCNAMEHEADER
    $global:userprincipalname = New-Object System.Windows.Forms.TextBox 
    $userprincipalname.Location = New-Object System.Drawing.Point(10,($locationnumber + 20)) 
    $userprincipalname.Size = New-Object System.Drawing.Size(180,20) 
    $form.Controls.Add($userprincipalname) 
    
    
    #######JOB TITLE#######
    
    $locationnumber = 220
    
    #JOB TITLE LABEL
    $global:jobtitlelabel = New-Object System.Windows.Forms.Label
    $jobtitlelabel.Location = New-Object System.Drawing.Point(10,$locationnumber) 
    $jobtitlelabel.Size = New-Object System.Drawing.Size(280,20) 
    $jobtitlelabel.Text = "Job Title:"
    $form.Controls.Add($jobtitlelabel) 
    
    #JOB TITLE 
    $global:jobtitle = New-Object System.Windows.Forms.TextBox 
    $jobtitle.Location = New-Object System.Drawing.Point(10,($locationnumber + 20)) 
    $jobtitle.Size = New-Object System.Drawing.Size(180,20) 
    $form.Controls.Add($jobtitle) 
    
    
    
    #######PASSWORD#######
    
    [string]$initialpassword = ([char[]](Get-Random -Input $(47..57 + 65..90 + 97..122) -Count 7)) + (Get-Random -Minimum 0 -Maximum 10)
    $passwordwithspacesremoved = $initialpassword.Replace(' ','')
    $finalpassword = $passwordwithspacesremoved
    $securepassword = ConvertTo-SecureString -AsPlainText $passwordwithspacesremoved -Force
    
    
    $locationnumber = 270
    
    #PASSWORD LABEL
    $global:PasswordLabel = New-Object System.Windows.Forms.Label
    $PasswordLabel.Location = New-Object System.Drawing.Point(10,$locationnumber) 
    $PasswordLabel.Size = New-Object System.Drawing.Size(280,20) 
    $PasswordLabel.Text = "Password (Make note of this!):"
    $form.Controls.Add($PasswordLabel) 
    
    #PASSWORD
    $global:Password = New-Object System.Windows.Forms.TextBox 
    $Password.Location = New-Object System.Drawing.Point(10,($locationnumber + 20)) 
    $Password.Size = New-Object System.Drawing.Size(180,20) 
    $password.Text = $finalpassword
    $form.Controls.Add($Password) 
    
    
    
    #######DESCRIPTION#######
    
    
    $locationnumber = 320
    
    #DESCRIPTION LABEL
    $global:DescriptionLabel = New-Object System.Windows.Forms.Label
    $DescriptionLabel.Location = New-Object System.Drawing.Point(10,$locationnumber) 
    $DescriptionLabel.Size = New-Object System.Drawing.Size(280,20) 
    $DescriptionLabel.Text = "Description (Same as Job Title):"
    $form.Controls.Add($DescriptionLabel) 
    
    #DESCRIPTION
    $global:Description = New-Object System.Windows.Forms.TextBox 
    $Description.Location = New-Object System.Drawing.Point(10,($locationnumber + 20)) 
    $Description.Size = New-Object System.Drawing.Size(460,20) 
    $form.Controls.Add($Description) 
    
    
    #######OFFICE#######
    
    
    $locationnumber = 370
    
    $global:officeHeader = New-Object System.Windows.Forms.Label
    $officeHeader.Location = New-Object System.Drawing.Point(10,$locationnumber) 
    $officeHeader.Size = New-Object System.Drawing.Size(280,20) 
    $officeHeader.Text = "Please Select an Office:"
    $form.Controls.Add($officeHeader)
    
    [array]$arrayOffices=@("Adelaide","Belfast","Birmingham","Bradford","Bristol","Coventry","Manchester","Nelson","Pembrokeshire","Rossendale","Sheffield","Sydney") 
    
    $global:officeComboBox = New-Object System.Windows.Forms.ComboBox 
    $officeComboBox.Location = New-Object System.Drawing.Size(10,($locationnumber + 20)) 
    $officeComboBox.Size = New-Object System.Drawing.Size(180,20) 
    $officeComboBox.DropDownHeight = 200 
    $Form.Controls.Add($officeComboBox) 
    
    foreach ($office in $arrayOffices) {
       $officeComboBox.Items.Add($office)
    }
    
    #######GROUP SETUP#######
    
    $locationnumber = 420
    
    #GROUP LABEL
    $global:GroupLabel = New-Object System.Windows.Forms.Label
    $GroupLabel.Location = New-Object System.Drawing.Point(10,$locationnumber) 
    $GroupLabel.Size = New-Object System.Drawing.Size(280,20) 
    $GroupLabel.Text = "Set Up The User Like This User (Full Name):"
    $form.Controls.Add($GroupLabel) 
    
    #GROUP
    $global:Group = New-Object System.Windows.Forms.TextBox 
    $Group.Location = New-Object System.Drawing.Point(10,($locationnumber + 20)) 
    $Group.Size = New-Object System.Drawing.Size(180,20) 
    $form.Controls.Add($Group) 
    $guideuser = $Group.Text
    
    
    #######OU SELECTION#######
    
    $locationnumber = 470
    
    $global:OUHeader = New-Object System.Windows.Forms.Label
    $OUHeader.Location = New-Object System.Drawing.Point(10,$locationnumber) 
    $OUHeader.Size = New-Object System.Drawing.Size(280,20) 
    $OUHeader.Text = "Please Select the an Organizational Unit:"
    $form.Controls.Add($OUHeader)
    
    [array]$arrayOU=@("Process Industries","Production & Print","Proteus","Mail Order","Multi-Channel Solutions(SMCSL)","One Iota","Priam","Wholesale & Distribution","Finance & Admin","Management","Marketing","Training")
    
    $global:OUComboBox = New-Object System.Windows.Forms.ComboBox 
    $OUComboBox.Location = New-Object System.Drawing.Size(10,($locationnumber + 20)) 
    $OUComboBox.Size = New-Object System.Drawing.Size(180,20) 
    $OUComboBox.DropDownHeight = 200 
    $Form.Controls.Add($OUComboBox) 
    
    foreach ($OU in $arrayOU) {
       $OUComboBox.Items.Add($OU)
    }
    
    
    switch ($OUComboBox){
        "Process Industries"{$OrgUnit = "sanderson.lan/Business Units/Manufacturing/Process Industries"}
        "Production & Print"{$OrgUnit = "sanderson.lan/Business Units/Manufacturing/Production & Print"}
        "Proteus"{$OrgUnit= "sanderson.lan/Business Units/Manufacturing/Proteus"}
        "Mail Order"{$OrgUnit = "sanderson.lan/Business Units/Multi-Channel/Mail Order"}
        "Multi-Channel Solutions(SMCSL)"{$OrgUnit = "sanderson.lan/Business Units/Multi-Channel/Multi-Channel Solutions (SMCSL)"}
        "One Iota"{$OrgUnit = "sanderson.lan/Business Units/Multi-Channel/One Iota"}
        "Priam"{$OrgUnit = "sanderson.lan/Business Units/Multi-Channel/Priam"}
        "Wholesale & Distribution"{$OrgUnit = "sanderson.lan/Business Units/Multi-Channel/Wholesale & Distribution"}
        "Finance & Admin"{$OrgUnit = "sanderson.lan/Business Units/Sanderson Group/Finance & Admin"}
        "Management"{$OrgUnit = "sanderson.lan/Business Units/Sanderson Group/Management"}
        "Marketing"{$OrgUnit = "sanderson.lan/Business Units/Sanderson Group/Marketing"}
        "Training"{$OrgUnit = "sanderson.lan/Business Units/Sanderson Group/Training"}
    }
    
    #######DATABASE SELECTION#######
    
    $locationnumber = 520
    
    $global:DataBaseHeader = New-Object System.Windows.Forms.Label
    $DataBaseHeader.Location = New-Object System.Drawing.Point(10,$locationnumber) 
    $DataBaseHeader.Size = New-Object System.Drawing.Size(280,20) 
    $DataBaseHeader.Text = "Please Select a mailbox database:"
    $form.Controls.Add($DataBaseHeader)
    
    [array]$arrayDataBase=@("User Mailboxes","Leavers")
    
    $global:DataBaseComboBox = New-Object System.Windows.Forms.ComboBox 
    $DataBaseComboBox.Location = New-Object System.Drawing.Size(10,($locationnumber + 20)) 
    $DataBaseComboBox.Size = New-Object System.Drawing.Size(180,20) 
    $DataBaseComboBox.DropDownHeight = 200 
    $Form.Controls.Add($DataBaseComboBox) 
    
    foreach ($Database in $arrayDataBase) {
       $DataBaseComboBox.Items.Add($Database)
    }
    
    #######RESET PASSWORD CHECKBOX#######
    $global:resetpasswordcheckbox = new-object System.Windows.Forms.checkbox
    $resetpasswordcheckbox.Location = new-object System.Drawing.Size(30,560)
    $resetpasswordcheckbox.Size = new-object System.Drawing.Size(200,45)
    $resetpasswordcheckbox.Text = "Reset Password Upon First Logon?"
    $resetpasswordcheckbox.Checked = $false
    $form.Controls.Add($resetpasswordcheckbox)  
    
    
    Clear-Host 
    
    if($ADInstallTester -eq $true){
        Write-Host "Active Directory module already installed" -ForegroundColor Yellow -BackgroundColor Black
    }elseif($ADInstallTester -eq $false){
        Write-Host "Installing Active Directory module" -ForegroundColor Yellow -BackgroundColor Black
    }
    
    
    
    #########################################################################################################################################################################
    ##CONSOLIDATING ALL VARIABLES##                                                                                                                                         #
    #########################################################################################################################################################################
    
    $form.Topmost = $True
    
    $form.Add_Shown({$displayname.Select()})
    $global:result = $form.ShowDialog()
    
    if ($result -eq [System.Windows.Forms.DialogResult]::OK)
    {
        #DONE
        $global:name = $displayname.Text
        $name
        #DONE
        $global:firstname = $firstname.Text
        $firstname
        #DONE
        $global:lastname = $lastname.Text
        $lastname
        #DONE
        $global:loginname = $userprincipalname.Text
        $loginname
        $global:samname = $loginname + "@sanderson.lan"
        #DONE
        $global:jobtitle = $jobtitle.Text
        $jobtitle
        #DONE
        $global:finalpassword
        #DONE
        $global:Description = $Description.Text
        $Description
        #DONE
        $global:office = $officeComboBox.Text
        $office
        #DONE
        $global:guideuser = $Group.Text
        $guideuser
        #DONE
        $global:OrganizationalUnit = $OUComboBox.Text
        $OrganizationalUnit
        #DONE
        $global:Database = $DataBaseComboBox.Text
        $Database
        #DONE
        $global:resetpassword = $resetpasswordcheckbox.Checked
        $resetpassword
    
    }


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    Thursday, July 6, 2017 11:18 AM

All replies

  • Works for me if I remove the line where you nullify the form and load the assembly.

    Add-Type -AssemblyName System.Windows.Forms
    #First variables
    $global:orgunit = $null
    $global:locationnumber = $null
    #$form = $null
    $result = $null
    
    
    $global:form = New-Object System.Windows.Forms.Form 
    $form.Text = "New User Creation Form"
    $form.Size = New-Object System.Drawing.Size(500,700) 
    $form.StartPosition = "CenterScreen"
    
    #OK BUTTON
    $global:OKButton = New-Object System.Windows.Forms.Button
    $OKButton.Location = New-Object System.Drawing.Point(175,630)
    $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)
    
    #CANCEL BUTTON
    $global:CancelButton = New-Object System.Windows.Forms.Button
    $CancelButton.Location = New-Object System.Drawing.Point(250,630)
    $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)
    
    
    
    #######DISPLAY NAME#######
    
    $locationnumber = 20
    
    #DISPLAY NAME HEADER
    $global:displaynamelabel = New-Object System.Windows.Forms.Label
    $displaynamelabel.Location = New-Object System.Drawing.Point(10,$locationnumber) 
    $displaynamelabel.Size = New-Object System.Drawing.Size(280,20) 
    $displaynamelabel.Text = "Display Name:"
    $form.Controls.Add($displaynamelabel) 
    
    #DISPLAY NAME INPUT
    $global:displayname = New-Object System.Windows.Forms.TextBox 
    $displayname.Location = New-Object System.Drawing.Point(10,($locationnumber + 20)) 
    $displayname.Size = New-Object System.Drawing.Size(460,20) 
    $form.Controls.Add($displayname) 
    
    
    #######FIRST NAME#######
    
    $locationnumber = 70
    
    #FIRST NAME LABEL
    $global:firstnamelabel = New-Object System.Windows.Forms.Label
    $firstnamelabel.Location = New-Object System.Drawing.Point(10,$locationnumber) 
    $firstnamelabel.Size = New-Object System.Drawing.Size(280,20) 
    $firstnamelabel.Text = "First Name:"
    $form.Controls.Add($firstnamelabel) 
    
    #FIRST NAME INPUT
    $global:firstname = New-Object System.Windows.Forms.TextBox 
    $firstname.Location = New-Object System.Drawing.Point(10,($locationnumber + 20)) 
    $firstname.Size = New-Object System.Drawing.Size(180,20) 
    $form.Controls.Add($firstname) 
    
    
    ########LAST NAME#######
    
    $locationnumber = 120
    
    #LAST NAME LABEL
    $global:lastnamelabel = New-Object System.Windows.Forms.Label
    $lastnamelabel.Location = New-Object System.Drawing.Point(10,$locationnumber) 
    $lastnamelabel.Size = New-Object System.Drawing.Size(280,20) 
    $lastnamelabel.Text = "Last Name:"
    $form.Controls.Add($lastnamelabel) 
    
    #LAST NAME INPUT
    $global:lastname = New-Object System.Windows.Forms.TextBox 
    $lastname.Location = New-Object System.Drawing.Point(10,($locationnumber + 20)) 
    $lastname.Size = New-Object System.Drawing.Size(180,20) 
    $form.Controls.Add($lastname) 
    
    
    
    #######UserPrincipalName#######
    
    $locationnumber = 170
    
    #USERPRINCNAMEHEADER
    $global:userprincipalnamelabel = New-Object System.Windows.Forms.Label
    $userprincipalnamelabel.Location = New-Object System.Drawing.Point(10,$locationnumber) 
    $userprincipalnamelabel.Size = New-Object System.Drawing.Size(280,20) 
    $userprincipalnamelabel.Text = "Logon Name:"
    $form.Controls.Add($userprincipalnamelabel) 
    
    #USERPRINCNAMEHEADER
    $global:userprincipalname = New-Object System.Windows.Forms.TextBox 
    $userprincipalname.Location = New-Object System.Drawing.Point(10,($locationnumber + 20)) 
    $userprincipalname.Size = New-Object System.Drawing.Size(180,20) 
    $form.Controls.Add($userprincipalname) 
    
    
    #######JOB TITLE#######
    
    $locationnumber = 220
    
    #JOB TITLE LABEL
    $global:jobtitlelabel = New-Object System.Windows.Forms.Label
    $jobtitlelabel.Location = New-Object System.Drawing.Point(10,$locationnumber) 
    $jobtitlelabel.Size = New-Object System.Drawing.Size(280,20) 
    $jobtitlelabel.Text = "Job Title:"
    $form.Controls.Add($jobtitlelabel) 
    
    #JOB TITLE 
    $global:jobtitle = New-Object System.Windows.Forms.TextBox 
    $jobtitle.Location = New-Object System.Drawing.Point(10,($locationnumber + 20)) 
    $jobtitle.Size = New-Object System.Drawing.Size(180,20) 
    $form.Controls.Add($jobtitle) 
    
    
    
    #######PASSWORD#######
    
    [string]$initialpassword = ([char[]](Get-Random -Input $(47..57 + 65..90 + 97..122) -Count 7)) + (Get-Random -Minimum 0 -Maximum 10)
    $passwordwithspacesremoved = $initialpassword.Replace(' ','')
    $finalpassword = $passwordwithspacesremoved
    $securepassword = ConvertTo-SecureString -AsPlainText $passwordwithspacesremoved -Force
    
    
    $locationnumber = 270
    
    #PASSWORD LABEL
    $global:PasswordLabel = New-Object System.Windows.Forms.Label
    $PasswordLabel.Location = New-Object System.Drawing.Point(10,$locationnumber) 
    $PasswordLabel.Size = New-Object System.Drawing.Size(280,20) 
    $PasswordLabel.Text = "Password (Make note of this!):"
    $form.Controls.Add($PasswordLabel) 
    
    #PASSWORD
    $global:Password = New-Object System.Windows.Forms.TextBox 
    $Password.Location = New-Object System.Drawing.Point(10,($locationnumber + 20)) 
    $Password.Size = New-Object System.Drawing.Size(180,20) 
    $password.Text = $finalpassword
    $form.Controls.Add($Password) 
    
    
    
    #######DESCRIPTION#######
    
    
    $locationnumber = 320
    
    #DESCRIPTION LABEL
    $global:DescriptionLabel = New-Object System.Windows.Forms.Label
    $DescriptionLabel.Location = New-Object System.Drawing.Point(10,$locationnumber) 
    $DescriptionLabel.Size = New-Object System.Drawing.Size(280,20) 
    $DescriptionLabel.Text = "Description (Same as Job Title):"
    $form.Controls.Add($DescriptionLabel) 
    
    #DESCRIPTION
    $global:Description = New-Object System.Windows.Forms.TextBox 
    $Description.Location = New-Object System.Drawing.Point(10,($locationnumber + 20)) 
    $Description.Size = New-Object System.Drawing.Size(460,20) 
    $form.Controls.Add($Description) 
    
    
    #######OFFICE#######
    
    
    $locationnumber = 370
    
    $global:officeHeader = New-Object System.Windows.Forms.Label
    $officeHeader.Location = New-Object System.Drawing.Point(10,$locationnumber) 
    $officeHeader.Size = New-Object System.Drawing.Size(280,20) 
    $officeHeader.Text = "Please Select an Office:"
    $form.Controls.Add($officeHeader)
    
    [array]$arrayOffices=@("Adelaide","Belfast","Birmingham","Bradford","Bristol","Coventry","Manchester","Nelson","Pembrokeshire","Rossendale","Sheffield","Sydney") 
    
    $global:officeComboBox = New-Object System.Windows.Forms.ComboBox 
    $officeComboBox.Location = New-Object System.Drawing.Size(10,($locationnumber + 20)) 
    $officeComboBox.Size = New-Object System.Drawing.Size(180,20) 
    $officeComboBox.DropDownHeight = 200 
    $Form.Controls.Add($officeComboBox) 
    
    foreach ($office in $arrayOffices) {
       $officeComboBox.Items.Add($office)
    }
    
    #######GROUP SETUP#######
    
    $locationnumber = 420
    
    #GROUP LABEL
    $global:GroupLabel = New-Object System.Windows.Forms.Label
    $GroupLabel.Location = New-Object System.Drawing.Point(10,$locationnumber) 
    $GroupLabel.Size = New-Object System.Drawing.Size(280,20) 
    $GroupLabel.Text = "Set Up The User Like This User (Full Name):"
    $form.Controls.Add($GroupLabel) 
    
    #GROUP
    $global:Group = New-Object System.Windows.Forms.TextBox 
    $Group.Location = New-Object System.Drawing.Point(10,($locationnumber + 20)) 
    $Group.Size = New-Object System.Drawing.Size(180,20) 
    $form.Controls.Add($Group) 
    $guideuser = $Group.Text
    
    
    #######OU SELECTION#######
    
    $locationnumber = 470
    
    $global:OUHeader = New-Object System.Windows.Forms.Label
    $OUHeader.Location = New-Object System.Drawing.Point(10,$locationnumber) 
    $OUHeader.Size = New-Object System.Drawing.Size(280,20) 
    $OUHeader.Text = "Please Select the an Organizational Unit:"
    $form.Controls.Add($OUHeader)
    
    [array]$arrayOU=@("Process Industries","Production & Print","Proteus","Mail Order","Multi-Channel Solutions(SMCSL)","One Iota","Priam","Wholesale & Distribution","Finance & Admin","Management","Marketing","Training")
    
    $global:OUComboBox = New-Object System.Windows.Forms.ComboBox 
    $OUComboBox.Location = New-Object System.Drawing.Size(10,($locationnumber + 20)) 
    $OUComboBox.Size = New-Object System.Drawing.Size(180,20) 
    $OUComboBox.DropDownHeight = 200 
    $Form.Controls.Add($OUComboBox) 
    
    foreach ($OU in $arrayOU) {
       $OUComboBox.Items.Add($OU)
    }
    
    
    switch ($OUComboBox){
        "Process Industries"{$OrgUnit = "sanderson.lan/Business Units/Manufacturing/Process Industries"}
        "Production & Print"{$OrgUnit = "sanderson.lan/Business Units/Manufacturing/Production & Print"}
        "Proteus"{$OrgUnit= "sanderson.lan/Business Units/Manufacturing/Proteus"}
        "Mail Order"{$OrgUnit = "sanderson.lan/Business Units/Multi-Channel/Mail Order"}
        "Multi-Channel Solutions(SMCSL)"{$OrgUnit = "sanderson.lan/Business Units/Multi-Channel/Multi-Channel Solutions (SMCSL)"}
        "One Iota"{$OrgUnit = "sanderson.lan/Business Units/Multi-Channel/One Iota"}
        "Priam"{$OrgUnit = "sanderson.lan/Business Units/Multi-Channel/Priam"}
        "Wholesale & Distribution"{$OrgUnit = "sanderson.lan/Business Units/Multi-Channel/Wholesale & Distribution"}
        "Finance & Admin"{$OrgUnit = "sanderson.lan/Business Units/Sanderson Group/Finance & Admin"}
        "Management"{$OrgUnit = "sanderson.lan/Business Units/Sanderson Group/Management"}
        "Marketing"{$OrgUnit = "sanderson.lan/Business Units/Sanderson Group/Marketing"}
        "Training"{$OrgUnit = "sanderson.lan/Business Units/Sanderson Group/Training"}
    }
    
    #######DATABASE SELECTION#######
    
    $locationnumber = 520
    
    $global:DataBaseHeader = New-Object System.Windows.Forms.Label
    $DataBaseHeader.Location = New-Object System.Drawing.Point(10,$locationnumber) 
    $DataBaseHeader.Size = New-Object System.Drawing.Size(280,20) 
    $DataBaseHeader.Text = "Please Select a mailbox database:"
    $form.Controls.Add($DataBaseHeader)
    
    [array]$arrayDataBase=@("User Mailboxes","Leavers")
    
    $global:DataBaseComboBox = New-Object System.Windows.Forms.ComboBox 
    $DataBaseComboBox.Location = New-Object System.Drawing.Size(10,($locationnumber + 20)) 
    $DataBaseComboBox.Size = New-Object System.Drawing.Size(180,20) 
    $DataBaseComboBox.DropDownHeight = 200 
    $Form.Controls.Add($DataBaseComboBox) 
    
    foreach ($Database in $arrayDataBase) {
       $DataBaseComboBox.Items.Add($Database)
    }
    
    #######RESET PASSWORD CHECKBOX#######
    $global:resetpasswordcheckbox = new-object System.Windows.Forms.checkbox
    $resetpasswordcheckbox.Location = new-object System.Drawing.Size(30,560)
    $resetpasswordcheckbox.Size = new-object System.Drawing.Size(200,45)
    $resetpasswordcheckbox.Text = "Reset Password Upon First Logon?"
    $resetpasswordcheckbox.Checked = $false
    $form.Controls.Add($resetpasswordcheckbox)  
    
    
    Clear-Host 
    
    if($ADInstallTester -eq $true){
        Write-Host "Active Directory module already installed" -ForegroundColor Yellow -BackgroundColor Black
    }elseif($ADInstallTester -eq $false){
        Write-Host "Installing Active Directory module" -ForegroundColor Yellow -BackgroundColor Black
    }
    
    
    
    #########################################################################################################################################################################
    ##CONSOLIDATING ALL VARIABLES##                                                                                                                                         #
    #########################################################################################################################################################################
    
    $form.Topmost = $True
    
    $form.Add_Shown({$displayname.Select()})
    $global:result = $form.ShowDialog()
    
    if ($result -eq [System.Windows.Forms.DialogResult]::OK)
    {
        #DONE
        $global:name = $displayname.Text
        $name
        #DONE
        $global:firstname = $firstname.Text
        $firstname
        #DONE
        $global:lastname = $lastname.Text
        $lastname
        #DONE
        $global:loginname = $userprincipalname.Text
        $loginname
        $global:samname = $loginname + "@sanderson.lan"
        #DONE
        $global:jobtitle = $jobtitle.Text
        $jobtitle
        #DONE
        $global:finalpassword
        #DONE
        $global:Description = $Description.Text
        $Description
        #DONE
        $global:office = $officeComboBox.Text
        $office
        #DONE
        $global:guideuser = $Group.Text
        $guideuser
        #DONE
        $global:OrganizationalUnit = $OUComboBox.Text
        $OrganizationalUnit
        #DONE
        $global:Database = $DataBaseComboBox.Text
        $Database
        #DONE
        $global:resetpassword = $resetpasswordcheckbox.Checked
        $resetpassword
    
    }


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    Thursday, July 6, 2017 11:18 AM
  • Your question is: "Here's my 200-line code dump. Can someone take the time to copy and paste all of this code and embark on a debugging session for me and fix whatever is wrong?" You can understand why that's a tall order for volunteers. For that reason this kind of question is outside the forum scope.

    Instead, Please post a very short script that contains only the absolute minimum amount of code needed to reproduce the problem. Read this first: Short, Self Contained, Correct (Compilable), Example.

    Also read this post from the top of the forum:

    This forum is for scripting questions rather than script requests


    -- Bill Stewart [Bill_Stewart]

    Thursday, July 6, 2017 12:34 PM
  • Thank you seanmcavinue!

    I forgot to remove the part "$form = $null" because I listed all the variables and made them null as I thought they were being called before they were being created.

    Can believe I forgot to load it hahhaha Thank you very much for your help :)

    Thursday, July 6, 2017 1:17 PM
  • Hi Bill_Stewart,

    apologies for the large code dump. I know it was a bit of a cheeky thing to do. But with my issue, if you had known what your were looking for, which I didn't but seanmcavinue did, then you would have only had to look at the first few lines.

    I even stated that I am getting errors relating to seemingly every object, which now that seanmcavinue, has explained what was missing I can see why that was the issue.

    Again, sorry for the code dump. But as always, I am amazed at the help  and professionalism that comes from this forum :)

    Thursday, July 6, 2017 1:29 PM
  • You're right that I didn't read through your code, but I have a policy that I don't try to understand code dumps because I think they are presumptuous of others' time. Glad you got a fix.

    -- Bill Stewart [Bill_Stewart]

    Thursday, July 6, 2017 2:01 PM