-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreate User with Random Password.ps1
More file actions
285 lines (264 loc) · 11.8 KB
/
Copy pathCreate User with Random Password.ps1
File metadata and controls
285 lines (264 loc) · 11.8 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#Requires -Version 5.1
<#
.SYNOPSIS
Create a local user account with options to enable and disable at specific dates, and add to local admin group. Saves randomly generated password to a custom field.
.DESCRIPTION
You can specify when the account will be enabled and/or disabled.
You can have the account be added as a member of the local Administrators group.
PARAMETER: -UserNameToAdd "JohnTSmith" -Name "John T Smith"
Create use with the name JohnTSmith and display name of John T Smith.
.EXAMPLE
-UserNameToAdd "JohnTSmith" -Name "John T Smith"
## EXAMPLE OUTPUT ##
User JohnTSmith has been created successfully.
User JohnTSmith was added to the local Users group.
PARAMETER: -UserNameToAdd "JohnTSmith" -Name "John T Smith" -DateAndTimeToEnable "Monday, January 1, 2020 1:00:00 PM"
Create use with the name JohnTSmith and display name of John T Smith.
The user will start out disabled.
A scheduled task will be create to enable the user after "Monday, January 1, 2020 1:00:00 PM"
.EXAMPLE
-UserNameToAdd "JohnTSmith" -Name "John T Smith" -DateAndTimeToEnable "Monday, January 1, 2020 1:00:00 PM"
## EXAMPLE OUTPUT ##
User JohnTSmith has been created successfully.
User JohnTSmith was added to the local Users group.
Created Scheduled Task: Enable User JohnTSmith
User JohnTSmith will be able to login after Monday, January 1, 2020 1:00:00 PM.
PARAMETER: -UserNameToAdd "JohnTSmith" -Name "John T Smith" -DisableAfterDays 10
Create use with the name JohnTSmith and display name of John T Smith.
The user will be disabled after 10 days after the user's creation.
.EXAMPLE
-UserNameToAdd "JohnTSmith" -Name "John T Smith" -DisableAfterDays 10
## EXAMPLE OUTPUT ##
User JohnTSmith has been created successfully.
User JohnTSmith was added to the local Users group.
PARAMETER: -UserNameToAdd "JohnTSmith" -Name "John T Smith" -AddToLocalAdminGroup
Create use with the name JohnTSmith and display name of John T Smith.
User will be added as a member of the local Administrators group.
.EXAMPLE
-UserNameToAdd "JohnTSmith" -Name "John T Smith" -AddToLocalAdminGroup
## EXAMPLE OUTPUT ##
User JohnTSmith has been created successfully.
User JohnTSmith was added to the local Users group.
User JohnTSmith was added to the local Administrators group.
.OUTPUTS
None
.NOTES
Minimum OS Architecture Supported: Windows 10, Windows Server 2016
Release Notes: Initial Release
#>
[CmdletBinding()]
param (
[Parameter()]
[String]$UserNameToAdd,
[Parameter()]
[String]$Name,
[Parameter()]
[String]$PasswordCustomField,
[Parameter()]
[int]$PasswordLength,
[Parameter()]
[DateTime]$DateAndTimeToEnable,
[Parameter()]
[int]$DisableAfterDays,
[Parameter()]
[Switch]$AddToLocalAdminGroup,
[Parameter()]
$PasswordOptions
)
begin {
function Test-IsElevated {
$id = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$p = New-Object System.Security.Principal.WindowsPrincipal($id)
$p.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)
}
function New-SecurePassword {
[CmdletBinding()]
param (
[Parameter(Mandatory = $false)]
[int]$Length = 16,
[Parameter(Mandatory = $false)]
[bool]$IncludeSpecialCharacters = $true
)
# .NET class for generating cryptographically secure random numbers
$cryptoProvider = New-Object System.Security.Cryptography.RNGCryptoServiceProvider
$SpecialCharacters = if ($IncludeSpecialCharacters) { '!@#$%&-' }
$passwordChars = "abcdefghjknpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ0123456789$SpecialCharacters"
$password = for ($i = 0; $i -lt $Length; $i++) {
$byte = [byte[]]::new(1)
$cryptoProvider.GetBytes($byte)
$charIndex = $byte[0] % $passwordChars.Length
$passwordChars[$charIndex]
}
return $password -join ''
}
function New-LocalUserFromNinja {
param(
[string]$Username,
[string]$Name,
[string]$PasswordCustomField,
[DateTime]$EnableDate,
[int]$DisableAfterDays,
[switch]$AddToLocalAdminGroup
)
# Generate a secure localUserPassword
$Password = New-SecurePassword -Length $PasswordLength -IncludeSpecialCharacters $true
if ($Username -and $Name) {
# Check if the user already exists
if (-not (Get-LocalUser -Name $Username -ErrorAction SilentlyContinue)) {
# Create new local user
$UserSplat = @{
Name = "$Username"
FullName = "$Name"
Password = ConvertTo-SecureString -String $($Password -join '') -AsPlainText -Force
Description = "User account created on $(Get-Date)"
PasswordNeverExpires = $false
}
if ($EnableDate -and $EnableDate -gt (Get-Date)) {
$UserSplat['Disabled'] = $true
}
if (-not $EnableDate -and $DisableAfterDays) {
$UserSplat['AccountExpires'] = $(Get-Date).AddDays($DisableAfterDays)
}
elseif ($DisableAfterDays) {
$UserSplat['AccountExpires'] = $(Get-Date $EnableDate).AddDays($DisableAfterDays)
}
if ($env:passwordOptions -like 'Password Never Expires' -or $PasswordOptions -like 'Password Never Expires') {
$UserSplat['PasswordNeverExpires'] = $true
}
New-LocalUser @UserSplat
if ($env:passwordOptions -like 'User Must Change Password' -or $PasswordOptions -like 'User Must Change Password') {
net.exe user $Username /logonpasswordchg:yes
}
# Write it to a secure custom field
if ((Get-LocalUser -Name $Username -ErrorAction SilentlyContinue)) {
Write-Host "User $Username has been created successfully."
if ($PasswordCustomField -like "null") {
Write-Host "CustomField not specified."
Write-Host "Password set to: $Password"
}
else {
Ninja-Property-Set -Name "$PasswordCustomField" -Value "$Password"
Write-Host "Password saved to $PasswordCustomField Custom Field."
}
}
else {
throw "Failed to create User $Username."
}
Add-LocalGroupMember -Group $(Get-LocalGroup -Name "Users") -Member $Username
Write-Host "User $UserName was added to the local Users group."
# If date to enable account is specified, disable account until then
if ($EnableDate) {
if ($EnableDate -gt (Get-Date)) {
# Schedule a job to enable the user at the specified date
$TaskSplat = @{
Description = "Ninja Automation Enable User $Username"
Action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -WindowStyle Hidden -Command & {Enable-LocalUser -Name `"$Username`"}"
Trigger = New-ScheduledTaskTrigger -Once -At $EnableDate
Principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount
}
try {
New-ScheduledTask @TaskSplat | Register-ScheduledTask -User "System" -TaskName "Enable User $Username" | Out-Null
if ($(Get-ScheduledTask -TaskName "Enable User $Username")) {
Write-Host "Created Scheduled Task: Enable User $Username"
}
else {
throw "Failed to find scheduled task with the name 'Enable User $Username'"
}
}
catch {
Write-Error $_
throw "Failed to create Enable User scheduled task."
}
Write-Host "User $Username will be able to login after $EnableDate."
}
}
else {
Write-Host "No Enable Date is Set, $UserName is able to login now."
}
# Add to local admin group if specified
if ($AddToLocalAdminGroup) {
Add-LocalGroupMember -Group $(Get-LocalGroup -Name "Administrators") -Member $Username
if (-not (Get-LocalGroupMember -Group $(Get-LocalGroup -Name "Administrators") -Member $Username)) {
throw "Failed to add user to local Administrators group."
}
Write-Host "User $UserName was added to the local Administrators group."
}
}
else {
Write-Host "User $Username already exists."
}
}
else {
throw "Username and Name are required to create a local account."
}
}
}
process {
if ($env:usernameToAdd -and $env:usernameToAdd -like "null") {
Write-Error "usernameToAdd($env:usernameToAdd) parameter is invalid."
exit 1
}
if ($env:name -and $env:name -like "null") {
Write-Error "name($env:name) parameter is invalid."
exit 1
}
if ($env:passwordCustomField -and $env:passwordCustomField -like "null") {
Write-Error "passwordCustomField($env:passwordCustomField) parameter is invalid."
exit 1
}
if (-not (Test-IsElevated)) {
Write-Error -Message "Access Denied. Please run with Administrator privileges."
exit 1
}
$params = @{
Username = if ($PSBoundParameters.ContainsKey("UserNameToAdd")) { $UserNameToAdd }else { $env:usernameToAdd }
Name = if ($PSBoundParameters.ContainsKey("Name")) { $Name }else { $env:name }
}
# Conditionally add EnableDate
if ($env:dateAndTimeToEnable -and $env:dateAndTimeToEnable -notlike "null") {
$params["EnableDate"] = Get-Date "$env:dateAndTimeToEnable"
}
elseif ($PSBoundParameters.ContainsKey("DateAndTimeToEnable") -and $DateAndTimeToEnable) {
$params["EnableDate"] = $DateAndTimeToEnable
}
# Conditionally add DisableAfterDays
if ($env:disableAfterDays -notlike "null") {
$params["DisableAfterDays"] = $env:disableAfterDays
}
elseif ($PSBoundParameters.ContainsKey("DisableAfterDays")) {
$params["DisableAfterDays"] = $DisableAfterDays
}
# Conditionally add AddToLocalAdminGroup
if ([Convert]::ToBoolean($env:addToLocalAdminGroup)) {
$params["AddToLocalAdminGroup"] = $true
}
elseif ($PSBoundParameters.ContainsKey("AddToLocalAdminGroup")) {
$params["AddToLocalAdminGroup"] = $AddToLocalAdminGroup
}
# Conditionally add AddToLocalAdminGroup
if ($env:passwordCustomField -notlike "null") {
$params["PasswordCustomField"] = $env:passwordCustomField
}
elseif ($env:passwordCustomField -like "null") {
Write-Error "passwordCustomField: is Required"
exit 1
}
elseif ($PSBoundParameters.ContainsKey("PasswordCustomField")) {
$params["PasswordCustomField"] = $PasswordCustomField
}
if ($env:passwordLength -notlike "null") {
$PasswordLength = $env:passwordLength
}
elseif (-not $passwordLength) {
$PasswordLength = 20
}
try {
New-LocalUserFromNinja @params
}
catch {
Write-Error $_
exit 1
}
}
end {
}