diff --git a/source/tests/Test-GlobalAdminsCount.ps1 b/source/tests/Test-GlobalAdminsCount.ps1 index f79b04e..e1fe62e 100644 --- a/source/tests/Test-GlobalAdminsCount.ps1 +++ b/source/tests/Test-GlobalAdminsCount.ps1 @@ -1,46 +1,59 @@ function Test-GlobalAdminsCount { [CmdletBinding()] param ( - # Define your parameters here + # Aligned + # Define your parameters here if needed ) begin { - # Dot source the class script + # Dot source the class script if necessary + . .\source\Classes\CISAuditResult.ps1 - $auditResults = @() + # Initialization code, if needed } process { # 1.1.3 (L1) Ensure that between two and four global admins are designated - # Pass if the count of global admins is between 2 and 4. Fail otherwise. + # Retrieve global admin role and members $globalAdminRole = Get-MgDirectoryRole -Filter "RoleTemplateId eq '62e90394-69f5-4237-9190-012177145e10'" $globalAdmins = Get-MgDirectoryRoleMember -DirectoryRoleId $globalAdminRole.Id $globalAdminCount = $globalAdmins.AdditionalProperties.Count $globalAdminUsernames = ($globalAdmins | ForEach-Object { $_.AdditionalProperties["displayName"] }) -join ', ' - # Create an instance of CISAuditResult and populate it + # Prepare failure reasons and details based on compliance + $failureReasons = if ($globalAdminCount -lt 2) { + "Less than 2 global admins: $globalAdminUsernames" + } + elseif ($globalAdminCount -gt 4) { + "More than 4 global admins: $globalAdminUsernames" + } + else { + "N/A" + } + + $details = "Count: $globalAdminCount; Users: $globalAdminUsernames" + + # Create and populate the CISAuditResult object $auditResult = [CISAuditResult]::new() $auditResult.CISControlVer = "v8" $auditResult.CISControl = "5.1" $auditResult.CISDescription = "Establish and Maintain an Inventory of Accounts" $auditResult.Rec = "1.1.3" - $auditResult.ELevel = "E3" # Based on your environment (E3, E5, etc.) + $auditResult.ELevel = "E3" $auditResult.ProfileLevel = "L1" - $auditResult.IG1 = $true # Set based on the benchmark - $auditResult.IG2 = $true # Set based on the benchmark - $auditResult.IG3 = $true # Set based on the benchmark + $auditResult.IG1 = $true + $auditResult.IG2 = $true + $auditResult.IG3 = $true $auditResult.RecDescription = "Ensure that between two and four global admins are designated" $auditResult.Result = $globalAdminCount -ge 2 -and $globalAdminCount -le 4 - $auditResult.Details = "Count: $globalAdminCount; Users: $globalAdminUsernames" - $auditResult.FailureReason = if ($globalAdminCount -lt 2) { "Less than 2 global admins: $globalAdminUsernames" } elseif ($globalAdminCount -gt 4) { "More than 4 global admins: $globalAdminUsernames" } else { "N/A" } + $auditResult.Details = $details + $auditResult.FailureReason = $failureReasons $auditResult.Status = if ($globalAdminCount -ge 2 -and $globalAdminCount -le 4) { "Pass" } else { "Fail" } - - $auditResults += $auditResult } end { - # Return auditResults - return $auditResults + # Return the audit result + return $auditResult } }