DscResource.Tests/DscResource.DocumentationHelper/MofHelper.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<#
.SYNOPSIS
 
Get-MofSchemaObject is used to read a .schema.mof file for a DSC resource
 
.DESCRIPTION
 
The Get-MofSchemaObject method is used to read the text content of the .schema.mof file
that all MOF based DSC resources have. The object that is returned contains all of the
data in the schema so it can be processed in other scripts.
 
.PARAMETER FileName
 
The full path to the .schema.mof file to process
 
.EXAMPLE
 
This example parses a MOF schema file
 
    $mof = Get-MofSchemaObject -FileName C:\repos\SharePointDsc\DSCRescoures\MSFT_SPSite\MSFT_SPSite.schema.mof
 
#>

function Get-MofSchemaObject
{
    [CmdletBinding()]
    [OutputType([System.Collections.Hashtable])]
    param (
        [Parameter(Mandatory = $true)]
        [string]
        $FileName
    )

    try
    {
        #region Workaround for OMI_BaseResource inheritance not resolving.

        $filePath = (Resolve-Path -Path $FileName).Path
        $tempFilePath = Join-Path -Path $env:TEMP -ChildPath "DscMofHelper_$((New-Guid).Guid).tmp"
        $moduleName = (Split-Path -Path $filePath -Leaf).Replace('.schema.mof',$null)
        $rawContent = (Get-Content -Path $filePath -Raw) -replace "$moduleName : OMI_BaseResource", $moduleName
        Set-Content -LiteralPath $tempFilePath -Value $rawContent -ErrorAction Stop

        # .NET methods don't like PowerShell drives
        $tempFilePath = Convert-Path -Path $tempFilePath

        #endregion

        $exceptionCollection = [System.Collections.ObjectModel.Collection[System.Exception]]::new()
        $moduleInfo = [System.Tuple]::Create('Module', [System.Version]'1.0.0')

        $class = [Microsoft.PowerShell.DesiredStateConfiguration.Internal.DscClassCache]::ImportClasses(
            $tempFilePath, $moduleInfo, $exceptionCollection
        )
    }
    catch
    {
        throw "Failed to import classes from file $FileName. Error $_"
    }
    finally
    {
        Remove-Item -LiteralPath $tempFilePath -Force
    }

    $attributes = foreach ($property in $class.CimClassProperties)
    {
        $state = switch ($property.flags)
        {
            {$_ -band [Microsoft.Management.Infrastructure.CimFlags]::Key}      {'Key'}
            {$_ -band [Microsoft.Management.Infrastructure.CimFlags]::Required} {'Required'}
            {$_ -band [Microsoft.Management.Infrastructure.CimFlags]::ReadOnly} {'Read'}
            default                                                             {'Write'}
        }

        @{
            Name             = $property.Name
            State            = $state
            DataType         = $property.CimType
            ValueMap         = $property.Qualifiers.Where({$_.Name -eq 'ValueMap'}).Value
            IsArray          = $property.CimType -gt 16
            Description      = $property.Qualifiers.Where({$_.Name -eq 'Description'}).Value
            EmbeddedInstance = $property.Qualifiers.Where({$_.Name -eq 'EmbeddedInstance'}).Value
        }
    }

    @{
        ClassName    = $class.CimClassName
        Attributes   = $attributes
        ClassVersion = $class.CimClassQualifiers.Where({$_.Name -eq 'ClassVersion'}).Value
        FriendlyName = $class.CimClassQualifiers.Where({$_.Name -eq 'FriendlyName'}).Value
    }
}

Export-ModuleMember -Function *