-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDscConfiguration.psm1
161 lines (129 loc) · 4.41 KB
/
DscConfiguration.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
$references = @{}
$variables = @{}
$parameters = @{}
function SetVariables {
param (
[hashtable] $Source,
[hashtable] $Target
)
foreach ($key in $Source.Keys) {
$Target.Add($key, $Source[$key])
$varName = $key.Replace(".", "_")
New-Variable -Name $varName -Value $Source[$key] -Scope Script -Force | Out-Null
New-Item -Path env:$varName -Value $Source[$key] -ErrorAction SilentlyContinue
}
}
function GetDefaultValues {
param (
[hashtable] $Source
)
$values = @{}
foreach ($key in $Source.Keys) {
$values.Add($key, $Source[$key].defaultValue)
}
return $values
}
function parameters {
param ([string] $Name)
$value = $parameters[$Name]
return $value
}
function variables {
param ([string] $Name)
$value = $variables[$Name]
return $value
}
function reference {
param ([string] $Name)
$value = $references[$Name]
return $value
}
function equals {
param ([string] $Left, [string] $Right)
return [System.String]::Equals($Left, $Right)
}
function not {
param ([Boolean] $Statement)
return $Statement -ne $true
}
function Invoke-DscConfiguration {
param (
[string] $FilePath,
[ValidateSet("Test", "Set")]
[string] $Mode = "Test"
)
$fileExtension = [System.IO.Path]::GetExtension($FilePath)
if ($fileExtension -eq ".yaml" -or $fileExtension -eq ".yml") {
$pipeline = get-content $FilePath | ConvertFrom-Yaml
}
elseif ($fileExtension -eq ".json") {
$pipeline = get-content $FilePath | ConvertFrom-Json -AsHashtable
}
$parameters.Clear()
$variables.Clear()
$references.Clear()
$defaultValues = GetDefaultValues -Source $pipeline.parameters
SetVariables -Source $pipeline.variables -Target $variables
SetVariables -Source $defaultValues -Target $parameters
# Execute Tasks
foreach ($task in $pipeline.resources) {
Write-Host ""
Write-Host "Resource [$($task.type)/$($task.name)]"
if ($null -ne $task.condition -and (Invoke-Expression $task.condition) -eq $false) {
Write-Host "skipping" -ForegroundColor Cyan
continue
}
$module = $task.type.Split("/")[0]
$resourceType = $task.type.Split("/")[1]
# Install the module if needed
$installedModule = Get-InstalledModule -Name $module -ErrorAction SilentlyContinue
if ($null -eq $installedModule) {
Set-PSRepository -Name 'PSGallery' -InstallationPolicy Trusted
Install-Module -Name $module -Confirm:$false -AcceptLicense -Repository PSGallery
}
# Expand variables in the input values
$Property = @{}
foreach ($key in $task.properties.Keys) {
$inputValue = $ExecutionContext.InvokeCommand.ExpandString($task.properties[$key])
$Property.Add($key, $inputValue)
}
$resourceParameters = @{
Name = $resourceType
ModuleName = $module
Method = "Test"
Property = $Property
}
# Execute Test for task
$result = Invoke-DscResource @resourceParameters
if ($result.InDesiredState) {
Write-Host "ok" -ForegroundColor Green
}
elseif ($Mode -eq "Set") {
$resourceParameters.Method = "Set"
try {
Invoke-DscResource @resourceParameters
Write-Host "changed" -ForegroundColor Yellow
}
catch {
Write-Host "failed" -ForegroundColor Red
}
}
else {
Write-Host "change needed" -ForegroundColor Yellow
}
# Remove all Properties except required properties or else the Get method gets mad
$resource = Get-DscResource -Module $module -Name $resourceType
$mandatoryProperties = @()
$resource.Properties | where { $_.IsMandatory } | foreach { $mandatoryProperties += $_.Name }
$getProperties = @{}
foreach ($key in $resourceParameters.Property.Keys) {
if ($mandatoryProperties.Contains($key)) {
$getProperties.Add($key, $resourceParameters.Property[$key])
}
}
$resourceParameters.Method = "Get"
$resourceParameters.Property = $getProperties
$output_var = Invoke-DscResource @resourceParameters
$references.Add($task.name, $output_var)
}
}