Reputation: 13
I am trying to create a script to aid my helpdesk team when creating an AD Account. So far I am able to create a user account with a few attributes, however I am trying to make the script capable of filling all the required details out. What I would like to do is have a set list of locations and once a location is selected the script will use the variable $UserLocation in a range of functions, for now I just want the text returned ie; a user selects 'Location1' the variable $UserLocation returns the value 'Location1'. However at the moment it doesn't seem to work, I realise this is a really silly question but I can't figure it out for the life of me.
The area that has the issue is ##GetUserLocation The end result is to user $UserLocation to return the text selected in the combo box.
The Code I have so far is:
$primaryTelexNumber = "1"
$telexNumber = "7"
##Get Users First Name
[string] [Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
$title = "AD New User Script: First Name"
$msg = "Please Enter the new User's First Name"
$FirstName = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title)
##Get Users Last Name
[string] [Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
$title = "AD New User Script: Last Name"
$msg = "Please Enter the new User's Last Name"
$LastName = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title)
##Define UserName - not wokring yet hense manual capture
#$UserName = "$LastName($FirstName.SubString(0,1))"
##Get UserName
[string] [Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
$title = "AD New User Script: User Name"
$msg = "Please Enter the proposed User Name"
$UserName = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title)
##Set UserPassowrd
[string] [Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
$title = "AD New User Script: Password"
$msg = "Please Enter initial Password"
$Password = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title)
##GetUserLocation
function Show-DropDownSelector_psf {
#----------------------------------------------
#region Import the Assemblies
#----------------------------------------------
[void][reflection.assembly]::Load('System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
[void][reflection.assembly]::Load('System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
#endregion Import Assemblies
#----------------------------------------------
#region Generated Form Objects
#----------------------------------------------
[System.Windows.Forms.Application]::EnableVisualStyles()
$formADNewUserScriptSetUs = New-Object 'System.Windows.Forms.Form'
$buttonCancel = New-Object 'System.Windows.Forms.Button'
$buttonOK = New-Object 'System.Windows.Forms.Button'
$labelSelectUserLocation = New-Object 'System.Windows.Forms.Label'
$combobox1 = New-Object 'System.Windows.Forms.ComboBox'
$InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
#endregion Generated Form Objects
#----------------------------------------------
# User Generated Script
#----------------------------------------------
$formADNewUserScriptSetUs_Load={
#TODO: Initialize Form Controls here
}
#region Control Helper Functions
function Update-ComboBox
{
<#
.SYNOPSIS
This functions helps you load items into a ComboBox.
.DESCRIPTION
Use this function to dynamically load items into the ComboBox control.
.PARAMETER ComboBox
The ComboBox control you want to add items to.
.PARAMETER Items
The object or objects you wish to load into the ComboBox's Items collection.
.PARAMETER DisplayMember
Indicates the property to display for the items in this control.
.PARAMETER ValueMember
Indicates the property to use for the value of the control.
.PARAMETER Append
Adds the item(s) to the ComboBox without clearing the Items collection.
.EXAMPLE
Update-ComboBox $combobox1 "Red", "White", "Blue"
.EXAMPLE
Update-ComboBox $combobox1 "Red" -Append
Update-ComboBox $combobox1 "White" -Append
Update-ComboBox $combobox1 "Blue" -Append
.EXAMPLE
Update-ComboBox $combobox1 (Get-Process) "ProcessName"
.NOTES
Additional information about the function.
#>
param
(
[Parameter(Mandatory = $true)]
[ValidateNotNull()]
[System.Windows.Forms.ComboBox]
$ComboBox,
[Parameter(Mandatory = $true)]
[ValidateNotNull()]
$Items,
[Parameter(Mandatory = $false)]
[string]$DisplayMember,
[Parameter(Mandatory = $false)]
[string]$ValueMember,
[switch]
$Append
)
if (-not $Append)
{
$ComboBox.Items.Clear()
}
if ($Items -is [Object[]])
{
$ComboBox.Items.AddRange($Items)
}
elseif ($Items -is [System.Collections.IEnumerable])
{
$ComboBox.BeginUpdate()
foreach ($obj in $Items)
{
$ComboBox.Items.Add($obj)
}
$ComboBox.EndUpdate()
}
else
{
$ComboBox.Items.Add($Items)
}
if ($DisplayMember)
{
$ComboBox.DisplayMember = $DisplayMember
}
if ($ValueMember)
{
$ComboBox.ValueMember = $ValueMember
}
}
#endregion
$combobox1_SelectedIndexChanged={
#TODO: Place custom script here
}
$buttonOK_Click={
#TODO: Place custom script here
$combobox1.add_SelectedIndexChanged({ $UserLocation = $combobox1.SelectedItem })
}
# --End User Generated Script--
#----------------------------------------------
#region Generated Events
#----------------------------------------------
$Form_StateCorrection_Load=
{
#Correct the initial state of the form to prevent the .Net maximized form issue
$formADNewUserScriptSetUs.WindowState = $InitialFormWindowState
}
$Form_Cleanup_FormClosed=
{
#Remove all event handlers from the controls
try
{
$buttonOK.remove_Click($buttonOK_Click)
$combobox1.remove_SelectedIndexChanged($combobox1_SelectedIndexChanged)
$formADNewUserScriptSetUs.remove_Load($formADNewUserScriptSetUs_Load)
$formADNewUserScriptSetUs.remove_Load($Form_StateCorrection_Load)
$formADNewUserScriptSetUs.remove_FormClosed($Form_Cleanup_FormClosed)
}
catch { Out-Null <# Prevent PSScriptAnalyzer warning #> }
}
#endregion Generated Events
#----------------------------------------------
#region Generated Form Code
#----------------------------------------------
$formADNewUserScriptSetUs.SuspendLayout()
#
# formADNewUserScriptSetUs
#
$formADNewUserScriptSetUs.Controls.Add($buttonCancel)
$formADNewUserScriptSetUs.Controls.Add($buttonOK)
$formADNewUserScriptSetUs.Controls.Add($labelSelectUserLocation)
$formADNewUserScriptSetUs.Controls.Add($combobox1)
$formADNewUserScriptSetUs.AutoScaleDimensions = New-Object System.Drawing.SizeF(6, 13)
$formADNewUserScriptSetUs.AutoScaleMode = 'Font'
$formADNewUserScriptSetUs.ClientSize = New-Object System.Drawing.Size(371, 123)
$formADNewUserScriptSetUs.Name = 'formADNewUserScriptSetUs'
$formADNewUserScriptSetUs.Text = 'AD New User Script: Set User Location'
$formADNewUserScriptSetUs.add_Load($formADNewUserScriptSetUs_Load)
#
# buttonCancel
#
$buttonCancel.Location = New-Object System.Drawing.Point(231, 86)
$buttonCancel.Name = 'buttonCancel'
$buttonCancel.Size = New-Object System.Drawing.Size(75, 23)
$buttonCancel.TabIndex = 3
$buttonCancel.Text = 'Cancel'
$buttonCancel.UseVisualStyleBackColor = $True
#
# buttonOK
#
$buttonOK.Location = New-Object System.Drawing.Point(137, 86)
$buttonOK.Name = 'buttonOK'
$buttonOK.Size = New-Object System.Drawing.Size(75, 23)
$buttonOK.TabIndex = 2
$buttonOK.Text = 'OK'
$buttonOK.UseVisualStyleBackColor = $True
$buttonOK.add_Click($buttonOK_Click)
#
# labelSelectUserLocation
#
$labelSelectUserLocation.AutoSize = $True
$labelSelectUserLocation.Location = New-Object System.Drawing.Point(25, 45)
$labelSelectUserLocation.Name = 'labelSelectUserLocation'
$labelSelectUserLocation.Size = New-Object System.Drawing.Size(106, 13)
$labelSelectUserLocation.TabIndex = 1
$labelSelectUserLocation.Text = 'Select User Location'
#
# combobox1
#
$combobox1.FormattingEnabled = $True
[void]$combobox1.Items.Add('Location1')
[void]$combobox1.Items.Add('Location2')
[void]$combobox1.Items.Add('Location3')
[void]$combobox1.Items.Add('Location4')
[void]$combobox1.Items.Add('Location5')
[void]$combobox1.Items.Add('Location6')
[void]$combobox1.Items.Add('Location7')
[void]$combobox1.Items.Add('Location8')
[void]$combobox1.Items.Add('Location9')
[void]$combobox1.Items.Add('Location10')
[void]$combobox1.Items.Add('Location11')
[void]$combobox1.Items.Add('Location12')
$combobox1.Location = New-Object System.Drawing.Point(137, 42)
$combobox1.Name = 'combobox1'
$combobox1.Size = New-Object System.Drawing.Size(199, 21)
$combobox1.TabIndex = 0
$combobox1.add_SelectedIndexChanged($combobox1_SelectedIndexChanged)
$formADNewUserScriptSetUs.ResumeLayout()
#endregion Generated Form Code
#----------------------------------------------
#Save the initial state of the form
$InitialFormWindowState = $formADNewUserScriptSetUs.WindowState
#Init the OnLoad event to correct the initial state of the form
$formADNewUserScriptSetUs.add_Load($Form_StateCorrection_Load)
#Clean up the control events
$formADNewUserScriptSetUs.add_FormClosed($Form_Cleanup_FormClosed)
#Show the Form
return $formADNewUserScriptSetUs.ShowDialog()
} #End Function
#Call the form
Show-DropDownSelector_psf | Out-Null
##Get E-Mail
[string] [Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
$title = "AD New User Script: E-mail"
$msg = "Please Enter the user's E-mail Address"
$Email = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title)
##Get JobTitle
[string] [Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
$title = "AD New User Script: Job Title"
$msg = "Please Enter the user's job title"
$JobTitle = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title)
##Get Department
[string] [Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
$title = "AD New User Script: Department"
$msg = "Please Enter the user's Department"
$Department = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title)
##Get DeskPhoneNumber
[string] [Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
$title = "AD New User Script: Desk Phone Extension"
$msg = "Please Enter the user's Desk Phone Extension"
$DeskPhone = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title)
##Confirm Account Details
[System.Windows.Forms.MessageBox]::show("Verify the followning is correct:
User Name: $UserName
Password: $Password
Full Name: $FirstName $LastName
Job Title: $JobTitle
Department: $Department
Desk Phone Ext: $DeskPhone
Email Address: $Email
Click OK to continue and Create the account. Press Cancel to stop the process.
" , "AD New User", 1)
##Create AD User Account
$NewUserAttributes = @{
'UserPrincipalName' = $UserName
'DisplayName' = "$LastName $FirstName"
'Name' = $UserName
'GivenName' = $FirstName
'Surname' = $LastName
'Title' = $JobTitle
'Department' = $Department
'Description' = $JobTitle
'EmailAddress' = $Email
'SamAccountName' = $UserName
'AccountPassword' = (ConvertTo-SecureString $Password -AsPlainText -Force)
'Enabled' = $True
'ChangePasswordAtLogon' = $True
'path' = "OU=Users,OU=TEST,OU=Site,DC=Dev,DC=Test,DC=Local"
}
New-ADUser @NewUserAttributes
##Set IP Phone
Set-ADUser $UserName -add @{ipPhone="$DeskPhone"}
Upvotes: 1
Views: 1081
Reputation: 60145
Here is what I personally do. You have a few issues with scopes. I don't think it's a good idea to create your forms inside functions, it will be a nightmare to work with and you will also have a big problem with scopes.
Functions are meant to act as event listeners for your UI application (imo).
There is probably a better way for this, but I'm used to retrieving values using Set-Variable
since the scope of Add_Click
event is different than the current scope. Doing something like $locationResult = $combobox1.selectedItem
doesn't work.
$formADNewUserScriptSetUs = New-Object 'System.Windows.Forms.Form'
$formADNewUserScriptSetUs.AutoScaleDimensions = New-Object System.Drawing.SizeF(6, 13)
$formADNewUserScriptSetUs.AutoScaleMode = 'Font'
$formADNewUserScriptSetUs.ClientSize = New-Object System.Drawing.Size(371, 123)
$combobox1 = New-Object 'System.Windows.Forms.ComboBox'
$combobox1.FormattingEnabled = $true
1..12|%{[void]$combobox1.Items.Add("Location $_")}
$combobox1.Location = New-Object System.Drawing.Point(137, 42)
$combobox1.Name = 'combobox1'
$combobox1.Size = New-Object System.Drawing.Size(199, 21)
$combobox1.TabIndex = 0
$buttonOK = New-Object 'System.Windows.Forms.Button'
$buttonOK.Location = New-Object System.Drawing.Point(137, 86)
$buttonOK.Name = 'buttonOK'
$buttonOK.Size = New-Object System.Drawing.Size(75, 23)
$buttonOK.TabIndex = 2
$buttonOK.Text = 'OK'
$buttonOK.UseVisualStyleBackColor = $True
$buttonOK.add_Click({
Write-Host $combobox1.selectedItem #Your answer here
# Use this
Set-Variable -Name locationResult -Value $combobox1.selectedItem -Force -Scope Script
# or this to retrieve the user selection
$script:locationResult = $combobox1.selectedItem
})
$formADNewUserScriptSetUs.Controls.Add($buttonOK)
$formADNewUserScriptSetUs.Controls.Add($combobox1)
$formADNewUserScriptSetUs.ShowDialog()
Upvotes: 2
Reputation: 13537
Very nice UI, nicely done :)
On closer review, you had code to set $combobox1.add_SelectedIndexChanged
more than once, effectively removing the eventhandler I suggested in my previous answer. There was also a function that was in your code but not used at all, a ComboList helper. I trimmed your code down here and it now works fine.
Here's your issue. You are correctly setting up an event handler to trigger when a user changes the selected item in a combobox.
$buttonOK_Click={
#TODO: Place custom script here
$combobox1.add_SelectedIndexChanged({ $UserLocation = $combobox1.SelectedItem })
}
BUT, this event handler is only set up after the user clicks the OK button, which isn't what you want to do. Also, when the user hits the OK button, the UI should probably progress to the next UI element. So just change it like this:
#move this event handler OUTSIDE of the $buttonOK_Click handler.
$combobox1.add_SelectedIndexChanged({ $UserLocation = $combobox1.SelectedItem })
#when the user clicks OK, echo out to the screen their new selection (for debugging purposes)
#and then also close the UI, as the user would expect
$buttonOK_Click={
#TODO: Place custom script here
$formADNewUserScriptSetUs.Close();
write-host "user select location $($combobox1.SelectedItem )"
}
After testing this, the UI closes as expected, and also the $userLocation
variable now held the value selected.
Upvotes: 0