I have created a form that gets data from a JSON config file. The first combobox gets a list of departments as listed in the JSON file. Based on what is selected from combobox1, combobox2 should list the titles associated with the selected department. I have the basics created and can get this to work, however, it is not user friendly.
The only thing I want my IT users updating is the JSON file. They should not have to update the Powershell. What I am having an issue figuring out is how to tell Powershell what department is selected and what titles to get out of the JSON file based on that.
Here is what I got so far.
Powershell
.NOTES =========================================================================== Created with: SAPIEN Technologies, Inc., PowerShell Studio 2020 v5.7.180 Created on: 8/19/2020 10:29 AM Created by: Joseph Organization: Company Filename: AddADUser.ps1 =========================================================================== .DESCRIPTION Form will allow the creation of standard AD user accounts #> < # Definitions Prefixes ~ $l_ = Label ~ $tb_ = Textbox ~ $lb_ = Listbox ~ $cb_ = Combobox ~ $ch_ = Checkbox ~ $rb_ = Radio Selection ~ $btn_ = Button #> #region Load Modules Import-Module ActiveDirectory #endregion #region Get Config File $configfile = Get-Content -Raw -Path .\ADCEU_Departments.json | ConvertFrom-Json #endregion #region Functions function AddADUser { $fname = $tb_fname.Text $lname = $tb_lname.Text $puid = $tb_puid.Text $dept = $cb_department.SelectedItem if ($ch_temp.Checked -eq $true) { $etype = 'Temp Employee' } else { $etype = 'Perm Employee' } [System.Windows.Forms.MessageBox]::Show("$fname $lname - $puid - $dept - $etype") } #endregion #region Get AD Info $domain = Get-ADDomainController $dname = $domain.Domain $dcname = $domain.Name #endregion #region Form Build #region Load Form Assemblies [void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") [void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") [void][System.Windows.Forms.Application]::EnableVisualStyles() #endregion #region Form Controls #region First Name $l_fname = New-Object System.Windows.Forms.Label $l_fname.Location = New-Object System.Drawing.Point(10, 20) $l_fname.Size = New-Object System.Drawing.Size(160, 20) $l_fname.Text = 'End User First Name' $tb_fname = New-Object System.Windows.Forms.TextBox $tb_fname.Location = New-Object System.Drawing.Point(10, 40) $tb_fname.Size = New-Object System.Drawing.Size(160, 20) #endregion #region Last Name $l_lname = New-Object System.Windows.Forms.Label $l_lname.Location = New-Object System.Drawing.Point(180, 20) $l_lname.Size = New-Object System.Drawing.Size(160, 20) $l_lname.Text = 'End User Last Name' $tb_lname = New-Object System.Windows.Forms.TextBox $tb_lname.Location = New-Object System.Drawing.Point(180, 40) $tb_lname.Size = New-Object System.Drawing.Size(160, 20) #endregion #region PUID $l_puid = New-Object System.Windows.Forms.Label $l_puid.Location = New-Object System.Drawing.Point(350, 20) $l_puid.Size = New-Object System.Drawing.Size(160, 20) $l_puid.Text = 'PUID' $tb_puid = New-Object System.Windows.Forms.TextBox $tb_puid.Location = New-Object System.Drawing.Point(350, 40) $tb_puid.Size = New-Object System.Drawing.Size(160, 20) #endregion #region Department $l_department = New-Object System.Windows.Forms.Label $l_department.Location = New-Object System.Drawing.Point(10, 70) $l_department.Size = New-Object System.Drawing.Size(160, 20) $l_department.Text = 'Department' $cb_department = New-Object System.Windows.Forms.ComboBox $cb_department.Location = New-Object System.Drawing.Point(10, 90) $cb_department.Size = New-Object System.Drawing.Size(160, 20) foreach ($dept in $configfile) { $deptname = $dept.Department [void]$cb_department.Items.Add($deptname) } #endregion #region Title $l_title = New-Object System.Windows.Forms.Label $l_title.Location = New-Object System.Drawing.Point(180, 70) $l_title.Size = New-Object System.Drawing.Size(160, 20) $l_title.Text = 'Title' $cb_title = New-Object System.Windows.Forms.ComboBox $cb_title.Location = New-Object System.Drawing.Point(180, 90) $cb_title.Size = New-Object System.Drawing.Size(160, 20) foreach ($dept in $configfile) { if ($dept.Department -eq 'Information Technology') { $titles = $dept.Groups.Titles | Sort-Object Title } } $cb_department_change = { $cb_title.Items.Clear() $cb_title.Text = $null switch ($cb_department.Text) { "Information Technology" { $titles.Title | foreach { $cb_title.Items.Add($_) } } } } $cb_department.add_SelectedIndexChanged($cb_department_change) #endregion #region Temp Employee $ch_temp = new-object System.Windows.Forms.checkbox $ch_temp.Location = new-object System.Drawing.Point(180, 90) $ch_temp.Text = "Temp Employee" $ch_temp.Checked = $false #endregion #region Create User Button $btn_createuser = new-object System.Windows.Forms.Button $btn_createuser.Location = new-object System.Drawing.Size(350, 80) $btn_createuser.Size = new-object System.Drawing.Size(100, 40) $btn_createuser.Text = "Create User" $btn_createuser.Add_Click( { AddADUser }) #endregion #region Cancel Button $btn_cancel = new-object System.Windows.Forms.Button $btn_cancel.Location = new-object System.Drawing.Size(460, 80) $btn_cancel.Size = new-object System.Drawing.Size(100, 40) $btn_cancel.Text = "Cancel" $btn_cancel.Add_Click( { $form.Close }) #endregion #endregion #region Form GUI Build $form = New-Object system.Windows.Forms.Form $form.Size = New-Object System.Drawing.Size(650, 200) $form.StartPosition = 'CenterScreen' $form.Text = "Active Directory: Create End User | Connected to $dcname in $dname" $form.Icon = 'File:\300x300_Ik4_icon.ico' $form.TopMost = $true $form.Controls.Add($l_fname) $form.Controls.Add($tb_fname) $form.Controls.Add($l_lname) $form.Controls.Add($tb_lname) $form.Controls.Add($l_puid) $form.Controls.Add($tb_puid) $form.Controls.Add($l_department) $form.Controls.Add($cb_department) $form.Controls.Add($l_title) $form.Controls.Add($cb_title) # $form.Controls.Add($ch_temp) $form.Controls.Add($btn_createuser) $form.Controls.Add($btn_cancel) $form.Add_Shown( { $tb_fname.Select() }) $form.ShowDialog() #endregion #endregion
JSON
[ { "Department": "Information Technology", "Groups": [ { "Group": "GG_Infrastructure", "Titles": [ { "Title": "Director of IT" }, { "Title": "Senior Systems Administrator" }, { "Title": "Systems Adminitrator" }, { "Title": "Desktop Support Technician" }, { "Title": "Systems Analyst I" }, { "Title": "Systems Analyst II" } ] }, { "Group": "GG_Solutions", "Titles": [ { "Title": "VP Systems Development" }, { "Title": "Senior Systems Developer" }, { "Title": "Healthcare EDI Analyst" }, { "Title": "Data Scientist" } ] } ] }, { "Department": "Billing", "Groups": [ { "Group": "GG_Billing", "Titles": [ { "Title": "Processor" } ] } ] }, { "Department": "Shipping", "Groups": [ { "Group": "GG_Shipping", "Titles": [ { "Title": "Shipping Manager" }, { "Title": "Associate" } ] } ] } ]