functions/public/Invoke-365AutomatedCheck.ps1

<#
.SYNOPSIS
    Invokes the 365AutomatedCheck function to perform automated checks on Microsoft 365 environments.

.DESCRIPTION
    The Invoke-365AutomatedCheck function performs automated checks on Microsoft 365 environments by running Pester tests. It generates an HTML report from the test results, and optionally, an Excel report for detailed analysis.

.PARAMETER PesterConfiguration
    Specifies the Pester configuration hashtable to be used for running the tests.

.PARAMETER Verbosity
    Specifies the verbosity level for the Pester output. Valid values are 'None', 'Normal', 'Detailed', and 'Diagnostic'. The default value is 'None'.

.PARAMETER XsltPath
    Specifies the path to the XSLT file used for transforming the XML report to HTML. The default path is "$RootPath/functions/private/DefaultReportConfig.xslt".

.PARAMETER XmlPath
    Specifies the path to the XML report file generated by the Pester tests. If not provided, the default path is '365ACReport.xml'.

.PARAMETER Path
    Specifies the path to the Pester test files. The default path is "$RootPath/tests/".

.PARAMETER OutputHtmlPath
    Specifies the path to save the generated HTML report. If not provided, the default path is '365ACReport.html'.

.PARAMETER PassThru
    Indicates whether to pass the Pester test results through the pipeline. By default, it is set to $false.

.PARAMETER Tag
    Specifies the tags to include when running the Pester tests. Multiple tags can be specified using an array.

.PARAMETER ExcludeTag
    Specifies the tags to exclude when running the Pester tests. Multiple tags can be specified using an array.

.PARAMETER ExcelFilePath
    Specifies the path to the Excel file used for additional validation. This parameter is optional.

.PARAMETER NoExcel
    Specifies whether to skip importing the Excel validation workbook. If set to $true, no Excel file imported. The default value is $false.

.EXAMPLE
    Invoke-365AutomatedCheck -PesterConfiguration $config -Verbosity 'Detailed' -XmlPath 'C:\Reports\365ACReport.xml' -OutputHtmlPath 'C:\Reports\365ACReport.html' -Tag 'Basic', 'HR'
    Runs the 365AutomatedCheck function with a detailed verbosity level, specifying the XML report path and the output HTML path. It includes tests with the 'Basic' and 'HR' tags.

.NOTES
    This function requires the Pester and ImportExcel modules to be installed. If the ImportExcel module is not installed and the Excel report is requested, the function will throw an error.

.LINK
    https://github.com/DevClate/365AutomatedCheck
#>

function Invoke-365AutomatedCheck {
    [CmdletBinding()]
    param (
        [hashtable] $PesterConfiguration,
        
        [ValidateSet('None', 'Normal', 'Detailed', 'Diagnostic')]
        [string] $Verbosity = 'None',
        
        [string] $XsltPath = "$RootPath/functions/private/DefaultReportConfig.xslt",
        
        [string] $XmlPath,
        
        [string] $Path = (Join-Path -Path $RootPath -ChildPath "tests"),
        
        [ValidatePattern(".*\.html$")]
        [string] $OutputHtmlPath,
        
        [bool] $PassThru = $false,
        
        [string[]] $Tag,
        
        [string[]] $ExcludeTag,
        
        [string] $ExcelFilePath,

        [bool] $NoExcel = $false
    )

    #Requires -Module Pester, ImportExcel

    if (-not $XmlPath -and $OutputHtmlPath) {
        # Change the extension of $OutputHtmlPath to .xml and assign it to $XmlPath
        $XmlPath = [System.IO.Path]::ChangeExtension($OutputHtmlPath, '.xml')
    }

    $XmlPath = Set-365ACDefaultOutputPath -Path $XmlPath -DefaultPath '365ACReport.xml'
    Write-PSFMessage -Level Host -Message "Using XML Path: $XmlPath"

    $OutputHtmlPath = Set-365ACDefaultOutputPath -Path $OutputHtmlPath -DefaultPath '365ACReport.html'
    Write-PSFMessage -Level Host -Message "Using HTML Output Path: $OutputHtmlPath"

    $pesterConfig = Get-365ACPesterConfiguration -Path $Path -Tag $Tag -ExcludeTag $ExcludeTag -XmlPath $XmlPath -PesterConfiguration $PesterConfiguration -Verbosity $Verbosity -PassThru $PassThru -NoExcel $NoExcel

    # Conditionally set the ExcelFilePath environment variable
    if (-not $NoExcel) {
        $env:ExcelFilePath = $ExcelFilePath
        $env:NoExcel = $false
    }
    else {
        $env:NoExcel = $true
    }

    Invoke-Pester -Configuration $pesterConfig

    Start-Sleep -Seconds 2

    Convert-365ACXmlToHtml -XmlPath $XmlPath -XsltPath $XsltPath -OutputHtmlPath $OutputHtmlPath
}