0

I'm creating a GUI to setup new users in AD. There is a section where a listbox of locations appear and I would like a series of commands to fire off when a location is picked. The problem I am seeing is that the first (write-host -BackgroundColor DarkGreen...) and last ($outputBox.text....) commands fire, but not the other two where variables are set. The $TEMPLATE and $OU lines run fine when I copy and paste them directly in the PowerShell console. I assume this is an issue with the way I've setup the add_click part.

Also, you can see that the variables pull from existing AD templates.

Hopefully this part of the code gives insight to what I'm trying to do.

#Branch
$Label_Branch = New-Object system.Windows.Forms.Label
$Label_Branch.text = "Branch"
$Label_Branch.autoSize = $true
$Label_Branch.width= 25
$Label_Branch.height = 10
$Label_Branch.location = New-Object System.Drawing.Point(180,160)
$Label_Branch.Font = 'Microsoft Sans Serif,10'
$Form_NewUser.Controls.Add($Label_Branch)

$ListBox_Branch = New-Object system.Windows.Forms.ListBox
$ListBox_Branch.text = "Branch"
$ListBox_Branch.width= 180
$ListBox_Branch.height = 80
$ListBox_Branch.location = New-Object System.Drawing.Point(180,180)
$Form_NewUser.Controls.Add($ListBox_Branch)

[void] $ListBox_Branch.Items.Add('A6A')
[void] $ListBox_Branch.Items.Add('BAA')
[void] $ListBox_Branch.Items.Add('BBS')
[void] $ListBox_Branch.Items.Add('BDH')
[void] $ListBox_Branch.Items.Add('CRPP')
[void] $ListBox_Branch.Items.Add('CBC')
[void] $ListBox_Branch.Items.Add('EPWA')
[void] $ListBox_Branch.Items.Add('FCB')
[void] $ListBox_Branch.Items.Add('LMS')
[void] $ListBox_Branch.Items.Add('LAR')
[void] $ListBox_Branch.Items.Add('LCCC')
[void] $ListBox_Branch.Items.Add('PHA')
[void] $ListBox_Branch.Items.Add('W1A')
[void] $ListBox_Branch.Items.Add('WFB')
[void] $ListBox_Branch.Items.Add('BIB')
[void] $ListBox_Branch.Items.Add('WOC')

#Output
$outputBox = New-Object System.Windows.Forms.TextBox 
$outputBox.Location = New-Object System.Drawing.Size(40,530) 
$outputBox.Size = New-Object System.Drawing.Size(470,130) 
$outputBox.MultiLine = $True
$outputBox.Scrollbars = "Vertical"
$Form_NewUser.Controls.Add($outputBox) 

#Chosing user template based on branch chosen
$ListBox_Branch.Add_Click({
    Switch ($ListBox_Branch.SelectedItem)
    {
        'A6A'{
            write-host -BackgroundColor DarkGreen "A6A user template selected (console)"
            $TEMPLATE = Get-ADUser -Identity templatear -Properties StreetAddress,City,State,PostalCode,Country,Office,Company
            $OU = "OU=AR,OU=Active User Accounts,OU=COMPANY,DC=LAN,DC=DOM,DC=ORG"
            $outputBox.text = "A6A user template selected (form)"
        }
        'BAA'{
            write-host -BackgroundColor DarkGreen "BAA user template selected"            
            $TEMPLATE = Get-ADUser -Identity templateae -Properties StreetAddress,City,State,PostalCode,Country,Office,Company
            $OU = "OU=AE,OU=Active User Accounts,OU=COMPANY,DC=LAN,DC=DOM,DC=ORG"
            $outputBox.text = "BAA user template selected"
        }
        'BBS'{
            write-host -BackgroundColor DarkGreen "BBS user template selected"   
            $TEMPLATE = Get-ADUser -Identity templatebw -Properties StreetAddress,City,State,PostalCode,Country,Office,Company
            $OU = "OU=BW,OU=Active User Accounts,OU=COMPANY,DC=LAN,DC=DOM,DC=ORG"
            $outputBox.text = "BBS user template selected"
  • 1
    In short: Event handlers run in a _child scope_, so in order to update a variable in the script's scope, you must use `$script:variable = ...`, otherwise you'll implicitly create a _block-local_ variable. See [this answer](https://stackoverflow.com/a/57649395/45375) to the linked duplicate. – mklement0 Oct 06 '21 at 15:48
  • The `script:` part is a _scope modifier_ that is part of the variable reference; therefore, all you need to do is to insert `script:` right after the `$` in all the variable assignments you want to be available outside the event handler; to spell it out for _one_ variable (all on one line, as in your code): `$script:TEMPLATE = Get-ADUser -Identity templatear -Properties StreetAddress,City,State,PostalCode,Country,Office,Company` - do the same analogously for the others. See [about_Scopes](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Scopes). – mklement0 Oct 06 '21 at 16:47

0 Answers0