-
Notifications
You must be signed in to change notification settings - Fork 2
/
New-MofFile.ps1
90 lines (77 loc) · 3.21 KB
/
New-MofFile.ps1
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
function New-MofFile
{
<#
.Synopsis
Generates a MOF Class declaration for a DSC Module
.DESCRIPTION
Uses the parameters of Set-TargetResource in a DSC Resource Module to generate a MOF schema file for use in DSC.
.EXAMPLE
New-MofFile -Name Pagefile
#>
param (
$Name,
$ModuleName,
$Version = '1.0.0'
)
if ([string]::IsNullOrEmpty($ModuleName))
{
$ModuleName = $Name
}
$Template = @"
[version("$Version"), FriendlyName("$ModuleName")]
class $Name : MSFT_BaseResourceConfiguration
{
"@
$CommonParameters = 'Verbose', 'Debug', 'ErrorAction', 'WarningAction',
'WarningVariable', 'ErrorVariable', 'OutVariable',
'OutBuffer', 'PipelineVariable'
$Command = get-command -Name Set-TargetResource -Module $ModuleName
foreach ($key in $Command.Parameters.Keys)
{
if ($CommonParameters -notcontains $key)
{
$CurrentParameter = $Command.Parameters[$key]
$IsKey = $false
$PropertyString = "`t"
$ParameterAttribute = $CurrentParameter.Attributes |
Where-Object {$_ -is [System.Management.Automation.ParameterAttribute]}
$ValidateSetAttribute = $CurrentParameter.Attributes |
Where-Object {$_ -is [System.Management.Automation.ValidateSetAttribute]}
if ($ParameterAttribute.Mandatory)
{
$PropertyString += '[Key'
$IsKey = $true
}
else
{
$PropertyString += '[write'
if ($ValidateSetAttribute -ne $null)
{
$OFS = '", "'
$PropertyString += ',ValueMap{"' + "$($ValidateSetAttribute.ValidValues)"
$PropertyString += '"},Values{"' + "$($ValidateSetAttribute.ValidValues)"
$PropertyString += '"}'
}
}
switch ($CurrentParameter.ParameterType)
{
{$_ -eq [System.String]} { $PropertyString += '] string ' + "$key;`n" }
{$_ -eq [System.Management.Automation.SwitchParameter]} { $PropertyString += '] boolean ' + "$key;`n"}
{$_ -eq [System.Management.Automation.PSCredential]} { $PropertyString += ',EmbeddedInstance("MSFT_Credential")] string ' + "$key;`n"}
{$_ -eq [System.String[]]} { $PropertyString += '] string ' + "$key[];`n" }
{$_ -eq [System.Int64]} { $PropertyString += '] sint64 ' + "$key;`n" }
{$_ -eq [System.Int64[]]} { $PropertyString += '] sint64 ' + "$key[];`n" }
{$_ -eq [System.Int32]} { $PropertyString += '] sint32 ' + "$key;`n" }
{$_ -eq [System.Int32[]]} { $PropertyString += '] sint32 ' + "$key[];`n" }
default { Write-Warning "Don't know what to do with $_";}
}
$Template += $PropertyString
}
}
$Template += @'
};
'@
$module = Get-Module $ModuleName
$Template |
Out-File -Encoding ascii -FilePath (join-path $Module.ModuleBase "$ModuleName.schema.mof")
}