forked from bwya77/PSHTML-AD-Report
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathPSHTML-AD.ps1
1996 lines (1487 loc) · 61.9 KB
/
PSHTML-AD.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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<#
.SYNOPSIS
Generate graphed report for all Active Directory objects.
.DESCRIPTION
Generate graphed report for all Active Directory objects.
Forked and updated by @vhoudoverdov to support the enumeration of vCenter components.
.PARAMETER CompanyLogo
Enter URL or UNC path to your desired Company Logo for generated report.
-CompanyLogo "\\Server01\Admin\Files\CompanyLogo.png"
.PARAMETER RightLogo
Enter URL or UNC path to your desired right-side logo for generated report.
-RightLogo "https://www.psmpartners.com/wp-content/uploads/2017/10/porcaro-stolarek-mete.png"
.PARAMETER ReportTitle
Enter desired title for generated report.
-ReportTitle "Active Directory Report"
.PARAMETER Days
Users that have not logged in [X] amount of days or more.
-Days "30"
.PARAMETER UserCreatedDays
Users that have been created within [X] amount of days.
-UserCreatedDays "7"
.PARAMETER DaysUntilPWExpireINT
Users password expires within [X] amount of days
-DaysUntilPWExpireINT "7"
.PARAMETER ADModNumber
Active Directory Objects that have been modified within [X] amount of days.
-ADModNumber "3"
.NOTES
Version: 1.0.3
Author: Bradley Wyatt
Date: 12/4/2018
Fork Author: Vasken Houdoverdov
Fork Date: 12/10/2018
Modified: vhoudoverdov 12/10/2018
Modified: JBear 12/5/2018
Bradley Wyatt 12/8/2018
jporgand 12/6/2018
#>
param (
#Company logo that will be displayed on the left, can be URL or UNC
[Parameter(ValueFromPipeline = $true, HelpMessage = "Enter URL or UNC path to Company Logo")]
[String]$CompanyLogo = "",
#Logo that will be on the right side, UNC or URL
[Parameter(ValueFromPipeline = $true, HelpMessage = "Enter URL or UNC path for Side Logo")]
[String]$RightLogo = "https://www.psmpartners.com/wp-content/uploads/2017/10/porcaro-stolarek-mete.png",
#Title of generated report
[Parameter(ValueFromPipeline = $true, HelpMessage = "Enter desired title for report")]
[String]$ReportTitle = "Active Directory Report",
#Location the report will be saved to
[Parameter(ValueFromPipeline = $true, HelpMessage = "Enter desired directory path to save; Default: C:\Automation\")]
[String]$ReportSavePath = "C:\Automation\",
#Find users that have not logged in X Amount of days, this sets the days
[Parameter(ValueFromPipeline = $true, HelpMessage = "Users that have not logged on in more than [X] days. amount of days; Default: 30")]
$Days = 30,
#Get users who have been created in X amount of days and less
[Parameter(ValueFromPipeline = $true, HelpMessage = "Users that have been created within [X] amount of days; Default: 7")]
$UserCreatedDays = 7,
#Get users whos passwords expire in less than X amount of days
[Parameter(ValueFromPipeline = $true, HelpMessage = "Users password expires within [X] amount of days; Default: 7")]
$DaysUntilPWExpireINT = 7,
#Get AD Objects that have been modified in X days and newer
[Parameter(ValueFromPipeline = $true, HelpMessage = "AD Objects that have been modified within [X] amount of days; Default: 3")]
$ADModNumber = 3
#CSS template located C:\Program Files\WindowsPowerShell\Modules\ReportHTML\1.4.1.1\
#Default template is orange and named "Sample"
)
ipmo VMware.VimAutomation.Core
Write-Host "Gathering Report Customization..." -ForegroundColor White
Write-Host "__________________________________" -ForegroundColor White
(Write-Host -NoNewline "Company Logo (left): " -ForegroundColor Yellow), (Write-Host $CompanyLogo -ForegroundColor White)
(Write-Host -NoNewline "Company Logo (right): " -ForegroundColor Yellow), (Write-Host $RightLogo -ForegroundColor White)
(Write-Host -NoNewline "Report Title: " -ForegroundColor Yellow), (Write-Host $ReportTitle -ForegroundColor White)
(Write-Host -NoNewline "Report Save Path: " -ForegroundColor Yellow), (Write-Host $ReportSavePath -ForegroundColor White)
(Write-Host -NoNewline "Amount of Days from Last User Logon Report: " -ForegroundColor Yellow), (Write-Host $Days -ForegroundColor White)
(Write-Host -NoNewline "Amount of Days for New User Creation Report: " -ForegroundColor Yellow), (Write-Host $UserCreatedDays -ForegroundColor White)
(Write-Host -NoNewline "Amount of Days for User Password Expiration Report: " -ForegroundColor Yellow), (Write-Host $DaysUntilPWExpireINT -ForegroundColor White)
(Write-Host -NoNewline "Amount of Days for Newly Modified AD Objects Report: " -ForegroundColor Yellow), (Write-Host $ADModNumber -ForegroundColor White)
Write-Host "__________________________________" -ForegroundColor White
function LastLogonConvert ($ftDate)
{
$Date = [DateTime]::FromFileTime($ftDate)
if ($Date -lt (Get-Date '1/1/1900') -or $date -eq 0 -or $date -eq $null)
{
"Never"
}
else
{
$Date
}
} #End function LastLogonConvert
#Check for ReportHTML Module
$Mod = Get-Module -ListAvailable -Name "ReportHTML"
If ($null -eq $Mod)
{
Write-Host "ReportHTML Module is not present, attempting to install it"
Install-Module -Name ReportHTML -Force
Import-Module ReportHTML -ErrorAction SilentlyContinue
}
#Array of default Security Groups
$DefaultSGs = @(
"Access Control Assistance Operators"
"Account Operators"
"Administrators"
"Allowed RODC Password Replication Group"
"Backup Operators"
"Certificate Service DCOM Access"
"Cert Publishers"
"Cloneable Domain Controllers"
"Cryptographic Operators"
"Denied RODC Password Replication Group"
"Distributed COM Users"
"DnsUpdateProxy"
"DnsAdmins"
"Domain Admins"
"Domain Computers"
"Domain Controllers"
"Domain Guests"
"Domain Users"
"Enterprise Admins"
"Enterprise Key Admins"
"Enterprise Read-only Domain Controllers"
"Event Log Readers"
"Group Policy Creator Owners"
"Guests"
"Hyper-V Administrators"
"IIS_IUSRS"
"Incoming Forest Trust Builders"
"Key Admins"
"Network Configuration Operators"
"Performance Log Users"
"Performance Monitor Users"
"Print Operators"
"Pre-Windows 2000 Compatible Access"
"Protected Users"
"RAS and IAS Servers"
"RDS Endpoint Servers"
"RDS Management Servers"
"RDS Remote Access Servers"
"Read-only Domain Controllers"
"Remote Desktop Users"
"Remote Management Users"
"Replicator"
"Schema Admins"
"Server Operators"
"Storage Replica Administrators"
"System Managed Accounts Group"
"Terminal Server License Servers"
"Users"
"Windows Authorization Access Group"
"WinRMRemoteWMIUsers"
)
$Table = New-Object 'System.Collections.Generic.List[System.Object]'
$OUTable = New-Object 'System.Collections.Generic.List[System.Object]'
$UserTable = New-Object 'System.Collections.Generic.List[System.Object]'
$GroupTypetable = New-Object 'System.Collections.Generic.List[System.Object]'
$DefaultGrouptable = New-Object 'System.Collections.Generic.List[System.Object]'
$EnabledDisabledUsersTable = New-Object 'System.Collections.Generic.List[System.Object]'
$DomainAdminTable = New-Object 'System.Collections.Generic.List[System.Object]'
$ExpiringAccountsTable = New-Object 'System.Collections.Generic.List[System.Object]'
$CompanyInfoTable = New-Object 'System.Collections.Generic.List[System.Object]'
$securityeventtable = New-Object 'System.Collections.Generic.List[System.Object]'
$DomainTable = New-Object 'System.Collections.Generic.List[System.Object]'
$OUGPOTable = New-Object 'System.Collections.Generic.List[System.Object]'
$GroupMembershipTable = New-Object 'System.Collections.Generic.List[System.Object]'
$PasswordExpirationTable = New-Object 'System.Collections.Generic.List[System.Object]'
$PasswordExpireSoonTable = New-Object 'System.Collections.Generic.List[System.Object]'
$userphaventloggedonrecentlytable = New-Object 'System.Collections.Generic.List[System.Object]'
$EnterpriseAdminTable = New-Object 'System.Collections.Generic.List[System.Object]'
$NewCreatedUsersTable = New-Object 'System.Collections.Generic.List[System.Object]'
$GroupProtectionTable = New-Object 'System.Collections.Generic.List[System.Object]'
$OUProtectionTable = New-Object 'System.Collections.Generic.List[System.Object]'
$GPOTable = New-Object 'System.Collections.Generic.List[System.Object]'
$ADObjectTable = New-Object 'System.Collections.Generic.List[System.Object]'
$ProtectedUsersTable = New-Object 'System.Collections.Generic.List[System.Object]'
$ComputersTable = New-Object 'System.Collections.Generic.List[System.Object]'
$ComputerProtectedTable = New-Object 'System.Collections.Generic.List[System.Object]'
$ComputersEnabledTable = New-Object 'System.Collections.Generic.List[System.Object]'
$DefaultComputersinDefaultOUTable = New-Object 'System.Collections.Generic.List[System.Object]'
$DefaultUsersinDefaultOUTable = New-Object 'System.Collections.Generic.List[System.Object]'
$TOPUserTable = New-Object 'System.Collections.Generic.List[System.Object]'
$TOPGroupsTable = New-Object 'System.Collections.Generic.List[System.Object]'
$TOPComputersTable = New-Object 'System.Collections.Generic.List[System.Object]'
$GraphComputerOS = New-Object 'System.Collections.Generic.List[System.Object]'
$VmwareVmList = New-Object 'System.Collections.Generic.List[System.Object]'
$OutdatedVMwareTools = New-Object 'System.Collections.Generic.List[System.Object]'
$OpenSnapshotTable = New-Object 'System.Collections.Generic.List[System.Object]'
$DatastoreTable = New-Object 'System.Collections.Generic.List[System.Object]'
$PortGroupTable = New-Object 'System.Collections.Generic.List[System.Object]'
$VcenterAlarmTable = New-Object 'System.Collections.Generic.List[System.Object]'
$EsxiHostTable = New-Object 'System.Collections.Generic.List[System.Object]'
#Get all users right away. Instead of doing several lookups, we will use this object to look up all the information needed.
$AllUsers = Get-ADUser -Filter * -Properties *
$GPOs = Get-GPO -All | Select-Object DisplayName, GPOStatus, ModificationTime, @{ Label = "ComputerVersion"; Expression = { $_.computer.dsversion } }, @{ Label = "UserVersion"; Expression = { $_.user.dsversion } }
<###########################
Dashboard
############################>
Write-Host "Working on Dashboard Report..." -ForegroundColor Green
$dte = (Get-Date).AddDays(- $ADModNumber)
$ADObjs = Get-ADObject -Filter { whenchanged -gt $dte -and ObjectClass -ne "domainDNS" -and ObjectClass -ne "rIDManager" -and ObjectClass -ne "rIDSet" } -Properties *
foreach ($ADObj in $ADObjs)
{
if ($ADObj.ObjectClass -eq "GroupPolicyContainer")
{
$Name = $ADObj.DisplayName
}
else
{
$Name = $ADObj.Name
}
$obj = [PSCustomObject]@{
'Name' = $Name
'Object Type' = $ADObj.ObjectClass
'When Changed' = $ADObj.WhenChanged
}
$ADObjectTable.Add($obj)
}
if (($ADObjectTable).Count -eq 0)
{
$Obj = [PSCustomObject]@{
Information = 'Information: No AD Objects have been modified recently'
}
$ADObjectTable.Add($obj)
}
$ADRecycleBinStatus = (Get-ADOptionalFeature -Filter 'name -like "Recycle Bin Feature"').EnabledScopes
if ($ADRecycleBinStatus.Count -lt 1)
{
$ADRecycleBin = "Disabled"
}
else
{
$ADRecycleBin = "Enabled"
}
#Company Information
$ADInfo = Get-ADDomain
$ForestObj = Get-ADForest
$DomainControllerobj = Get-ADDomain
$Forest = $ADInfo.Forest
$InfrastructureMaster = $DomainControllerobj.InfrastructureMaster
$RIDMaster = $DomainControllerobj.RIDMaster
$PDCEmulator = $DomainControllerobj.PDCEmulator
$DomainNamingMaster = $ForestObj.DomainNamingMaster
$SchemaMaster = $ForestObj.SchemaMaster
$obj = [PSCustomObject]@{
'Domain' = $Forest
'AD Recycle Bin' = $ADRecycleBin
'Infrastructure Master' = $InfrastructureMaster
'RID Master' = $RIDMaster
'PDC Emulator' = $PDCEmulator
'Domain Naming Master' = $DomainNamingMaster
'Schema Master' = $SchemaMaster
}
$CompanyInfoTable.Add($obj)
if (($CompanyInfoTable).Count -eq 0)
{
$Obj = [PSCustomObject]@{
Information = 'Information: Could not get items for table'
}
$CompanyInfoTable.Add($obj)
}
#Get newly created users
$When = ((Get-Date).AddDays(- $UserCreatedDays)).Date
$NewUsers = $AllUsers | Where-Object { $_.whenCreated -ge $When }
foreach ($Newuser in $Newusers)
{
$obj = [PSCustomObject]@{
'Name' = $Newuser.Name
'Enabled' = $Newuser.Enabled
'Creation Date' = $Newuser.whenCreated
}
$NewCreatedUsersTable.Add($obj)
}
if (($NewCreatedUsersTable).Count -eq 0)
{
$Obj = [PSCustomObject]@{
Information = 'Information: No new users have been recently created'
}
$NewCreatedUsersTable.Add($obj)
}
#Get Domain Admins
$DomainAdminMembers = Get-ADGroupMember "Domain Admins"
foreach ($DomainAdminMember in $DomainAdminMembers)
{
$Name = $DomainAdminMember.Name
$Type = $DomainAdminMember.ObjectClass
$Enabled = ($AllUsers | Where-Object { $_.Name -eq $Name }).Enabled
$obj = [PSCustomObject]@{
'Name' = $Name
'Enabled' = $Enabled
'Type' = $Type
}
$DomainAdminTable.Add($obj)
}
if (($DomainAdminTable).Count -eq 0)
{
$Obj = [PSCustomObject]@{
Information = 'Information: No Domain Admin Members were found'
}
$DomainAdminTable.Add($obj)
}
#Get Enterprise Admins
$EnterpriseAdminsMembers = Get-ADGroupMember "Enterprise Admins" -Server $SchemaMaster
foreach ($EnterpriseAdminsMember in $EnterpriseAdminsMembers)
{
$Name = $EnterpriseAdminsMember.Name
$Type = $EnterpriseAdminsMember.ObjectClass
$Enabled = ($AllUsers | Where-Object { $_.Name -eq $Name }).Enabled
$obj = [PSCustomObject]@{
'Name' = $Name
'Enabled' = $Enabled
'Type' = $Type
}
$EnterpriseAdminTable.Add($obj)
}
if (($EnterpriseAdminTable).Count -eq 0)
{
$Obj = [PSCustomObject]@{
Information = 'Information: Enterprise Admin members were found'
}
$EnterpriseAdminTable.Add($obj)
}
$DefaultComputersOU = (Get-ADDomain).computerscontainer
$DefaultComputers = Get-ADComputer -Filter * -Properties * -SearchBase "$DefaultComputersOU"
foreach ($DefaultComputer in $DefaultComputers)
{
$obj = [PSCustomObject]@{
'Name' = $DefaultComputer.Name
'Enabled' = $DefaultComputer.Enabled
'Operating System' = $DefaultComputer.OperatingSystem
'Modified Date' = $DefaultComputer.Modified
'Password Last Set' = $DefaultComputer.PasswordLastSet
'Protect from Deletion' = $DefaultComputer.ProtectedFromAccidentalDeletion
}
$DefaultComputersinDefaultOUTable.Add($obj)
}
if (($DefaultComputersinDefaultOUTable).Count -eq 0)
{
$Obj = [PSCustomObject]@{
Information = 'Information: No computers were found in the Default OU'
}
$DefaultComputersinDefaultOUTable.Add($obj)
}
$DefaultUsersOU = (Get-ADDomain).UsersContainer
$DefaultUsers = $Allusers | Where-Object { $_.DistinguishedName -like "*$($DefaultUsersOU)" } | Select-Object Name, UserPrincipalName, Enabled, ProtectedFromAccidentalDeletion, EmailAddress, @{ Name = 'lastlogon'; Expression = { LastLogonConvert $_.lastlogon } }, DistinguishedName
foreach ($DefaultUser in $DefaultUsers)
{
$obj = [PSCustomObject]@{
'Name' = $DefaultUser.Name
'UserPrincipalName' = $DefaultUser.UserPrincipalName
'Enabled' = $DefaultUser.Enabled
'Protected from Deletion' = $DefaultUser.ProtectedFromAccidentalDeletion
'Last Logon' = $DefaultUser.LastLogon
'Email Address' = $DefaultUser.EmailAddress
}
$DefaultUsersinDefaultOUTable.Add($obj)
}
if (($DefaultUsersinDefaultOUTable).Count -eq 0)
{
$Obj = [PSCustomObject]@{
Information = 'Information: No Users were found in the default OU'
}
$DefaultUsersinDefaultOUTable.Add($obj)
}
#Expiring Accounts
$LooseUsers = Search-ADAccount -AccountExpiring -UsersOnly
foreach ($LooseUser in $LooseUsers)
{
$NameLoose = $LooseUser.Name
$UPNLoose = $LooseUser.UserPrincipalName
$ExpirationDate = $LooseUser.AccountExpirationDate
$enabled = $LooseUser.Enabled
$obj = [PSCustomObject]@{
'Name' = $NameLoose
'UserPrincipalName' = $UPNLoose
'Expiration Date' = $ExpirationDate
'Enabled' = $enabled
}
$ExpiringAccountsTable.Add($obj)
}
if (($ExpiringAccountsTable).Count -eq 0)
{
$Obj = [PSCustomObject]@{
Information = 'Information: No Users were found to expire soon'
}
$ExpiringAccountsTable.Add($obj)
}
#Security Logs
$SecurityLogs = Get-EventLog -Newest 7 -LogName "Security" | Where-Object { $_.Message -like "*An account*" }
foreach ($SecurityLog in $SecurityLogs)
{
$TimeGenerated = $SecurityLog.TimeGenerated
$EntryType = $SecurityLog.EntryType
$Recipient = $SecurityLog.Message
$obj = [PSCustomObject]@{
'Time' = $TimeGenerated
'Type' = $EntryType
'Message' = $Recipient
}
$SecurityEventTable.Add($obj)
}
if (($SecurityEventTable).Count -eq 0)
{
$Obj = [PSCustomObject]@{
Information = 'Information: No logon security events were found'
}
$SecurityEventTable.Add($obj)
}
#Tenant Domain
$Domains = Get-ADForest | Select-Object -ExpandProperty upnsuffixes | ForEach-Object{
$obj = [PSCustomObject]@{
'UPN Suffixes' = $_
Valid = "True"
}
$DomainTable.Add($obj)
}
if (($DomainTable).Count -eq 0)
{
$Obj = [PSCustomObject]@{
Information = 'Information: No UPN Suffixes were found'
}
$DomainTable.Add($obj)
}
Write-Host "Done!" -ForegroundColor White
<###########################
Groups
############################>
Write-Host "Working on Groups Report..." -ForegroundColor Green
#Get groups and sort in alphabetical order
$Groups = Get-ADGroup -Filter * -Properties *
$SecurityCount = 0
$MailSecurityCount = 0
$CustomGroup = 0
$DefaultGroup = 0
$Groupswithmemebrship = 0
$Groupswithnomembership = 0
$GroupsProtected = 0
$GroupsNotProtected = 0
foreach ($Group in $Groups)
{
$DefaultADGroup = 'False'
$Type = New-Object 'System.Collections.Generic.List[System.Object]'
$Gemail = (Get-ADGroup $Group -Properties mail).mail
if (($group.GroupCategory -eq "Security") -and ($Gemail -ne $Null))
{
$MailSecurityCount++
}
if (($group.GroupCategory -eq "Security") -and (($Gemail) -eq $Null))
{
$SecurityCount++
}
if ($Group.ProtectedFromAccidentalDeletion -eq $True)
{
$GroupsProtected++
}
else
{
$GroupsNotProtected++
}
if ($DefaultSGs -contains $Group.Name)
{
$DefaultADGroup = "True"
$DefaultGroup++
}
else
{
$CustomGroup++
}
if ($group.GroupCategory -eq "Distribution")
{
$Type = "Distribution Group"
}
if (($group.GroupCategory -eq "Security") -and (($Gemail) -eq $Null))
{
$Type = "Security Group"
}
if (($group.GroupCategory -eq "Security") -and (($Gemail) -ne $Null))
{
$Type = "Mail-Enabled Security Group"
}
if ($Group.Name -ne "Domain Users")
{
$Users = (Get-ADGroupMember -Identity $Group | Sort-Object DisplayName | Select-Object -ExpandProperty Name) -join ", "
if (!($Users))
{
$Groupswithnomembership++
}
else
{
$Groupswithmemebrship++
}
}
else
{
$Users = "Skipped Domain Users Membership"
}
$OwnerDN = Get-ADGroup -Filter { name -eq $Group.Name } -Properties managedBy | Select-Object -ExpandProperty ManagedBy
Try
{
$Manager = Get-ADUser -Filter { distinguishedname -like $OwnerDN } | Select-Object -ExpandProperty Name
}
Catch
{
write-host -ForegroundColor Yellow "Cannot resolve the manager, " $Manager " on the group " $group.name
}
#$Manager = $AllUsers | Where-Object { $_.distinguishedname -eq $OwnerDN } | Select-Object -ExpandProperty Name
$obj = [PSCustomObject]@{
'Name' = $Group.name
'Type' = $Type
'Members' = $users
'Managed By' = $Manager
'E-mail Address' = $GEmail
'Protected from Deletion' = $Group.ProtectedFromAccidentalDeletion
'Default AD Group' = $DefaultADGroup
}
$table.Add($obj)
}
if (($table).Count -eq 0)
{
$Obj = [PSCustomObject]@{
Information = 'Information: No Groups were found'
}
$table.Add($obj)
}
#TOP groups table
$obj1 = [PSCustomObject]@{
'Total Groups' = $Groups.Count
'Mail-Enabled Security Groups' = $MailSecurityCount
'Security Groups' = $SecurityCount
'Distribution Groups' = $DistroCount
}
$TOPGroupsTable.Add($obj1)
$obj1 = [PSCustomObject]@{
'Name' = 'Mail-Enabled Security Groups'
'Count' = $MailSecurityCount
}
$GroupTypetable.Add($obj1)
$obj1 = [PSCustomObject]@{
'Name' = 'Security Groups'
'Count' = $SecurityCount
}
$GroupTypetable.Add($obj1)
$DistroCount = ($Groups | Where-Object { $_.GroupCategory -eq "Distribution" }).Count
$obj1 = [PSCustomObject]@{
'Name' = 'Distribution Groups'
'Count' = $DistroCount
}
$GroupTypetable.Add($obj1)
#Default Group Pie Chart
$obj1 = [PSCustomObject]@{
'Name' = 'Default Groups'
'Count' = $DefaultGroup
}
$DefaultGrouptable.Add($obj1)
$obj1 = [PSCustomObject]@{
'Name' = 'Custom Groups'
'Count' = $CustomGroup
}
$DefaultGrouptable.Add($obj1)
#Group Protection Pie Chart
$obj1 = [PSCustomObject]@{
'Name' = 'Protected'
'Count' = $GroupsProtected
}
$GroupProtectionTable.Add($obj1)
$obj1 = [PSCustomObject]@{
'Name' = 'Not Protected'
'Count' = $GroupsNotProtected
}
$GroupProtectionTable.Add($obj1)
#Groups with membership vs no membership pie chart
$objmem = [PSCustomObject]@{
'Name' = 'With Members'
'Count' = $Groupswithmemebrship
}
$GroupMembershipTable.Add($objmem)
$objmem = [PSCustomObject]@{
'Name' = 'No Members'
'Count' = $Groupswithnomembership
}
$GroupMembershipTable.Add($objmem)
Write-Host "Done!" -ForegroundColor White
<###########################
Organizational Units
############################>
Write-Host "Working on Organizational Units Report..." -ForegroundColor Green
#Get all OUs'
$OUs = Get-ADOrganizationalUnit -Filter * -Properties *
$OUwithLinked = 0
$OUwithnoLink = 0
$OUProtected = 0
$OUNotProtected = 0
foreach ($OU in $OUs)
{
$LinkedGPOs = New-Object 'System.Collections.Generic.List[System.Object]'
if (($OU.linkedgrouppolicyobjects).length -lt 1)
{
$LinkedGPOs = "None"
$OUwithnoLink++
}
else
{
$OUwithLinked++
$GPOslinks = $OU.linkedgrouppolicyobjects
foreach ($GPOlink in $GPOslinks)
{
$Split1 = $GPOlink -split "{" | Select-Object -Last 1
$Split2 = $Split1 -split "}" | Select-Object -First 1
$LinkedGPOs.Add((Get-GPO -Guid $Split2 -ErrorAction SilentlyContinue).DisplayName)
}
}
if ($OU.ProtectedFromAccidentalDeletion -eq $True)
{
$OUProtected++
}
else
{
$OUNotProtected++
}
$LinkedGPOs = $LinkedGPOs -join ", "
$obj = [PSCustomObject]@{
'Name' = $OU.Name
'Linked GPOs' = $LinkedGPOs
'Modified Date' = $OU.WhenChanged
'Protected from Deletion' = $OU.ProtectedFromAccidentalDeletion
}
$OUTable.Add($obj)
}
if (($OUTable).Count -eq 0)
{
$Obj = [PSCustomObject]@{
Information = 'Information: No OUs were found'
}
$OUTable.Add($obj)
}
#OUs with no GPO Linked
$obj1 = [PSCustomObject]@{
'Name' = "OUs with no GPO's linked"
'Count' = $OUwithnoLink
}
$OUGPOTable.Add($obj1)
$obj2 = [PSCustomObject]@{
'Name' = "OUs with GPO's linked"
'Count' = $OUwithLinked
}
$OUGPOTable.Add($obj2)
#OUs Protected Pie Chart
$obj1 = [PSCustomObject]@{
'Name' = "Protected"
'Count' = $OUProtected
}
$OUProtectionTable.Add($obj1)
$obj2 = [PSCustomObject]@{
'Name' = "Not Protected"
'Count' = $OUNotProtected
}
$OUProtectionTable.Add($obj2)
Write-Host "Done!" -ForegroundColor White
<###########################
USERS
############################>
Write-Host "Working on Users Report..." -ForegroundColor Green
$UserEnabled = 0
$UserDisabled = 0
$UserPasswordExpires = 0
$UserPasswordNeverExpires = 0
$ProtectedUsers = 0
$NonProtectedUsers = 0
$UsersWIthPasswordsExpiringInUnderAWeek = 0
$UsersNotLoggedInOver30Days = 0
$AccountsExpiringSoon = 0
#Get users that haven't logged on in X amount of days, var is set at start of script
$userphaventloggedonrecentlytable = New-Object 'System.Collections.Generic.List[System.Object]'
foreach ($User in $AllUsers)
{
$AttVar = $User | Select-Object Enabled, PasswordExpired, PasswordLastSet, PasswordNeverExpires, PasswordNotRequired, Name, SamAccountName, EmailAddress, AccountExpirationDate, @{ Name = 'lastlogon'; Expression = { LastLogonConvert $_.lastlogon } }, DistinguishedName
$maxPasswordAge = (Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge.Days
if ((($AttVar.PasswordNeverExpires) -eq $False) -and (($AttVar.Enabled) -ne $false))
{
#Get Password last set date
$passwordSetDate = ($User | ForEach-Object { $_.PasswordLastSet })
if ($null -eq $passwordSetDate)
{
$daystoexpire = "User has never logged on"
}
else
{
#Check for Fine Grained Passwords
$PasswordPol = (Get-ADUserResultantPasswordPolicy $user)
if (($PasswordPol) -ne $null)
{
$maxPasswordAge = ($PasswordPol).MaxPasswordAge
}
$expireson = $passwordsetdate.AddDays($maxPasswordAge)
$today = (Get-Date)
#Gets the count on how many days until the password expires and stores it in the $daystoexpire var
$daystoexpire = (New-TimeSpan -Start $today -End $Expireson).Days
}
}
else
{
$daystoexpire = "N/A"
}
if (($User.Enabled -eq $True) -and ($AttVar.LastLogon -lt ((Get-Date).AddDays(- $Days))) -and ($User.LastLogon -ne $NULL))
{
$obj = [PSCustomObject]@{
'Name' = $User.Name
'UserPrincipalName' = $User.UserPrincipalName
'Enabled' = $AttVar.Enabled
'Protected from Deletion' = $User.ProtectedFromAccidentalDeletion
'Last Logon' = $AttVar.lastlogon
'Password Never Expires' = $AttVar.PasswordNeverExpires
'Days Until Password Expires' = $daystoexpire
}
$userphaventloggedonrecentlytable.Add($obj)
}
#Items for protected vs non protected users
if ($User.ProtectedFromAccidentalDeletion -eq $False)
{
$NonProtectedUsers++
}
else
{