-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDelegate-Create-Modify-ADuser.ps1
More file actions
147 lines (128 loc) · 4.89 KB
/
Delegate-Create-Modify-ADuser.ps1
File metadata and controls
147 lines (128 loc) · 4.89 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
#region Prep work
$PswaServerName = 'PSWA'
$DomainControllerName = 'LABDC'
$JeaRoleName = 'ADUserManager'
$AdGroupName = 'ADUserManagers'
$DomainName = 'lab.local'
## Run this on $DomainControllerName
New-ADGroup -Name ADUserManagers -GroupScope DomainLocal
## Add any applicable users to the group
# Add-ADGroupMember -Identity ADUserManagers -Members XXXXX
#endregion
#region JEA Setup
#region Create the script that users will run to create new AD users
$functionText = "@
#requires -Module ActiveDirectory
function New-User {
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]`$FirstName,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]`$LastName,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[ValidateScript({
`$deps = 'Accounting', 'Information Services'
if (`$_ -notin `$deps) {
throw `"You have used an invalid department name. Choose from the following: `$(`$deps -join ', ').`"
} else {
`$true
}
})]
[string]`$Department,
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]`$DomainName = 'lab.local'
)
`$userName = '{0}{1}' -f `$FirstName.Substring(0,1),`$LastName
if (Get-AdUser -Filter `"samAccountName -eq '`$userName'`") {
Write-Host `"The username [`$(`$userName)] already exists.`" -ForegroundColor Red
} elseif (-not (Get-ADOrganizationalUnit -Filter `"Name -eq '`$Department'`")) {
Write-Host `"The Active Directory OU for department [`$(`$Department)] could not be found.`" -ForegroundColor Red
} else {
`$password = [System.Web.Security.Membership]::GeneratePassword((Get-Random -Minimum 20 -Maximum 32), 3)
`$secPw = ConvertTo-SecureString -String `$password -AsPlainText -Force
`$ouPath = 'OU={0}, DC={1}, DC={2}' -f `$Department,`$DomainName.Split('.')[0],`$DomainName.Split('.')[1]
`$newUserParams = @{
GivenName = `$FirstName
Surname = `$LastName
Name = `$userName
AccountPassword = `$secPw
ChangePasswordAtLogon = `$true
Enabled = `$true
Department = `$Department
Path = `$ouPath
}
New-AdUser @newUserParams
}
}
@"
Set-Content -Path C:\AdUserInitScript.ps1 -Value $functionText
#endregion
# Create a folder for the module
$modulePath = Join-Path $env:ProgramFiles "WindowsPowerShell\Modules\$JeaRoleName"
$null = New-Item -ItemType Directory -Path $modulePath
# Create an empty script module and module manifest. At least one file in the module folder must have the same name as the folder itself.
$null = New-Item -ItemType File -Path (Join-Path $modulePath "$JeaRoleName.psm1")
New-ModuleManifest -Path (Join-Path $modulePath "$JeaRoleName.psd1") -RootModule "$JeaRoleName.psm1"
# Create the RoleCapabilities folder and copy in the PSRC file
$rcFolder = Join-Path $modulePath "RoleCapabilities"
$null = New-Item -ItemType Directory $rcFolder
$rcCapFilePath = Join-Path -Path $rcFolder -ChildPath "$JeaRoleName.psrc"
$roleCapParams = @{
Path = $rcCapFilePath
VisibleFunctions = 'New-User'
ModulesToImport = 'ActiveDirectory'
AssembliesToLoad = 'System.Web'
VisibleCmdlets = 'ConvertTo-SecureString', @{
Name = 'New-Aduser'
Parameters = @{ Name = 'GivenName' },
@{ Name = 'SurName' },
@{ Name = 'Name' },
@{ Name = 'AccountPassword' },
@{ Name = 'ChangePasswordAtLogon' },
@{ Name = 'Enabled' },
@{ Name = 'Department' },
@{ Name = 'Path' }
},
@{
Name = 'Get-AdUser'
Parameters = @{
Name = 'Filter'
}
},
@{
Name = 'Set-Aduser'
Parameters = @{ Name = 'GivenName' },
@{ Name = 'SurName' },
@{ Name = 'Name' },
@{ Name = 'ChangePasswordAtLogon' },
@{ Name = 'Department' }
}
}
New-PSRoleCapabilityFile @roleCapParams
$sessionFilePath = Join-Path -Path $rcFolder -ChildPath "$JeaRoleName.pssc"
$params = @{
SessionType = 'RestrictedRemoteServer'
Path = $sessionFilePath
RunAsVirtualAccount = $true
ScriptsToProcess = 'C:\AdUserInitScript.ps1'
RoleDefinitions = @{ 'LAB\ADUserManagers' = @{ RoleCapabilities = $JeaRoleName } }
}
New-PSSessionConfigurationFile @params
if (-not (Test-PSSessionConfigurationFile -Path $sessionFilePath)) {
throw 'Failed session configuration file test.'
}
Register-PSSessionConfiguration -Path $sessionFilePath -Name $JeaRoleName -Force
## Test JEA
$nonAdminCred = Get-Credential -Message 'Input user credential to test JEA.'
Invoke-Command -ComputerName $DomainControllerName -ScriptBlock { New-User -FirstName 'Adam' -LastName 'Bertram' -Department 'Information Services' }
#endregion
#region PowerShell Web Access Setup
Add-WindowsFeature -Name WindowsPowerShellWebAccess
Install-PswaWebApplication –UseTestCertificate
Add-PswaAuthorizationRule –ComputerName $DomainControllerName –UserGroupName "$DomainName\$AdGroupName" –ConfigurationName $JeaRoleName
#endregion