-
Notifications
You must be signed in to change notification settings - Fork 0
/
DscExecResource.ps1
148 lines (122 loc) · 4.8 KB
/
DscExecResource.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
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
<#
.SYNOPSIS
.
.DESCRIPTION
.
.INPUTS
.
.OUTPUTS
.
.EXAMPLE
PS C:\> DscExecResource
.
.LINK
https://github.com/claudiospizzi/PSDscExecutor
#>
function DscExecResource
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true, Position = 0)]
[System.String]
$ModuleName,
[Parameter(Mandatory = $true, Position = 1)]
[System.String]
$ResourceName,
[Parameter(Mandatory = $true, Position = 2)]
[System.String]
$Name,
[Parameter(Mandatory = $true, Position = 3)]
[System.Collections.Hashtable]
$Property
)
try
{
if ($ModuleName.Contains('@'))
{
# The module definition contains the required version. Extract the
# version of the definition and the get the DSC resource.
$ModuleName, $moduleVersion = $ModuleName.Split('@', 2)
$dscResource = Get-DscResource -Module @{ ModuleName = $ModuleName; RequiredVersion = $moduleVersion } -Name $ResourceName
}
else
{
# Get the DSC resource right away. Check if the DSC resource was
# loaded and extract the version.
$dscResource = Get-DscResource -Module $ModuleName -Name $ResourceName
$moduleVersion = $dscResource.Version
if ($null -eq $moduleVersion)
{
$moduleVersion = 'n/a'
if ($dscResource.Path -like 'C:\Windows\System32\WindowsPowershell\v1.0\Modules\PSDesiredStateConfiguration\DscResources\*')
{
$moduleVersion = '1.1'
}
}
}
# Verify all mandatory properties are part of the provided properties
# and extract them in a dedicated hash table.
$keyProperties = @{}
foreach ($propertyKey in $dscResource.Properties.Where({ $_.IsMandatory }).Name)
{
if ($Property.Keys -notcontains $propertyKey)
{
throw "The mandatory property $propertyKey is not specified in $ModuleName@$moduleVersion/$ResourceName/$Name."
}
$keyProperties[$propertyKey] = $Property[$propertyKey]
}
# Copy and verify all provided properties.
$allProperties = @{}
foreach ($propertyKey in $Property.Keys)
{
if ($dscResource.Properties.Name -notcontains $propertyKey)
{
throw "The property $propertyKey not found in the resource $ModuleName@$moduleVersion/$ResourceName."
}
$allProperties[$propertyKey] = $Property[$propertyKey]
}
# Prepare all the DSC invocation helpers
$invokeDscResourceVerbose = "[$ModuleName@$moduleVersion] [$ResourceName] [$Name]"
# $verboseResourceName = "[$ModuleName@$moduleVersion] [$ResourceName] [$Name]"
# $verboseResourceSpace = "[$ModuleName@$moduleVersion] [$ResourceName]" -replace '.', ' '
# $verboseInstanceName = "$verboseResourceName [$Name]"
# $verboseInstanceSpace = $verboseInstanceName -replace '.', ' '
$invokeDscResourceSplat = @{
Name = $ResourceName
ModuleName = @{
ModuleName = $ModuleName
RequiredVersion = $moduleVersion
}
}
Write-Verbose $invokeDscResourceVerbose
# Loop over the resource as long as it is not in the desired state.
$inDesiredState = $false
while (-not $inDesiredState)
{
Write-Verbose "$invokeDscResourceVerbose [Test] Start"
$inDesiredState = Invoke-DscResource @invokeDscResourceSplat -Method 'Test' -Property $allProperties | Select-Object -ExpandProperty 'InDesiredState'
Write-Verbose "$invokeDscResourceVerbose [Test] DesiredState = $inDesiredState"
Write-Verbose "$invokeDscResourceVerbose [Test] End"
if (-not $inDesiredState)
{
Write-Verbose "$invokeDscResourceVerbose [Set] Start"
Invoke-DscResource @invokeDscResourceSplat -Method 'Set' -Property $allProperties
Write-Verbose "$invokeDscResourceVerbose [Set] End"
}
}
Write-Verbose "$invokeDscResourceVerbose [Get] Start"
$stateData = Invoke-DscResource @invokeDscResourceSplat -Method 'Get' -Property $keyProperties
foreach ($stateDataPropertyKey in $stateData.Keys)
{
Write-Verbose "$invokeDscResourceVerbose [Get] $stateDataPropertyKey = $($stateData[$stateDataPropertyKey])"
}
Write-Verbose "$invokeDscResourceVerbose [Get] End"
Write-Verbose $invokeDscResourceVerbose
return $stateData
}
catch
{
$PSCmdlet.ThrowTerminatingError($_)
}
}