Created
March 26, 2021 15:29
-
-
Save 1RedOne/94bf106325353ea2cb6055c77bb3aa48 to your computer and use it in GitHub Desktop.
Stack Answer 66817943
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
##Setting Static Information | |
$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 | |
#---------------------------------------------- | |
#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 | |
} | |
$combobox1_SelectedIndexChanged= { | |
Write-host "Location changed to $($combobox1.SelectedItem )" | |
$global:UserLocation = $combobox1.SelectedItem | |
} | |
$buttonOK_Click={ | |
#TODO: Place custom script here | |
$formADNewUserScriptSetUs.Close(); | |
write-host "user select location $($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 | |
$formADNewUserScriptSetUs.ShowDialog() | |
##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"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment