diff --git a/source/Private/Get-ExceededLengthResultDetail.ps1 b/source/Private/Get-ExceededLengthResultDetail.ps1 new file mode 100644 index 0000000..bd2b956 --- /dev/null +++ b/source/Private/Get-ExceededLengthResultDetail.ps1 @@ -0,0 +1,48 @@ +function Get-ExceededLengthResultDetail { + [CmdletBinding()] + param ( + [Parameter(Mandatory = $true, ParameterSetName = 'UpdateArray')] + [Parameter(Mandatory = $true, ParameterSetName = 'ReturnExceedingTests')] + [object[]]$AuditResults, + + [Parameter(Mandatory = $true, ParameterSetName = 'UpdateArray')] + [Parameter(Mandatory = $true, ParameterSetName = 'ReturnExceedingTests')] + [string[]]$TestNumbersToCheck, + + [Parameter(Mandatory = $true, ParameterSetName = 'UpdateArray')] + [string[]]$ExportedTests, + + [Parameter(Mandatory = $true, ParameterSetName = 'ReturnExceedingTests')] + [switch]$ReturnExceedingTestsOnly, + + [int]$DetailsLengthLimit = 30000 + ) + + $exceedingTests = @() + $updatedResults = @() + + for ($i = 0; $i -lt $AuditResults.Count; $i++) { + $auditResult = $AuditResults[$i] + if ($auditResult.Rec -in $TestNumbersToCheck) { + if ($auditResult.Details.Length -gt $DetailsLengthLimit) { + if ($ReturnExceedingTestsOnly) { + $exceedingTests += $auditResult.Rec + } else { + if ($ExportedTests -contains $auditResult.Rec) { + Write-Information "The test result for $($auditResult.Rec) is too large for CSV and was included in the export. Check the exported files." + $auditResult.Details = "The test result is too large to be exported to CSV. Use the audit result and the export function for full output." + } else { + $auditResult.Details = "The test result is too large to be exported to CSV. Use the audit result and the export function for full output." + } + } + } + } + $updatedResults += $auditResult + } + + if ($ReturnExceedingTestsOnly) { + return $exceedingTests + } else { + return $updatedResults + } +} \ No newline at end of file diff --git a/source/Private/Initialize-LargeTestTable.ps1 b/source/Private/Initialize-LargeTestTable.ps1 index f5e2d52..e3d9d1d 100644 --- a/source/Private/Initialize-LargeTestTable.ps1 +++ b/source/Private/Initialize-LargeTestTable.ps1 @@ -23,7 +23,7 @@ function Initialize-LargeTestTable { ) process { $header = "UserPrincipalName|AuditEnabled|AdminActionsMissing|DelegateActionsMissing|OwnerActionsMissing" - $lineTemplate = "user{0}@contosonorthwind.net|True|FB,CP,MV|FB,MV|ML,MV,CR" + $lineTemplate = "user{0}@contosonorthwind.net|True|FB,CP,MV,MTDI|FB,MV|ML,MV,CR" # Generate the header and lines $lines = @($header) for ($i = 1; $i -le $lineCount; $i++) { diff --git a/source/Public/Export-M365SecurityAuditTable.ps1 b/source/Public/Export-M365SecurityAuditTable.ps1 index 6fa8fcb..4790139 100644 --- a/source/Public/Export-M365SecurityAuditTable.ps1 +++ b/source/Public/Export-M365SecurityAuditTable.ps1 @@ -166,28 +166,12 @@ function Export-M365SecurityAuditTable { if ($ExportOriginalTests) { # Define the test numbers to check - $testNumbersToCheck = "1.1.1", "1.3.1", "6.1.2", "6.1.3", "7.3.4" - # Iterate through the AuditResults and check the Details length for the specified test numbers - for ($i = 0; $i -lt $AuditResults.Count; $i++) { - $auditResult = $AuditResults[$i] - if ($auditResult.Rec -in $testNumbersToCheck) { - if ($auditResult.Details.Length -gt 30000) { - if ($exportedTests -contains $auditResult.Rec) { - Write-Information "The test result for $($auditResult.Rec) is too large for CSV and was included in the export. Check the exported files." - $auditResult.Details = "The test result is too large to be exported to CSV. Use the audit result and the export function for full output." - } - else { - Write-Information "The test result for $($auditResult.Rec) is too large for CSV." - $auditResult.Details = "The test result is too large to be exported to CSV. Use the audit result and the export function for full output." - } - # Update the AuditResults array with the modified auditResult - $AuditResults[$i] = $auditResult - } - } - } - # Export the modified audit results to a CSV file + $TestNumbersToCheck = "1.1.1", "1.3.1", "6.1.2", "6.1.3", "7.3.4" + + # Check for large details and update the AuditResults array + $updatedAuditResults = Get-ExceededLengthResultDetail -AuditResults $AuditResults -TestNumbersToCheck $TestNumbersToCheck -ExportedTests $exportedTests -DetailsLengthLimit 30000 $originalFileName = "$ExportPath\$timestamp`_M365FoundationsAudit.csv" - $AuditResults | Export-Csv -Path $originalFileName -NoTypeInformation + $updatedAuditResults | Export-Csv -Path $originalFileName -NoTypeInformation } } elseif ($OutputTestNumber) { diff --git a/source/Public/Invoke-M365SecurityAudit.ps1 b/source/Public/Invoke-M365SecurityAudit.ps1 index 5e94add..54671fb 100644 --- a/source/Public/Invoke-M365SecurityAudit.ps1 +++ b/source/Public/Invoke-M365SecurityAudit.ps1 @@ -286,6 +286,16 @@ function Invoke-M365SecurityAudit { # Call the private function to calculate and display results Measure-AuditResult -AllAuditResults $allAuditResults -FailedTests $script:FailedTests # Return all collected audit results + # Define the test numbers to check + $TestNumbersToCheck = "1.1.1", "1.3.1", "6.1.2", "6.1.3", "7.3.4" + + # Check for large details in the audit results + $exceedingTests = Get-ExceededLengthResultDetail -AuditResults $allAuditResults -TestNumbersToCheck $TestNumbersToCheck -ReturnExceedingTestsOnly -DetailsLengthLimit 30000 + if ($exceedingTests.Count -gt 0) { + Write-Information "The following tests exceeded the details length limit: $($exceedingTests -join ', ')" -InformationAction Continue + Write-Host "(Assuming the results were instantiated. Ex: `$object = invoke-M365SecurityAudit) Use the following command and adjust as neccesary to view the full details of the test results:" -ForegroundColor Gray + Write-Host "Export-M365SecurityAuditTable -ExportAllTests -AuditResults `$object -ExportPath `"C:\temp`" -ExportOriginalTests" -ForegroundColor Green + } return $allAuditResults.ToArray() | Sort-Object -Property Rec } } diff --git a/source/tests/Test-MailboxAuditingE5.ps1 b/source/tests/Test-MailboxAuditingE5.ps1 index adca387..fbd0f07 100644 --- a/source/tests/Test-MailboxAuditingE5.ps1 +++ b/source/tests/Test-MailboxAuditingE5.ps1 @@ -103,7 +103,7 @@ function Test-MailboxAuditingE5 { else { "UserPrincipalName|AuditEnabled|AdminActionsMissing|DelegateActionsMissing|OwnerActionsMissing`n" + ($allFailures -join "`n") # Condition A for fail } - # $details = Initialize-LargeTestTable -lineCount 3000 # Adjust the lineCount to exceed 32,000 characters + $details = Initialize-LargeTestTable -lineCount 3000 # Adjust the lineCount to exceed 32,000 characters # Populate the audit result $params = @{ Rec = $recnum diff --git a/tests/Unit/Private/Get-ExceededLengthResultDetail.tests.ps1 b/tests/Unit/Private/Get-ExceededLengthResultDetail.tests.ps1 new file mode 100644 index 0000000..4a2aa69 --- /dev/null +++ b/tests/Unit/Private/Get-ExceededLengthResultDetail.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' + } + } + } +} +