From f905f269d1d3f3dd2308780809386d28b7f3b0a3 Mon Sep 17 00:00:00 2001 From: DrIOS <58635327+DrIOSX@users.noreply.github.com> Date: Fri, 7 Jun 2024 21:10:28 -0500 Subject: [PATCH] fix: output for 6.1.2/3 and 7.3.4 --- source/tests/Test-MailboxAuditingE3.ps1 | 8 +++++ source/tests/Test-MailboxAuditingE5.ps1 | 30 +++++++++++-------- source/tests/Test-RestrictCustomScripts.ps1 | 29 +++++++++++------- .../Unit/Private/Get-MostCommonWord.tests.ps1 | 27 +++++++++++++++++ 4 files changed, 70 insertions(+), 24 deletions(-) create mode 100644 tests/Unit/Private/Get-MostCommonWord.tests.ps1 diff --git a/source/tests/Test-MailboxAuditingE3.ps1 b/source/tests/Test-MailboxAuditingE3.ps1 index c103d23..ad6364b 100644 --- a/source/tests/Test-MailboxAuditingE3.ps1 +++ b/source/tests/Test-MailboxAuditingE3.ps1 @@ -95,6 +95,14 @@ function Test-MailboxAuditingE3 { } end { + #$verbosePreference = 'Continue' + $detailsLength = $details.Length + Write-Verbose "Character count of the details: $detailsLength" + + if ($detailsLength -gt 32767) { + Write-Verbose "Warning: The character count exceeds the limit for Excel cells." + } + #$verbosePreference = 'SilentlyContinue' return $auditResult } } diff --git a/source/tests/Test-MailboxAuditingE5.ps1 b/source/tests/Test-MailboxAuditingE5.ps1 index 978ffa2..539b628 100644 --- a/source/tests/Test-MailboxAuditingE5.ps1 +++ b/source/tests/Test-MailboxAuditingE5.ps1 @@ -23,6 +23,7 @@ function Test-MailboxAuditingE5 { try { foreach ($user in $allUsers) { if ($processedUsers.ContainsKey($user.UserPrincipalName)) { + Write-Verbose "Skipping already processed user: $($user.UserPrincipalName)" continue } @@ -45,32 +46,27 @@ function Test-MailboxAuditingE5 { foreach ($action in $OwnerActions) { if ($mailbox.AuditOwner -notcontains $action) { $missingActions += "Owner action '$action' missing" } } + + if ($missingActions.Count -gt 0) { + $formattedActions = Format-MissingActions -missingActions $missingActions + $allFailures += "$userUPN|True|$($formattedActions.Admin)|$($formattedActions.Delegate)|$($formattedActions.Owner)" + } } else { $allFailures += "$userUPN|False|||" - continue } - if ($missingActions) { - $formattedActions = Format-MissingActions -missingActions $missingActions - $allFailures += "$userUPN|True|$($formattedActions.Admin)|$($formattedActions.Delegate)|$($formattedActions.Owner)" - } - else { - Write-Verbose "User $($user.UserPrincipalName) passed the mailbox audit checks." - } + # Mark the user as processed $processedUsers[$user.UserPrincipalName] = $true } - else { - # Adding verbose output to indicate the user does not have an E5 license - Write-Verbose "User $($user.UserPrincipalName) does not have an Office E5 license." - } } # Prepare failure reasons and details based on compliance $failureReasons = if ($allFailures.Count -eq 0) { "N/A" } else { "Audit issues detected." } $details = if ($allFailures.Count -eq 0) { "All Office E5 users have correct mailbox audit settings." - } else { + } + else { "UserPrincipalName|AuditEnabled|AdminActionsMissing|DelegateActionsMissing|OwnerActionsMissing`n" + ($allFailures -join "`n") } @@ -99,6 +95,14 @@ function Test-MailboxAuditingE5 { } end { + #$verbosePreference = 'Continue' + $detailsLength = $details.Length + Write-Verbose "Character count of the details: $detailsLength" + + if ($detailsLength -gt 32767) { + Write-Verbose "Warning: The character count exceeds the limit for Excel cells." + } + #$verbosePreference = 'SilentlyContinue' return $auditResult } } diff --git a/source/tests/Test-RestrictCustomScripts.ps1 b/source/tests/Test-RestrictCustomScripts.ps1 index 5dbd411..9eb39e7 100644 --- a/source/tests/Test-RestrictCustomScripts.ps1 +++ b/source/tests/Test-RestrictCustomScripts.ps1 @@ -28,20 +28,27 @@ function Test-RestrictCustomScripts { # Find sites where custom scripts are allowed $customScriptAllowedSites = $processedUrls | Where-Object { $_.DenyAddAndCustomizePages -ne 'Enabled' } + $verbosePreference = 'Continue' + # Check the total length of URLs + $totalUrlLength = ($customScriptAllowedSites.Url -join '').Length + Write-Verbose "Total length of URLs: $totalUrlLength" - # Extract hostnames from allowed sites - Write-Verbose "Extracting hostnames from URLs..." - $hostnames = $customScriptAllowedSites.Url | ForEach-Object { - if ($_ -match '^https://([^\.]+)\.') { - $matches[1] + # Extract hostnames from allowed sites if the total length exceeds the limit + $mostUsedHostname = $null + if ($totalUrlLength -gt 20000) { + Write-Verbose "Extracting hostnames from URLs..." + $hostnames = $customScriptAllowedSites.Url | ForEach-Object { + if ($_ -match '^https://([^\.]+)\.') { + $matches[1] + } } + Write-Verbose "Extracted hostnames: $($hostnames -join ', ')" + + # Find the most used hostname using the Get-MostCommonWord function + $mostUsedHostname = Get-MostCommonWord -InputStrings $hostnames + Write-Verbose "Most used hostname: $mostUsedHostname" } - Write-Verbose "Extracted hostnames: $($hostnames -join ', ')" - - # Find the most used hostname using the Get-MostCommonWord function - $mostUsedHostname = Get-MostCommonWord -InputStrings $hostnames - Write-Verbose "Most used hostname: $mostUsedHostname" - + $verbosePreference = 'SilentlyContinue' # Compliance is true if no sites allow custom scripts $complianceResult = $customScriptAllowedSites.Count -eq 0 diff --git a/tests/Unit/Private/Get-MostCommonWord.tests.ps1 b/tests/Unit/Private/Get-MostCommonWord.tests.ps1 new file mode 100644 index 0000000..4a2aa69 --- /dev/null +++ b/tests/Unit/Private/Get-MostCommonWord.tests.ps1 @@ -0,0 +1,27 @@ +$ProjectPath = "$PSScriptRoot\..\..\.." | Convert-Path +$ProjectName = ((Get-ChildItem -Path $ProjectPath\*\*.psd1).Where{ + ($_.Directory.Name -match 'source|src' -or $_.Directory.Name -eq $_.BaseName) -and + $(try { Test-ModuleManifest $_.FullName -ErrorAction Stop } catch { $false } ) + }).BaseName + + +Import-Module $ProjectName + +InModuleScope $ProjectName { + Describe Get-PrivateFunction { + Context 'Default' { + BeforeEach { + $return = Get-PrivateFunction -PrivateData 'string' + } + + It 'Returns a single object' { + ($return | Measure-Object).Count | Should -Be 1 + } + + It 'Returns a string based on the parameter PrivateData' { + $return | Should -Be 'string' + } + } + } +} +