This repository was archived by the owner on Jul 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathVersion5DSC.ps1
156 lines (128 loc) · 5.21 KB
/
Version5DSC.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
149
150
151
152
153
154
155
156
<#
NOTE FOR AZURE AUTOMATION DSC CLIENTS:
Partial Configs are NOT supported yet. Same should be true for Composite resources,
but actually isn't - they work as expected, due to this:
"(...) However, DSC composite resources can be imported and used in Azure Automation DSC Configurations like in local PowerShell, enabling configuration reuse."
SOURCE: https://docs.microsoft.com/en-us/azure/automation/automation-dsc-overview
Anyway, I've added the functionality (partial configs) for future use
Cheers,
Rad
#>
[cmdletbinding(SupportsShouldProcess=$True)]
Param
(
[Parameter(Mandatory=$True)]
[String[]]$ConfigurationNames,
[Parameter(Mandatory=$True)]
[String]$PullServerRegKey,
[Parameter(Mandatory=$True)]
[String]$PullServerURL
) # Params
Set-Location -Path $PSScriptRoot
Write-Verbose 'Constructing SetupLCM DSC Configuration object...'
#$ConfigurationNames = $($ConfigurationNames.Split(',')).Trim()
[DscLocalConfigurationManager()]
Configuration SetupLCM
{
Node $env:COMPUTERNAME
{
Settings
{
ActionAfterReboot = 'ContinueConfiguration'
AllowModuleOverwrite = $True
ConfigurationMode = 'ApplyAndAutoCorrect'
ConfigurationModeFrequencyMins = 60
RebootNodeIfNeeded = $True
RefreshFrequencyMins = 60
RefreshMode = 'PULL'
} # Settings
ConfigurationRepositoryWeb AzureAutomationDSC
{
ConfigurationNames = $ConfigurationNames
RegistrationKey = $PullServerRegKey
ServerUrl = $PullServerURL
} # Azure Automatio nDSC Pull Server
if ($ConfigurationNames.Count -gt 1)
{
foreach ($ConfigurationName in $ConfigurationNames)
{
PartialConfiguration $ConfigurationName
{
Description = "$ConfigurationName"
ConfigurationSource = @("[ConfigurationRepositoryWeb]AzureAutomationDSC")
} # Partial config
} # foreach
} # if
ResourceRepositoryWeb AzureAutomationDSC
{
RegistrationKey = $PullServerRegKey
ServerUrl = $PullServerURL
} # Azure Automation DSC Respository
ReportServerWeb AzureAutomationDSC
{
RegistrationKey = $PullServerRegKey
ServerUrl = $PullServerURL
} # Azure Automation DSC Report Server
} # Node
} # Configuration SetupLCM
Write-Verbose 'DONE!'
Write-Verbose ''
Write-Verbose 'Executing SetupLCM DSC Configuration object...'
if ($PSCmdlet.ShouldProcess('SetupLCM DSC Configuration', 'Executing'))
{
try
{
SetupLCM
} # try
catch
{
Write-Host -ForegroundColor Red "`tFailed to execute SetupLCM DSC Configuration!"
Write-Host -ForegroundColor Red "`tError details: $($Error[0].Exception)"
Write-Host -ForegroundColor Red "`tABORTING!"
break
} # catch
} # if
Write-Verbose 'DONE!'
Write-Verbose ''
Write-Verbose 'Applying SetupLCM DSC Configuration to self...'
if ($PSCmdlet.ShouldProcess('SetupLCM DSC Configuration', 'Applying'))
{
try
{
Set-DSCLocalConfigurationManager –Path .\SetupLCM –Verbose -ErrorAction Stop
} # try
catch
{
Write-Host -ForegroundColor Red "`tFailed to apply SetupLCM DSC Configuration to self!"
Write-Host -ForegroundColor Red "`tError details: $($Error[0].Exception)"
Write-Host -ForegroundColor Red "`tABORTING!"
break
} # catch
} # if
Write-Verbose 'DONE!'
Write-Verbose ''
Write-Verbose 'Creating ReRegisterLCM scheduled task...'
$Command = @"
schtasks /CREATE /RU "SYSTEM" /SC ONEVENT /TN "ReRegisterLCM" /TR "C:\cfn\DSC\ReRegisterLCM.bat" /F /RL HIGHEST /EC "Microsoft-Windows-DSC/Operational" /MO "*[System[Provider[@Name='Microsoft-Windows-DSC'] and EventID=4260]]"
"@
try
{
Set-Content -Path C:\cfn\DSC\ReRegisterLCM.bat -Value 'powershell -command "& {Set-DscLocalConfigurationManager -Path C:\cfn\DSC\SetupLCM\ -Force}"' -Force -ErrorAction Stop
} # try
catch
{
Write-Host -ForegroundColor Red "`tFailed to create ReRegisterLCM scheduled task's script file!"
Write-Host -ForegroundColor Red "`tError details: $($Error[0].Exception)"
Write-Host -ForegroundColor Red "`tABORTING!"
break
} # catch
try
{
Invoke-Expression -Command $Command -ErrorAction Stop
} # try
catch
{
Write-Host -ForegroundColor Red "`tFailed to create ReRegisterLCM scheduled task!"
Write-Host -ForegroundColor Red "`tError details: $($Error[0].Exception)"
} # catch
Write-Verbose 'DONE!'