-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserCreationScript.ps1
179 lines (146 loc) · 6.33 KB
/
UserCreationScript.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#this script creates a gui in powershell for creating a user in active directory and assigns certain properties like position. Can easily be modified for any enviornment for quick user creation
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
Import-Module ActiveDirectory
# Start of the form this is where I can modify the overall size of the popout windoe
$form = New-Object System.Windows.Forms.Form
$form.Text = "Employee Information"
$form.Size = New-Object System.Drawing.Size(500, 500)
$form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle
$form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen
# Label Section, for orgranization purposes it's a lot easier to group labels all in one section
$labelFirstName = New-Object System.Windows.Forms.Label
$labelFirstName.Text = "First Name:"
$labelFirstName.Location = New-Object System.Drawing.Point(20, 20)
$labelFirstName.AutoSize = $true
$labelLastName = New-Object System.Windows.Forms.Label
$labelLastName.Text = "Last Name:"
$labelLastName.Location = New-Object System.Drawing.Point(20, 60)
$labelLastName.AutoSize = $true
$labelDepartment = New-Object System.Windows.Forms.Label
$labelDepartment.Text = "Department:"
$labelDepartment.Location = New-Object System.Drawing.Point(20, 100)
$labelDepartment.AutoSize = $true
$labelPosition = New-Object System.Windows.Forms.Label
$labelPosition.Text = "Position:"
$labelPosition.Location = New-Object System.Drawing.Point(20, 140)
$labelPosition.AutoSize = $true
# textboxes
$textBoxFirstName = New-Object System.Windows.Forms.TextBox
$textBoxFirstName.Location = New-Object System.Drawing.Point(120, 20)
$textBoxFirstName.Size = New-Object System.Drawing.Size(150, 20)
$textBoxLastName = New-Object System.Windows.Forms.TextBox
$textBoxLastName.Location = New-Object System.Drawing.Point(120, 60)
$textBoxLastName.Size = New-Object System.Drawing.Size(150, 20)
# dropdown list for department
$comboBoxDepartment = New-Object System.Windows.Forms.ComboBox
$comboBoxDepartment.Location = New-Object System.Drawing.Point(120, 100)
$comboBoxDepartment.Size = New-Object System.Drawing.Size(150, 20)
# Add department options to the dropdown list
$departments = @("Accounting", "Executive", "Hospitality", "HR", "Maintenance", "Marketing", "Operations")
$comboBoxDepartment.Items.AddRange($departments)
# Create dropdown list for position
$comboBoxPosition = New-Object System.Windows.Forms.ComboBox
$comboBoxPosition.Location = New-Object System.Drawing.Point(120, 140)
$comboBoxPosition.Size = New-Object System.Drawing.Size(150, 20)
# Event handler for when the department selection changes
$comboBoxDepartment.add_SelectedIndexChanged({
$selectedDepartment = $comboBoxDepartment.SelectedItem
# Populate positions based on the selected department
switch ($selectedDepartment) {
"Accounting" {
$positions = @()
}
"Executive" {
$positions = @()
}
"Hospitality" {
$Positions = @()
}
"HR" {
$Positions = @()
}
"Maintenance" {
$Positions = @()
}
"Marketing" {
$Positions = @()
}
"Operations" {
$Positions = @()
}
"Parts" {
$Positions = @()
}
"" {
$Positions = @()
}
"Sales" {
$Positions = @()
}
"Service" {
$Positions = @()
}
"" {
$Positions = @()
}
default {
$positions = @()
}
}
# Clear and update the position dropdown list
$comboBoxPosition.Items.Clear()
$comboBoxPosition.Items.AddRange($positions)
})
$positionToGroupMap = @{
"position" =@("Name of Group")
}
# OK button
$buttonOK = New-Object System.Windows.Forms.Button
$buttonOK.Text = "OK"
$buttonOK.Location = New-Object System.Drawing.Point(120, 240)
$buttonOK.DialogResult = [System.Windows.Forms.DialogResult]::OK
# click for ok button
$buttonOK.Add_Click({
$firstName = $textBoxFirstName.Text
$lastName = $textBoxLastName.Text
$department = $comboBoxDepartment.SelectedItem
$position = $comboBoxPosition.SelectedItem
# Check if all fields are filled
if ($firstName -eq "" -or $lastName -eq "" -or $department -eq "" -or $position -eq "") {
[System.Windows.Forms.MessageBox]::Show("Please fill all fields.", "Error", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
return
}
# Create the user in Active Directory
try {
$ADName = $firstName + $lastName
$username = $firstName.Substring(0, 1) + $lastName
$ouPath = ""
New-ADUser -SamAccountName $username -Name $ADName -GivenName $firstName -Surname $lastName -Description $position -Department $department -Title $position -Enabled $true -AccountPassword (ConvertTo-SecureString "Ukts1976!" -AsPlainText -Force) -Path $ouPath
# Get the corresponding group name for the position from the hashtable
$groupNames = $positionToGroupMap[$position]
# Add the user to the appropriate group in Active Directory
if ($groupNames) {
foreach ($groupName in $groupNames){
Add-ADGroupMember -Identity $groupName -Members $username
}
}
[System.Windows.Forms.MessageBox]::Show("User '$username' created successfully.", "Success", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Information)
} catch {
[System.Windows.Forms.MessageBox]::Show("Error creating the user. " + $_.Exception.Message, "Error", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
}
})
# Add controls to the form
$form.Controls.Add($labelFirstName)
$form.Controls.Add($textBoxFirstName)
$form.Controls.Add($labelLastName)
$form.Controls.Add($textBoxLastName)
$form.Controls.Add($labelDepartment)
$form.Controls.Add($comboBoxDepartment)
$form.Controls.Add($labelPosition)
$form.Controls.Add($comboBoxPosition)
$form.Controls.Add($buttonOK)
# Show the form
$result = $form.ShowDialog()
# Dispose the form
$form.Dispose()