maester365 / maester

The core repository for the Maester module with helper cmdlets that will be called from the Pester tests.
https://maester.dev
MIT License
366 stars 89 forks source link

Add-MtTestResultDetail not adding details to custom tests #528

Open ntatschner opened 3 weeks ago

ntatschner commented 3 weeks ago

Hey all,

I'm trying to use Add-MtTestResultDetail to add details from a custom test, but unfortunately it's not working as documented.

I have 3 files in the "custom" folder, all with the same name, a .ps1, .Tests.ps1 and a .md

The test runs as expected and produces the overall status of the test. I based the logic of the test on your existing tests and used the same logic to add the test details.

These are the files: .ps1

function Find-RSYUsersMissingManagers {
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseSingularNouns', '', Justification = 'Exists is not a plurality')]
    [CmdletBinding()]
    [OutputType([bool])]

    # Retrieve all users from Microsoft Graph
    $users = Get-MgUser -All

    # Initialize an array to track users without a manager
    $usersWithoutManager = @()

    $result = $true

    # Loop through each user and ensure they have a manager assigned
    foreach ($user in $users) {
        # Fetch the manager for the current user
        $manager = Get-MgUserManager -UserId $user.Id -ErrorAction SilentlyContinue

        if ([string]::IsNullOrEmpty($manager)) {
            $result = $false
            $usersWithoutManager += [PSCustomObject]@{
                DisplayName       = $user.DisplayName
                UserPrincipalName = $user.UserPrincipalName
            }
        }
    }

    if ($result -eq $false) {
        $TestResults = "There were no users with out Managers assigned. Well Done! :raised_hands:"
    }
    else {
        $TestResults += "Could find managers on:`n`n"
        foreach ($u in $usersWithoutManager) {
            $TestResults += "- User: $($u.DisplayName) | UPN $($u.UserPrincipalName). :cry:`n"
        }
        Add-MtTestResultDetail -TestName "Manager Attribute: All users should have a manager attribute set" -Result $TestResults
    }
    return $result
}

.Tests.ps1

BeforeAll {
    . $PSScriptRoot/Find-RSYUsersMissingManagers.ps1
}
Describe "Rothesay" -Tag "Entra", "CustomTests", "AttributeTest" {
    It "Manager Attribute: All users should have a manager attribute set" {
        $result = Find-RSYUsersMissingManagers
        $result | Should -Be $true -Because "All users should have an assigned Manager"
    }
}

.md

### This test checks if there are any users without a manager assigned. 
##### Custom Rothesay Test
---

<!--- Results --->

%TestResult%

Any help would be appreciated and thank you for creating an amazing utility.

ntatschner commented 1 week ago

Hey Maester community,

Has anyone else had this issue with custom tests? Anyone been able to add details to custom tests?