Skip to content

ScomManagementPack: Set() fails to import inline ManagementPackContent (encoding, filename, null path) #2

@raandree

Description

@raandree

Problem description

ScomManagementPack.Set() has three bugs that prevent importing management packs via the ManagementPackContent property (inline XML). All three are in the same code path and together make the ManagementPackContent feature completely non-functional:

  1. UTF-16 encoding causes XSD verification failure: The temp file is written with -Encoding Unicode (UTF-16LE with BOM), but the XML declaration says encoding="utf-8". SCOM's Import-SCManagementPack XSD parser rejects the encoding mismatch at line 1, position 40.

  2. Temp filename doesn't match MP identity: The temp file is named $this.Name.xml (the DSC resource key name, e.g., Dsc.ManagedNodes), but SCOM requires the filename to match the <ID> element in the management pack manifest (e.g., DSC.Management.Pack). This causes Identity mismatch: The loaded identity is 'DSC.Management.Pack' and the file name is 'Dsc.ManagedNodes.xml'.

  3. Null path guard missing: When only ManagementPackContent is set (no ManagementPackPath), the extension validation Get-Item -Path $this.ManagementPackPath executes before the content is written to a temp file, failing with Cannot bind argument to parameter 'Path' because it is null.

Verbose logs

Cannot bind argument to parameter 'Path' because it is null.
    + CategoryInfo          : InvalidData: (:) [], CimException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetItemCommand

After fixing bug 3 only:

XSD verification failed for the management pack. [Line: 1, Position: 40]

After fixing bugs 3 and 1:

Identity mismatch: The loaded identity (from the management pack manifest section) is 'DSC.Management.Pack' and the file name is 'Dsc.ManagedNodes.xml'. The identity of a management pack should be the same as its file name without the file extension.

DSC configuration

ScomManagementPacks:
  ManagementPacks:
    - Name: Dsc.ManagedNodes
      ManagementPackContent: |
        <?xml version="1.0" encoding="utf-8"?>
        <ManagementPack ContentReadable="true" SchemaVersion="2.0" ...>
          <Manifest>
            <Identity>
              <ID>DSC.Management.Pack</ID>
              <Version>1.0.0.21</Version>
            </Identity>
            ...
          </Manifest>
          ...
        </ManagementPack>

Suggested solution

Fix 1 — Change encoding from Unicode to UTF8:

# Before
Set-Content -Path $tmpPath -Force -Encoding Unicode -Value $this.ManagementPackContent

# After
Set-Content -Path $tmpPath -Force -Encoding UTF8 -Value $this.ManagementPackContent

Fix 2 — Extract the MP ID from the XML content for the filename:

# Before
$tmpPath = Join-Path -Path ([IO.Path]::GetTempPath()) -ChildPath "$($this.Name).xml"

# After
$mpId = $this.Name
if ($this.ManagementPackContent -match '<ID>([^<]+)</ID>') { $mpId = $Matches[1] }
$tmpPath = Join-Path -Path ([IO.Path]::GetTempPath()) -ChildPath "$mpId.xml"

Fix 3 — Guard the extension check with a null test on ManagementPackPath:

# Before
if ((Get-Item -Path $this.ManagementPackPath).Extension -notin '.xml', '.mp', '.mpb')

# After
if ($this.ManagementPackPath -and (Get-Item -Path $this.ManagementPackPath).Extension -notin '.xml', '.mp', '.mpb')

Operating system the target node is running

OsName               : Microsoft Windows Server 2025 Datacenter Evaluation
OSArchitecture       : 64-bit
WindowsVersion       : 2009
WindowsBuildLabEx    : 26100.1.amd64fre.ge_release.240331-1435

PowerShell version and build the target node is running

Name                           Value
----                           -----
PSVersion                      5.1.26100.7462
PSEdition                      Desktop
BuildVersion                   10.0.26100.7462
CLRVersion                     4.0.30319.42000

cScom version

Name  Version Path
----  ------- ----
cScom 1.0.5   C:\Program Files\WindowsPowerShell\Modules\cScom\1.0.5\cScom.psd1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions