From 8001bd484783c998f2236962ce8b90b43a9ced78 Mon Sep 17 00:00:00 2001 From: Brad Wyatt Date: Thu, 6 Dec 2018 21:13:50 -0600 Subject: [PATCH 1/3] Empty Table Fixes Fixing errors from empty tables --- PSHTML-AD.ps1 | 307 +++++++++++++++++++++++++++++++------------------- 1 file changed, 192 insertions(+), 115 deletions(-) diff --git a/PSHTML-AD.ps1 b/PSHTML-AD.ps1 index 0df6922..108812a 100644 --- a/PSHTML-AD.ps1 +++ b/PSHTML-AD.ps1 @@ -87,13 +87,13 @@ param ( 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 "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 @@ -255,6 +255,17 @@ foreach ($ADObj in $ADObjs) $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 @@ -294,6 +305,16 @@ $obj = [PSCustomObject]@{ $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 } @@ -310,17 +331,18 @@ foreach ($Newuser in $Newusers) $NewCreatedUsersTable.Add($obj) } - - if (($NewCreatedUsersTable).Count -eq 0) { - $NewCreatedUsersTable = [PSCustomObject]@{ + $Obj = [PSCustomObject]@{ - Information = 'Information: No New Users Have Been Created Recently' + Information = 'Information: No new users have been recently created' } + $NewCreatedUsersTable.Add($obj) } + + #Get Domain Admins $DomainAdminMembers = Get-ADGroupMember "Domain Admins" @@ -341,6 +363,17 @@ foreach ($DomainAdminMember in $DomainAdminMembers) $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" @@ -361,6 +394,16 @@ foreach ($EnterpriseAdminsMember in $EnterpriseAdminsMembers) $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" @@ -380,6 +423,16 @@ foreach ($DefaultComputer in $DefaultComputers) $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 @@ -398,16 +451,17 @@ foreach ($DefaultUser in $DefaultUsers) $DefaultUsersinDefaultOUTable.Add($obj) } - if (($DefaultUsersinDefaultOUTable).Count -eq 0) { - $DefaultUsersinDefaultOUTable = [PSCustomObject]@{ + $Obj = [PSCustomObject]@{ - Information = 'Information: No Computers were found in the Default OU' + Information = 'Information: No Users were found in the default OU' } + $DefaultUsersinDefaultOUTable.Add($obj) } + #Expiring Accounts $LooseUsers = Search-ADAccount -AccountExpiring -UsersOnly @@ -433,10 +487,11 @@ foreach ($LooseUser in $LooseUsers) if (($ExpiringAccountsTable).Count -eq 0) { - $ExpiringAccountsTable = [PSCustomObject]@{ + $Obj = [PSCustomObject]@{ Information = 'Information: No Users were found to expire soon' } + $ExpiringAccountsTable.Add($obj) } #Security Logs @@ -459,13 +514,14 @@ foreach ($SecurityLog in $SecurityLogs) $SecurityEventTable.Add($obj) } -if (($securityeventtable).Count -eq 0) +if (($SecurityEventTable).Count -eq 0) { - $securityeventtable = [PSCustomObject]@{ + $Obj = [PSCustomObject]@{ - Information = 'Information: No recent security logs' + Information = 'Information: No logon security events were found' } + $SecurityEventTable.Add($obj) } #Tenant Domain @@ -479,6 +535,15 @@ $Domains = Get-ADForest | Select-Object -ExpandProperty upnsuffixes | ForEach-Ob $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 @@ -617,12 +682,12 @@ foreach ($Group in $Groups) if (($table).Count -eq 0) { - $table = [PSCustomObject]@{ + $Obj = [PSCustomObject]@{ Information = 'Information: No Groups were found' } + $table.Add($obj) } - #TOP groups table $obj1 = [PSCustomObject]@{ @@ -781,10 +846,11 @@ foreach ($OU in $OUs) if (($OUTable).Count -eq 0) { - $OUTable = [PSCustomObject]@{ + $Obj = [PSCustomObject]@{ - Information = 'Information: No Organizational Units were found' + Information = 'Information: No OUs were found' } + $OUTable.Add($obj) } #OUs with no GPO Linked @@ -846,7 +912,7 @@ 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 + $maxPasswordAge = (Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge.Days if ((($AttVar.PasswordNeverExpires) -eq $False) -and (($AttVar.Enabled) -ne $false)) { @@ -872,7 +938,7 @@ foreach ($User in $AllUsers) $maxPasswordAge = ($PasswordPol).MaxPasswordAge } - $expireson = $passwordsetdate + $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 @@ -995,10 +1061,11 @@ foreach ($User in $AllUsers) if (($usertable).Count -eq 0) { - $usertable = [PSCustomObject]@{ + $Obj = [PSCustomObject]@{ - Information = 'Information: No Users were found' + Information = 'Information: No users were found' } + $usertable.Add($obj) } #Data for users enabled vs disabled pie graph @@ -1103,6 +1170,15 @@ foreach ($GPO in $GPOs) $GPOTable.Add($obj) } +if (($GPOTable).Count -eq 0) +{ + + $Obj = [PSCustomObject]@{ + + Information = 'Information: No Group Policy Obejects were found' + } + $GPOTable.Add($obj) +} Write-Host "Done!" -ForegroundColor White <########################### @@ -1207,13 +1283,14 @@ foreach ($Computer in $Computers) } } -If (($ComputersTable).Count -eq 0) +if (($ComputersTable).Count -eq 0) { - $ComputersTable = [PSCustomObject]@{ + $Obj = [PSCustomObject]@{ - Information = 'Information: No Computers were found' + Information = 'Information: No computers were found' } + $ComputersTable.Add($obj) } #Data for TOP Computers data table @@ -1710,89 +1787,89 @@ $FinalReport.Add($(Get-HTMLTabContentopen -TabName $tabarray[3] -TabHeading ("Re $FinalReport.Add($(Get-HTMLContentOpen -HeaderText "Users Overivew")) $FinalReport.Add($(Get-HTMLContentTable $TOPUserTable -HideFooter)) - $FinalReport.Add($(Get-HTMLContentClose)) - - $FinalReport.Add($(Get-HTMLContentOpen -HeaderText "Active Directory Users")) - $FinalReport.Add($(Get-HTMLContentDataTable $UserTable -HideFooter)) - $FinalReport.Add($(Get-HTMLContentClose)) - - $FinalReport.Add($(Get-HTMLContentOpen -HeaderText "Expiring Items")) - $FinalReport.Add($(Get-HTMLColumn1of2)) - $FinalReport.Add($(Get-HTMLContentOpen -BackgroundShade 1 -HeaderText "Users with Passwords Expiring in less than $DaysUntilPWExpireINT days")) - $FinalReport.Add($(Get-HTMLContentDataTable $PasswordExpireSoonTable -HideFooter)) - $FinalReport.Add($(Get-HTMLContentClose)) - $FinalReport.Add($(Get-HTMLColumnClose)) - $FinalReport.Add($(Get-HTMLColumn2of2)) - $FinalReport.Add($(Get-HTMLContentOpen -HeaderText 'Accounts Expiring Soon')) - $FinalReport.Add($(Get-HTMLContentDataTable $ExpiringAccountsTable -HideFooter)) - $FinalReport.Add($(Get-HTMLContentClose)) - $FinalReport.Add($(Get-HTMLColumnClose)) - $FinalReport.Add($(Get-HTMLContentClose)) - - $FinalReport.Add($(Get-HTMLContentOpen -HeaderText "Accounts")) - $FinalReport.Add($(Get-HTMLColumn1of2)) - $FinalReport.Add($(Get-HTMLContentOpen -BackgroundShade 1 -HeaderText "Users Haven't Logged on in $Days Days")) - $FinalReport.Add($(Get-HTMLContentDataTable $userphaventloggedonrecentlytable -HideFooter)) - $FinalReport.Add($(Get-HTMLContentClose)) - $FinalReport.Add($(Get-HTMLColumnClose)) - $FinalReport.Add($(Get-HTMLColumn2of2)) - - $FinalReport.Add($(Get-HTMLContentOpen -BackgroundShade 1 -HeaderText "Accounts Created in $UserCreatedDays Days or Less")) - $FinalReport.Add($(Get-HTMLContentDataTable $NewCreatedUsersTable -HideFooter)) - $FinalReport.Add($(Get-HTMLContentClose)) - $FinalReport.Add($(Get-HTMLColumnClose)) - $FinalReport.Add($(Get-HTMLContentClose)) - - $FinalReport.Add($(Get-HTMLContentOpen -HeaderText "Users Charts")) - $FinalReport.Add($(Get-HTMLColumnOpen -ColumnNumber 1 -ColumnCount 3)) - $FinalReport.Add($(Get-HTMLPieChart -ChartObject $EnabledDisabledUsersPieObject -DataSet $EnabledDisabledUsersTable)) - $FinalReport.Add($(Get-HTMLColumnClose)) - $FinalReport.Add($(Get-HTMLColumnOpen -ColumnNumber 2 -ColumnCount 3)) - $FinalReport.Add($(Get-HTMLPieChart -ChartObject $PWExpiresUsersTable -DataSet $PasswordExpirationTable)) - $FinalReport.Add($(Get-HTMLColumnClose)) - $FinalReport.Add($(Get-HTMLColumnOpen -ColumnNumber 3 -ColumnCount 3)) - $FinalReport.Add($(Get-HTMLPieChart -ChartObject $PieObjectProtectedUsers -DataSet $ProtectedUsersTable)) - $FinalReport.Add($(Get-HTMLColumnClose)) - $FinalReport.Add($(Get-HTMLContentClose)) - $FinalReport.Add($(Get-HTMLTabContentClose)) - - #GPO Report - $FinalReport.Add($(Get-HTMLTabContentopen -TabName $tabarray[4] -TabHeading ("Report: " + (Get-Date -Format MM-dd-yyyy)))) - $FinalReport.Add($(Get-HTMLContentOpen -HeaderText "Group Policies")) - $FinalReport.Add($(Get-HTMLContentDataTable $GPOTable -HideFooter)) - $FinalReport.Add($(Get-HTMLContentClose)) - $FinalReport.Add($(Get-HTMLTabContentClose)) - - #Computers Report - $FinalReport.Add($(Get-HTMLTabContentopen -TabName $tabarray[5] -TabHeading ("Report: " + (Get-Date -Format MM-dd-yyyy)))) - - $FinalReport.Add($(Get-HTMLContentOpen -HeaderText "Computers Overivew")) - $FinalReport.Add($(Get-HTMLContentTable $TOPComputersTable -HideFooter)) - $FinalReport.Add($(Get-HTMLContentClose)) - - $FinalReport.Add($(Get-HTMLContentOpen -HeaderText "Computers")) - $FinalReport.Add($(Get-HTMLContentDataTable $ComputersTable -HideFooter)) - $FinalReport.Add($(Get-HTMLContentClose)) - - $FinalReport.Add($(Get-HTMLContentOpen -HeaderText "Computers Charts")) - $FinalReport.Add($(Get-HTMLColumnOpen -ColumnNumber 1 -ColumnCount 2)) - $FinalReport.Add($(Get-HTMLPieChart -ChartObject $PieObjectComputersProtected -DataSet $ComputerProtectedTable)) - $FinalReport.Add($(Get-HTMLColumnClose)) - $FinalReport.Add($(Get-HTMLColumnOpen -ColumnNumber 2 -ColumnCount 2)) - $FinalReport.Add($(Get-HTMLPieChart -ChartObject $PieObjectComputersEnabled -DataSet $ComputersEnabledTable)) - $FinalReport.Add($(Get-HTMLColumnClose)) - $FinalReport.Add($(Get-HTMLContentclose)) - - $FinalReport.Add($(Get-HTMLContentOpen -HeaderText "Computers Operating System Breakdown")) - $FinalReport.Add($(Get-HTMLPieChart -ChartObject $PieObjectComputerObjOS -DataSet $GraphComputerOS)) - $FinalReport.Add($(Get-HTMLContentclose)) - - $FinalReport.Add($(Get-HTMLTabContentClose)) - $FinalReport.Add($(Get-HTMLClosePage)) - - $Day = (Get-Date).Day - $Month = (Get-Date).Month - $Year = (Get-Date).Year - $ReportName = ("$Day - $Month - $Year - AD Report") - - Save-HTMLReport -ReportContent $FinalReport -ShowReport -ReportName $ReportName -ReportPath $ReportSavePath +$FinalReport.Add($(Get-HTMLContentClose)) + +$FinalReport.Add($(Get-HTMLContentOpen -HeaderText "Active Directory Users")) +$FinalReport.Add($(Get-HTMLContentDataTable $UserTable -HideFooter)) +$FinalReport.Add($(Get-HTMLContentClose)) + +$FinalReport.Add($(Get-HTMLContentOpen -HeaderText "Expiring Items")) +$FinalReport.Add($(Get-HTMLColumn1of2)) +$FinalReport.Add($(Get-HTMLContentOpen -BackgroundShade 1 -HeaderText "Users with Passwords Expiring in less than $DaysUntilPWExpireINT days")) +$FinalReport.Add($(Get-HTMLContentDataTable $PasswordExpireSoonTable -HideFooter)) +$FinalReport.Add($(Get-HTMLContentClose)) +$FinalReport.Add($(Get-HTMLColumnClose)) +$FinalReport.Add($(Get-HTMLColumn2of2)) +$FinalReport.Add($(Get-HTMLContentOpen -HeaderText 'Accounts Expiring Soon')) +$FinalReport.Add($(Get-HTMLContentDataTable $ExpiringAccountsTable -HideFooter)) +$FinalReport.Add($(Get-HTMLContentClose)) +$FinalReport.Add($(Get-HTMLColumnClose)) +$FinalReport.Add($(Get-HTMLContentClose)) + +$FinalReport.Add($(Get-HTMLContentOpen -HeaderText "Accounts")) +$FinalReport.Add($(Get-HTMLColumn1of2)) +$FinalReport.Add($(Get-HTMLContentOpen -BackgroundShade 1 -HeaderText "Users Haven't Logged on in $Days Days")) +$FinalReport.Add($(Get-HTMLContentDataTable $userphaventloggedonrecentlytable -HideFooter)) +$FinalReport.Add($(Get-HTMLContentClose)) +$FinalReport.Add($(Get-HTMLColumnClose)) +$FinalReport.Add($(Get-HTMLColumn2of2)) + +$FinalReport.Add($(Get-HTMLContentOpen -BackgroundShade 1 -HeaderText "Accounts Created in $UserCreatedDays Days or Less")) +$FinalReport.Add($(Get-HTMLContentDataTable $NewCreatedUsersTable -HideFooter)) +$FinalReport.Add($(Get-HTMLContentClose)) +$FinalReport.Add($(Get-HTMLColumnClose)) +$FinalReport.Add($(Get-HTMLContentClose)) + +$FinalReport.Add($(Get-HTMLContentOpen -HeaderText "Users Charts")) +$FinalReport.Add($(Get-HTMLColumnOpen -ColumnNumber 1 -ColumnCount 3)) +$FinalReport.Add($(Get-HTMLPieChart -ChartObject $EnabledDisabledUsersPieObject -DataSet $EnabledDisabledUsersTable)) +$FinalReport.Add($(Get-HTMLColumnClose)) +$FinalReport.Add($(Get-HTMLColumnOpen -ColumnNumber 2 -ColumnCount 3)) +$FinalReport.Add($(Get-HTMLPieChart -ChartObject $PWExpiresUsersTable -DataSet $PasswordExpirationTable)) +$FinalReport.Add($(Get-HTMLColumnClose)) +$FinalReport.Add($(Get-HTMLColumnOpen -ColumnNumber 3 -ColumnCount 3)) +$FinalReport.Add($(Get-HTMLPieChart -ChartObject $PieObjectProtectedUsers -DataSet $ProtectedUsersTable)) +$FinalReport.Add($(Get-HTMLColumnClose)) +$FinalReport.Add($(Get-HTMLContentClose)) +$FinalReport.Add($(Get-HTMLTabContentClose)) + +#GPO Report +$FinalReport.Add($(Get-HTMLTabContentopen -TabName $tabarray[4] -TabHeading ("Report: " + (Get-Date -Format MM-dd-yyyy)))) +$FinalReport.Add($(Get-HTMLContentOpen -HeaderText "Group Policies")) +$FinalReport.Add($(Get-HTMLContentDataTable $GPOTable -HideFooter)) +$FinalReport.Add($(Get-HTMLContentClose)) +$FinalReport.Add($(Get-HTMLTabContentClose)) + +#Computers Report +$FinalReport.Add($(Get-HTMLTabContentopen -TabName $tabarray[5] -TabHeading ("Report: " + (Get-Date -Format MM-dd-yyyy)))) + +$FinalReport.Add($(Get-HTMLContentOpen -HeaderText "Computers Overivew")) +$FinalReport.Add($(Get-HTMLContentTable $TOPComputersTable -HideFooter)) +$FinalReport.Add($(Get-HTMLContentClose)) + +$FinalReport.Add($(Get-HTMLContentOpen -HeaderText "Computers")) +$FinalReport.Add($(Get-HTMLContentDataTable $ComputersTable -HideFooter)) +$FinalReport.Add($(Get-HTMLContentClose)) + +$FinalReport.Add($(Get-HTMLContentOpen -HeaderText "Computers Charts")) +$FinalReport.Add($(Get-HTMLColumnOpen -ColumnNumber 1 -ColumnCount 2)) +$FinalReport.Add($(Get-HTMLPieChart -ChartObject $PieObjectComputersProtected -DataSet $ComputerProtectedTable)) +$FinalReport.Add($(Get-HTMLColumnClose)) +$FinalReport.Add($(Get-HTMLColumnOpen -ColumnNumber 2 -ColumnCount 2)) +$FinalReport.Add($(Get-HTMLPieChart -ChartObject $PieObjectComputersEnabled -DataSet $ComputersEnabledTable)) +$FinalReport.Add($(Get-HTMLColumnClose)) +$FinalReport.Add($(Get-HTMLContentclose)) + +$FinalReport.Add($(Get-HTMLContentOpen -HeaderText "Computers Operating System Breakdown")) +$FinalReport.Add($(Get-HTMLPieChart -ChartObject $PieObjectComputerObjOS -DataSet $GraphComputerOS)) +$FinalReport.Add($(Get-HTMLContentclose)) + +$FinalReport.Add($(Get-HTMLTabContentClose)) +$FinalReport.Add($(Get-HTMLClosePage)) + +$Day = (Get-Date).Day +$Month = (Get-Date).Month +$Year = (Get-Date).Year +$ReportName = ("$Day - $Month - $Year - AD Report") + +Save-HTMLReport -ReportContent $FinalReport -ShowReport -ReportName $ReportName -ReportPath $ReportSavePath From e1b598ebf9f8782b61f0dacc2013160454417353 Mon Sep 17 00:00:00 2001 From: Brad Wyatt Date: Fri, 7 Dec 2018 08:12:23 -0600 Subject: [PATCH 2/3] Update PSHTML-AD.ps1 --- PSHTML-AD.ps1 | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/PSHTML-AD.ps1 b/PSHTML-AD.ps1 index 108812a..9a7fb9c 100644 --- a/PSHTML-AD.ps1 +++ b/PSHTML-AD.ps1 @@ -308,7 +308,7 @@ $CompanyInfoTable.Add($obj) if (($CompanyInfoTable).Count -eq 0) { - $Obj= [PSCustomObject]@{ + $Obj = [PSCustomObject]@{ Information = 'Information: Could not get items for table' } @@ -1057,6 +1057,16 @@ foreach ($User in $AllUsers) $PasswordExpireSoonTable.Add($obj) } } +if (($PasswordExpireSoonTable).Count -eq 0) +{ + + $Obj = [PSCustomObject]@{ + + Information = 'Information: No users were found to have passwords expiring soon' + } + $PasswordExpireSoonTable.Add($obj) +} + if (($usertable).Count -eq 0) { From 56a362426a470ca8b2d86cec9caca0a695c6fe0e Mon Sep 17 00:00:00 2001 From: Brad Wyatt Date: Sat, 8 Dec 2018 14:43:56 -0600 Subject: [PATCH 3/3] Multiple Fixes and performance benefits - FIXED: list.add object errors - Incorrect listing on user top table for null objects - Grabbing AD objects multiple times to store in multiple vars - FIXED: Empty table errors when saving report --- PSHTML-AD.ps1 | 52 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 21 deletions(-) diff --git a/PSHTML-AD.ps1 b/PSHTML-AD.ps1 index 9a7fb9c..79c9fa8 100644 --- a/PSHTML-AD.ps1 +++ b/PSHTML-AD.ps1 @@ -21,9 +21,9 @@ -ReportTitle "Active Directory Report" .PARAMETER Days - Users that have not logged in within [X] amount of days. + Users that have not logged in [X] amount of days or more. - -Days "1" + -Days "30" .PARAMETER UserCreatedDays Users that have been created within [X] amount of days. @@ -41,6 +41,7 @@ -ADModNumber "3" .NOTES + Version: 1.0.3 Author: Bradley Wyatt Date: 12/4/2018 Modified: JBear 12/5/2018 @@ -66,8 +67,8 @@ param ( [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 in within [X] amount of days; Default: 1")] - $Days = 1, + [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")] @@ -908,6 +909,9 @@ $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) { @@ -952,8 +956,7 @@ foreach ($User in $AllUsers) $daystoexpire = "N/A" } - #Get users that haven't logged on in X amount of days, var is set at start of script - if (($User.Enabled -eq $True) -and ($User.LastLogonDate -lt (Get-Date).AddDays(- $Days)) -and ($User.LastLogonDate -ne $NULL)) + if (($User.Enabled -eq $True) -and ($AttVar.LastLogon -lt ((Get-Date).AddDays(- $Days))) -and ($User.LastLogon -ne $NULL)) { $obj = [PSCustomObject]@{ @@ -970,15 +973,6 @@ foreach ($User in $AllUsers) $userphaventloggedonrecentlytable.Add($obj) } - if (($userphaventloggedonrecentlytable).Count -eq 0) - { - - $userphaventloggedonrecentlytable = [PSCustomObject]@{ - - Information = "Information: No Users were found to have not logged on in $Days days" - } - } - #Items for protected vs non protected users if ($User.ProtectedFromAccidentalDeletion -eq $False) { @@ -1057,6 +1051,13 @@ foreach ($User in $AllUsers) $PasswordExpireSoonTable.Add($obj) } } +if (($userphaventloggedonrecentlytable).Count -eq 0) +{ + $userphaventloggedonrecentlytable = [PSCustomObject]@{ + + Information = "Information: No Users were found to have not logged on in $Days days or more" + } +} if (($PasswordExpireSoonTable).Count -eq 0) { @@ -1128,16 +1129,25 @@ $objULic = [PSCustomObject]@{ } $ProtectedUsersTable.Add($objULic) - +if ($null -ne (($userphaventloggedonrecentlytable).Information)) +{ + $UHLONXD = "0" + +} +Else +{ + $UHLONXD = $userphaventloggedonrecentlytable.Count + +} #TOP User table -If (($ExpiringAccountsTable).Count -gt 0) +If ($null -eq (($ExpiringAccountsTable).Information)) { $objULic = [PSCustomObject]@{ 'Total Users' = $AllUsers.Count "Users with Passwords Expiring in less than $DaysUntilPWExpireINT days" = $PasswordExpireSoonTable.Count 'Expiring Accounts' = $ExpiringAccountsTable.Count - "Users Haven't Logged on in $Days Days" = $userphaventloggedonrecentlytable.Count + "Users Haven't Logged on in $Days Days or more" = $UHLONXD } $TOPUserTable.Add($objULic) @@ -1151,7 +1161,7 @@ Else 'Total Users' = $AllUsers.Count "Users with Passwords Expiring in less than $DaysUntilPWExpireINT days" = $PasswordExpireSoonTable.Count 'Expiring Accounts' = "0" - "Users Haven't Logged on in $Days Days" = $userphaventloggedonrecentlytable.Count + "Users Haven't Logged on in $Days Days or more" = $UHLONXD } $TOPUserTable.Add($objULic) } @@ -1718,7 +1728,7 @@ $FinalReport.Add($(Get-HTMLContentClose)) $FinalReport.Add($(Get-HTMLContentOpen -HeaderText "Accounts")) $FinalReport.Add($(Get-HTMLColumn1of2)) -$FinalReport.Add($(Get-HTMLContentOpen -BackgroundShade 1 -HeaderText "Users Haven't Logged on in $Days Days")) +$FinalReport.Add($(Get-HTMLContentOpen -BackgroundShade 1 -HeaderText "Users Haven't Logged on in $Days Days or more")) $FinalReport.Add($(Get-HTMLContentDataTable $userphaventloggedonrecentlytable -HideFooter)) $FinalReport.Add($(Get-HTMLContentClose)) $FinalReport.Add($(Get-HTMLColumnClose)) @@ -1818,7 +1828,7 @@ $FinalReport.Add($(Get-HTMLContentClose)) $FinalReport.Add($(Get-HTMLContentOpen -HeaderText "Accounts")) $FinalReport.Add($(Get-HTMLColumn1of2)) -$FinalReport.Add($(Get-HTMLContentOpen -BackgroundShade 1 -HeaderText "Users Haven't Logged on in $Days Days")) +$FinalReport.Add($(Get-HTMLContentOpen -BackgroundShade 1 -HeaderText "Users Haven't Logged on in $Days Days or more")) $FinalReport.Add($(Get-HTMLContentDataTable $userphaventloggedonrecentlytable -HideFooter)) $FinalReport.Add($(Get-HTMLContentClose)) $FinalReport.Add($(Get-HTMLColumnClose))