Repository navigation
Expand file tree
/
Copy pathAD-Admin-Console-fixed.ps
More file actions
461 lines (295 loc) · 14.1 KB
/
Copy pathAD-Admin-Console-fixed.ps
File metadata and controls
461 lines (295 loc) · 14.1 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<# ===========================================================================
AD-Admin-Console.ps1
A simple, professional, menu-driven “admin console” for common AD tasks.
- Uses Out-GridView for semi-GUI selection when available (WinPS on desktop).
- Falls back to Read-Host prompts if OGV isn’t present.
- Idempotent where possible (existence checks).
- Safe defaults; re-runnable.
Requires: RSAT/ActiveDirectory + GroupPolicy modules on DC01 or an RSAT workstation.
============================================================================ #>
[CmdletBinding(SupportsShouldProcess=$true)]
param()
# ---------------------- Helpers & Prereqs ----------------------
function Test-Module {
param([string]$Name)
$m = Get-Module -ListAvailable -Name $Name
return [bool]$m
}
function Ensure-Modules {
$missing = @()
foreach ($m in @('ActiveDirectory','GroupPolicy')) {
if (-not (Test-Module $m)) { $missing += $m }
}
if ($missing.Count -gt 0) {
throw "Missing modules: $($missing -join ', '). Install RSAT AD & GPMC."
}
Import-Module ActiveDirectory -ErrorAction Stop
Import-Module GroupPolicy -ErrorAction Stop
}
function Get-DomainDN {
try { (Get-ADDomain).DistinguishedName }
catch { throw "Cannot query domain. Are you on a DC or RSAT workstation?" }
}
function Use-OGV { Get-Command Out-GridView -ErrorAction SilentlyContinue }
function Select-OU {
# returns DistinguishedName of selected OU
$ous = Get-ADOrganizationalUnit -Filter * -Properties Name,DistinguishedName |
Sort-Object Name
if ((Use-OGV)) {
$sel = $ous | Select-Object Name,DistinguishedName | Out-GridView -Title "Select an OU" -PassThru
if (-not $sel) { throw "No OU selected." }
return $sel.DistinguishedName
} else {
Write-Host "`nAvailable OUs (first 25):"
$ous | Select-Object -First 25 | ForEach-Object { Write-Host " - $($_.Name)" }
$name = Read-Host "Type OU name (exact match)"
$hit = $ous | Where-Object Name -eq $name
if (-not $hit) { throw "OU not found: $name" }
return $hit.DistinguishedName
}
}
function Select-Users {
$users = Get-ADUser -Filter * -Properties DisplayName,SamAccountName |
Sort-Object DisplayName
if ((Use-OGV)) {
$sel = $users | Select-Object DisplayName,SamAccountName | Out-GridView -Title "Select user(s)" -PassThru -Multiple
if (-not $sel) { throw "No users selected." }
return $sel.SamAccountName
} else {
$inp = Read-Host "Enter one or more sAMAccountNames (comma-separated)"
return ($inp -split ',').ForEach({ $_.Trim() }) | Where-Object { $_ }
}
}
function Select-Groups {
$groups = Get-ADGroup -Filter * -Properties Name | Sort-Object Name
if ((Use-OGV)) {
$sel = $groups | Select-Object Name | Out-GridView -Title "Select group(s)" -PassThru -Multiple
if (-not $sel) { throw "No groups selected." }
return $sel.Name
} else {
$inp = Read-Host "Enter one or more group names (comma-separated)"
return ($inp -split ',').ForEach({ $_.Trim() }) | Where-Object { $_ }
}
}
function Select-Computer {
$comps = Get-ADComputer -Filter * -Properties Name | Sort-Object Name
if ((Use-OGV)) {
$sel = $comps | Select-Object Name | Out-GridView -Title "Select computer" -PassThru
if (-not $sel) { throw "No computer selected." }
return $sel.Name
} else {
return Read-Host "Enter computer name"
}
}
# ---------------------- Tasks ----------------------
function New-OU-Interactive {
$domainDN = Get-DomainDN
Write-Host "`n[Create OU] Choose mode:"
Write-Host " 1) Create under domain root ($domainDN)"
Write-Host " 2) Create under selected parent OU"
$mode = Read-Host "Select 1 or 2"
$parentDN = if ($mode -eq '2') { Select-OU } else { $domainDN }
$name = Read-Host "Enter OU name (single) OR a hierarchy with / separators (e.g., Departments/Engineering)"
if ($name -match '[\\/]' ) {
# Hierarchy
$current = $parentDN
$parts = $name -split '[\\/]'
foreach ($p in $parts) {
$ou = $p.Trim()
if (-not $ou) { continue }
$dn = "OU=$ou,$current"
$exists = Get-ADOrganizationalUnit -LDAPFilter "(distinguishedName=$dn)" -ErrorAction SilentlyContinue
if ($exists) {
Write-Host "OU exists: $dn" -ForegroundColor Yellow
$current = $dn
} else {
if ($PSCmdlet.ShouldProcess($dn, "Create OU")) {
New-ADOrganizationalUnit -Name $ou -Path $current -ProtectedFromAccidentalDeletion $true | Out-Null
Write-Host "Created OU: $dn" -ForegroundColor Green
$current = $dn
}
}
}
Write-Host "Final OU: $current"
} else {
$ou = $name.Trim()
if (-not $ou) { throw "OU name cannot be empty." }
$dn = "OU=$ou,$parentDN"
$exists = Get-ADOrganizationalUnit -LDAPFilter "(distinguishedName=$dn)" -ErrorAction SilentlyContinue
if ($exists) {
Write-Host "OU exists: $dn" -ForegroundColor Yellow
} else {
if ($PSCmdlet.ShouldProcess($dn, "Create OU")) {
New-ADOrganizationalUnit -Name $ou -Path $parentDN -ProtectedFromAccidentalDeletion $true | Out-Null
Write-Host "Created OU: $dn" -ForegroundColor Green
}
}
}
}
function New-Group-Interactive {
$parent = Select-OU
$name = Read-Host "Enter group name"
if (-not $name) { throw "Group name required." }
$dn = "CN=$name,$parent"
$exists = Get-ADGroup -LDAPFilter "(cn=$name)" -SearchBase $parent -ErrorAction SilentlyContinue
if ($exists) { Write-Host "Group exists: $name" -ForegroundColor Yellow; return }
if ($PSCmdlet.ShouldProcess($dn, "Create Group")) {
New-ADGroup -Name $name -GroupScope Global -GroupCategory Security -Path $parent
Write-Host "Created group $name in $parent" -ForegroundColor Green
}
}
function New-User-Interactive {
$parent = Select-OU
$given = Read-Host "First name"
$sn = Read-Host "Last name"
$sam = Read-Host "sAMAccountName (e.g., john.ripper)"
$upn = Read-Host "UserPrincipalName (e.g., john.ripper@ToonWrld.local)"
$pwd = Read-Host "Initial password" -AsSecureString
if ($PSCmdlet.ShouldProcess("CN=$given $sn,$parent", "Create User")) {
try {
New-ADUser -Name "$given $sn" -GivenName $given -Surname $sn `
-SamAccountName $sam -UserPrincipalName $upn -Path $parent `
-AccountPassword $pwd -Enabled $true
Write-Host "Created user $sam in $parent" -ForegroundColor Green
} catch { Write-Warning $_.Exception.Message }
}
}
function New-Users-FromCsv {
$path = Read-Host "CSV path (FirstName,LastName,SamAccountName,UPN,OU,InitialPassword,Groups)"
if (-not (Test-Path $path)) { throw "CSV not found: $path" }
$domainDN = Get-DomainDN
$deptRoot = "OU=Departments,$domainDN"
Import-Csv $path | ForEach-Object {
$user = $_ # ← сохраняем объект
$ouDn = if ($user.OU -match '^OU=') { $user.OU } else { "OU=$($user.OU),$deptRoot" }
$exists = Get-ADUser -LDAPFilter "(sAMAccountName=$($user.SamAccountName))" -ErrorAction SilentlyContinue
if (-not $exists) {
$sec = ConvertTo-SecureString $user.InitialPassword -AsPlainText -Force
try {
New-ADUser -Name "$($user.FirstName) $($user.LastName)" `
-GivenName $user.FirstName -Surname $user.LastName `
-SamAccountName $user.SamAccountName -UserPrincipalName $user.UPN `
-Path $ouDn -AccountPassword $sec -Enabled $true
Write-Host "Created user $($user.SamAccountName) in $ouDn" -ForegroundColor Green
} catch { Write-Warning "User create failed for $($user.SamAccountName): $($_.Exception.Message)" }
} else {
Write-Host "User exists: $($user.SamAccountName)" -ForegroundColor Yellow
}
if ($user.Groups) {
# Разбиваем строку групп и добавляем, используя сохранённый SamAccountName
$sam = $user.SamAccountName # ← фиксируем ДО внутреннего цикла
$user.Groups -split '[;, ]+' | Where-Object { $_ } | ForEach-Object {
$groupName = $_
try {
Add-ADGroupMember -Identity $groupName -Members $sam -ErrorAction Stop
Write-Host " Added $sam -> $groupName" -ForegroundColor Cyan
}
catch {
Write-Warning " Group add failed ($sam -> $groupName): $($_.Exception.Message)"
}
}
}
}
}
function Add-Users-ToGroups {
$users = Select-Users
$groups = Select-Groups
foreach ($u in $users) {
foreach ($g in $groups) {
try { Add-ADGroupMember -Identity $g -Members $u -ErrorAction Stop; Write-Host "Added $u -> $g" -ForegroundColor Green }
catch { Write-Warning "Failed $u -> $g : $($_.Exception.Message)" }
}
}
}
function Move-Computer-ToOU {
$comp = Select-Computer
$target = Select-OU
try {
$obj = Get-ADComputer -Identity $comp -ErrorAction Stop
Move-ADObject -Identity $obj.DistinguishedName -TargetPath $target
Write-Host "Moved $comp to $target" -ForegroundColor Green
} catch { Write-Warning $_.Exception.Message }
}
function Set-LogonBanner-GPO {
$targetOU = Select-OU
$gpoName = Read-Host "GPO name (default: Pied Piper Banner)"
if (-not $gpoName) { $gpoName = "Pied Piper Banner" }
$title = Read-Host "Banner title (default: Pied Piper Security Notice)"
if (-not $title) { $title = "Pied Piper Security Notice" }
$text = Read-Host "Banner text (lines allowed). End with Enter."
if (-not $text) { $text = "Authorized access only.`nAll activity is monitored.`nPied Piper Systems Confidential." }
$gpo = Get-GPO -Name $gpoName -ErrorAction SilentlyContinue
if (-not $gpo) { $gpo = New-GPO -Name $gpoName; Write-Host "Created GPO: $gpoName" -ForegroundColor Green }
New-GPLink -Name $gpoName -Target $targetOU -ErrorAction SilentlyContinue | Out-Null
Set-GPRegistryValue -Name $gpoName -Key "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System" -ValueName "legalnoticecaption" -Type String -Value $title
Set-GPRegistryValue -Name $gpoName -Key "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System" -ValueName "legalnoticetext" -Type String -Value $text
Write-Host "Banner configured & linked to $targetOU" -ForegroundColor Green
}
function Delegate-ResetPassword {
$ou = Select-OU
$group = Read-Host "Delegate to which group (e.g., BizOps)"
if (-not $group) { throw "Group name required." }
$domain = (Get-ADDomain).NetBIOSName
& dsacls $ou /I:S /G "$domain\$group:CA;Reset Password" | Out-Null
& dsacls $ou /I:S /G "$domain\$group:WP;lockoutTime;user" | Out-Null
Write-Host "Delegation applied to $ou for $domain\$group" -ForegroundColor Green
}
function Export-AD-State {
$stamp = (Get-Date).ToString('yyyyMMdd-HHmm')
$out = Join-Path $PSScriptRoot "..\artifacts\$stamp"
New-Item -ItemType Directory -Force -Path $out | Out-Null
Get-ADOrganizationalUnit -Filter * | Select Name,DistinguishedName |
Export-Csv (Join-Path $out "OU-Inventory.csv") -NoTypeInformation
Get-ADGroup -Filter * | Select Name,GroupScope,GroupCategory,DistinguishedName |
Export-Csv (Join-Path $out "Group-Inventory.csv") -NoTypeInformation
Get-ADUser -Filter * -Properties DisplayName,Department,Enabled,PasswordLastSet,LastLogonDate |
Select DisplayName,SamAccountName,UserPrincipalName,Department,Enabled,PasswordLastSet,LastLogonDate,DistinguishedName |
Export-Csv (Join-Path $out "User-Inventory.csv") -NoTypeInformation
Get-ADComputer -Filter * -Properties OperatingSystem,LastLogonDate |
Select Name,OperatingSystem,LastLogonDate,DistinguishedName |
Export-Csv (Join-Path $out "Computer-Inventory.csv") -NoTypeInformation
$gpoHtml = Join-Path $out "GPO-Report.html"
Get-GPOReport -All -ReportType Html -Path $gpoHtml
Write-Host "Exported artifacts to $out" -ForegroundColor Green
}
# ---------------------- Menu ----------------------
function Show-Menu {
Clear-Host
Write-Host "==================================================" -ForegroundColor Cyan
Write-Host " Active Directory Admin Console " -ForegroundColor Cyan
Write-Host "==================================================" -ForegroundColor Cyan
Write-Host " [1] Create OU (single / hierarchy)"
Write-Host " [2] Create Group"
Write-Host " [3] Create User (single)"
Write-Host " [4] Create Users from CSV"
Write-Host " [5] Add Users to Groups"
Write-Host " [6] Move Computer to OU"
Write-Host " [7] Set 'Logon Banner' GPO"
Write-Host " [8] Delegate 'Reset Password' on an OU"
Write-Host " [9] Export AD State (CSV + GPO report)"
Write-Host " [Q] Quit"
Write-Host "--------------------------------------------------"
}
try {
Ensure-Modules
while ($true) {
Show-Menu
$choice = Read-Host "Select an option"
switch ($choice.ToUpper()) {
'1' { New-OU-Interactive; Pause }
'2' { New-Group-Interactive; Pause }
'3' { New-User-Interactive; Pause }
'4' { New-Users-FromCsv; Pause }
'5' { Add-Users-ToGroups; Pause }
'6' { Move-Computer-ToOU; Pause }
'7' { Set-LogonBanner-GPO; Pause }
'8' { Delegate-ResetPassword; Pause }
'9' { Export-AD-State; Pause }
'Q' { break }
default { Write-Host "Invalid selection."; Pause }
}
}
}
catch {
Write-Error $_.Exception.Message
}