Public/Get-SCOMManagementPackXml.ps1

function Get-SCOMManagementPackXml {
    <#
    .SYNOPSIS
        Extracts the XML content from a SCOM Management Pack object.
 
    .DESCRIPTION
        This function takes a SCOM Management Pack object and exports its XML content as a string.
        It uses the Export-SCOMManagementPack cmdlet to export the management pack to a temporary
        file and then reads the XML content from that file. The temporary file is automatically
        cleaned up after the operation.
 
    .PARAMETER ManagementPack
        The SCOM Management Pack object from which to extract the XML content. This parameter is
        mandatory and accepts pipeline input.
 
    .INPUTS
        Microsoft.EnterpriseManagement.Configuration.ManagementPack
        You can pipe Management Pack objects to this function.
 
    .OUTPUTS
        System.String
        Returns the XML content of the Management Pack as a string.
 
    .EXAMPLE
        # Get a specific management pack and extract its XML
        $mp = Get-SCOMManagementPack -Name "Microsoft.SystemCenter.Apm.Infrastructure"
        $xmlContent = Get-SCOMManagementPackXml -ManagementPack $mp
        Write-Host $xmlContent
 
    .EXAMPLE
        # Search for a specific string across all management packs using pipeline
        $searchString = "MS21"
        Get-SCOMManagementPack | Get-SCOMManagementPackXml | Where-Object { $_ -match $searchString }
 
 
    .EXAMPLE
        # Find all management packs containing a specific server name
        $serverName = "MS21"
        $mpsWithServer = Get-SCOMManagementPack | Where-Object {
            $xmlContent = Get-SCOMManagementPackXml -ManagementPack $_
            $xmlContent -match $serverName
        }
        $mpsWithServer | Select-Object Name, DisplayName, Version
 
    .EXAMPLE
        # Search for a specific string in all management packs and display matching lines with context
        $matchstring = "MS21"
        $MPs = Get-SCOMManagementPack
        foreach ($MP in $MPs) {
            $xmlContent = Get-SCOMManagementPackXml -ManagementPack $MP
            if ($xmlContent -match $matchstring) {
                Write-Host "'$($matchstring)' found in Management Pack: $($MP.Name)" -F Yellow
                # Split the XML content into lines and find matching lines
                $lines = $xmlContent -split "`n"
                $matchingLines = $lines | Where-Object { $_ -match $matchstring }
                foreach ($line in $matchingLines) {
                    Write-Host "`t$($MP.Name) $line" -ForegroundColor Cyan
                }
            }
        }
 
    .NOTES
        Author: Tyson Paul
        Date: September 2, 2025
        Version: 1.0
 
        This function requires the SCOM PowerShell module to be loaded and a connection to a
        SCOM Management Group to be established.
 
    .LINK
        Get-SCOMManagementPack
        Export-SCOMManagementPack
    #>


    [CmdletBinding()]
    param(
        [Parameter(Mandatory, ValueFromPipeline)]
        [Microsoft.EnterpriseManagement.Configuration.ManagementPack]$ManagementPack
    )

    process {
        try {
            # There might be a better in-memory approach to export the XML content directly without using a temporary file,
            # but this is a straightforward method.
            $tempFolder = [System.IO.Path]::GetTempPath()

            # Export to temporary file
            $ManagementPack | Export-SCOMManagementPack -Path $tempFolder

            # Read the XML content
            $xmlContent = Get-Content -Path "$(Join-Path $tempFolder $ManagementPack.Name).xml" -Raw

            # Clean up temporary file
            Remove-Item -Path "$(Join-Path $tempFolder $ManagementPack.Name).xml" -Force -ErrorAction SilentlyContinue

            return $xmlContent
        }
        catch {
            Write-Error "Failed to export Management Pack XML: $($_.Exception.Message)"
        }
    }
}