-
Notifications
You must be signed in to change notification settings - Fork 1
/
DSC2XTA.psm1
228 lines (195 loc) · 7.08 KB
/
DSC2XTA.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
function Get-DSCVariables
{
[CmdletBinding()]
[OutputType([System.String[]])]
param(
[Parameter()]
[System.String]
$Content
)
$Tokens = $null
$ParseErrors = $null
$AST = [System.Management.Automation.Language.Parser]::ParseInput($Content, [ref]$Tokens, [ref]$ParseErrors)
$variables = @()
foreach ($token in $Tokens)
{
if ($token.Kind -eq 'Variable')
{
$variables += $token.Extent.Text
}
}
$variablesToExclude = @('$null', '$false', '$true', '$_')
# sort variable by length descending to avoid partial matches
return $variables | Where-Object { $_ -notin $variablesToExclude } | Select-Object -Unique | Sort-Object -Property Length -Descending
}
function Format-XTAProperty
{
[CmdletBinding()]
[OutputType([System.String])]
param(
[Parameter()]
[System.String]
$Property,
[Parameter()]
[System.String[]]
$Variables
)
foreach($variable in $Variables)
{
# matches params of the type : [parameters('FQDN')] where the parameter value is used as a single value.
if ($Property -eq $variable)
{
$Property = "[parameters('$($variable.Substring(1))')]"
return $Property
}
}
# matches params of the type : [concat('admin_', parameters('FQDN'), '@', parameters('Domain'), '.com')] where the parameter value is used within a list.
# Replace all variables with ,parameters('variableName'), and then split the string by ',' and then join the string with concat
$hasVariable = $false
foreach($variable in $Variables)
{
$hasVariable = $hasVariable -or $Property.Contains($variable)
# Replace doesn't work well with special characters, so we need to escape special characters the variable
$escapedVariable = [regex]::Escape($variable)
$property = $Property -replace $escapedVariable, ",parameters('$($variable.Substring(1))'),"
}
if($hasVariable)
{
$splits = @()
$property.Split(",") | ForEach-Object {
if($_ -ne "")
{
if($_ -match "parameters\('(.*)'\)")
{
$splits += $_
}
else
{
$splits += "'$_'"
}
}
}
$property = "[concat($($splits -join ', '))]"
}
return $Property
}
function Format-XTAProperties
{
[CmdletBinding()]
[OutputType([System.Collections.Hashtable])]
param(
[Parameter()]
[System.Collections.Hashtable]
$Resource,
[Parameter()]
[System.String[]]
$Variables
)
$ParsedResource = @{}
foreach ($key in $Resource.Keys)
{
$value = $Resource[$key]
$parsedValue = $value
if ($value -is [System.String])
{
$parsedValue = Format-XTAProperty -Property $value -Variables $Variables
}
elseif ($value -is [System.Collections.Hashtable])
{
$parsedValue = Format-XTAProperties -Resource $value -Variables $Variables
}
elseif ($value -is [System.Collections.ArrayList])
{
$parsedValue = @()
foreach ($item in $value)
{
if ($item -is [System.String])
{
$parsedItem = Format-XTAProperty -Property $item -Variables $Variables
$parsedValue += $parsedItem
}
elseif ($item -is [System.Collections.Hashtable])
{
$parsedItem = Format-XTAProperties -Resource $item -Variables $Variables
$parsedValue += $parsedItem
}
}
}
$ParsedResource.Add($key, $parsedValue)
}
return $ParsedResource
}
function ConvertFrom-DSCToXTA
{
[CmdletBinding()]
[OutputType([System.String])]
param(
[Parameter()]
[System.String]
$Path,
[Parameter()]
[System.String]
$Content,
[Parameter()]
[System.Boolean]
$Compress = $false
)
# Initialization - Skip
$Global:M365DSCSkipDependenciesValidation = $true
# Initialization - Load the Mapping Information
$mappingPath = Join-Path -Path $PSScriptRoot -ChildPath 'DSC2XTAMappings.psd1' -Resolve
$mappings = Import-PowerShellDataFile $mappingPath
# Initialization - Load the XTA Template
$templatePath = Join-Path -Path $PSScriptRoot -ChildPath 'XTATemplate.json' -Resolve
$templateContent = Get-Content -Path $templatePath -Raw
$template = ConvertFrom-JSON $templateContent
# If a path to a file is provided, then get its raw content
if ([System.String]::IsNullOrEmpty($Content) -and -not [System.String]::IsNullOrEmpty($Path))
{
$Content = Get-Content -Path $Path -Raw
}
# Use the DSCParser to convert the file's content into an array of
# PowerShell objects (Hashtables).
$parsedContent = ConvertTo-DSCObject -Content $Content `
-IncludeCIMInstanceInfo $false
# Get all the variables used in the DSC configuration
$variables = Get-DSCVariables -Content $Content
# Add the variables as parameters to the XTA template
foreach ($variable in $variables)
{
$variableName = $variable.Substring(1)
$template.Parameters += @{
displayName = $variableName
description = "The declaration of variable $variable."
parameterType = "String"
}
}
# Loop through all the resources and convert them to XTA
$allResources = @()
foreach ($resource in $parsedContent)
{
$mappedNamespace = $mappings.($resource.ResourceName)
if (-not [System.String]::IsNullOrEmpty($mappedNamespace))
{
$currentResource = @{
displayname = $resource.ResourceInstanceName
resourceType = $mappedNamespace
}
$resource.Remove("ResourceInstanceName") | Out-Null
$resource.Remove("ResourceName") | Out-Null
$resource.Remove("Credential") | Out-Null
$resource.Remove("ApplicationId") | Out-Null
$resource.Remove("TenantId") | Out-Null
$resource.Remove("CertificateThumbprint") | Out-Null
$resource.Remove("ApplicationSecret") | Out-Null
$resource.Remove("CertificatePath") | Out-Null
$resource.Remove("CertificatePassword") | Out-Null
$resource = Format-XTAProperties -Resource $resource -Variables $variables
$currentResource.Add("properties", $resource)
$allResources += $currentResource
}
}
$template.Resources = $allResources
$json = (ConvertTo-Json $template -Depth 99 -Compress:$Compress)
return [regex]::Unescape($json)
}