-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDSCTools.psm1
More file actions
2219 lines (1817 loc) · 95.6 KB
/
DSCTools.psm1
File metadata and controls
2219 lines (1817 loc) · 95.6 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#Requires -Version 4.0
##########################################################################################################################################
# DSCTools
##########################################################################################################################################
# See README.md for additional information.
# Github Repo: https://github.com/PlagueHO/Powershell/tree/master/DSCTools
# Script Center: https://gallery.technet.microsoft.com/scriptcenter/DSC-Tools-c96e2c53
##########################################################################################################################################
# DSC Configurations for configuring DSC Pull Server and LCM
# These sections are now containined in separate files found in the .\Configurations folder.
# This is so that this module will load even if the configurations contain import-dscresource commands that import resources
# That aren't available on the local computer. For example if the computer being used has not yet had the DSC Resource Kit Installed.
##########################################################################################################################################
# Available Configuration Files
# -----------------------------
# Configuration Config_SetLCMPullMode
# Configuration Config_SetLCMPushMode
# Configuration Config_EnablePullServerHTTP
# Configuration Config_EnablePullServerSMB
##########################################################################################################################################
##########################################################################################################################################
# Default Configuration Variables
##########################################################################################################################################
# The DSCTools module contains some script variables that can be changed to allow the default properties
# of the module to be changed. This helps reduce the number of parameters that need to be passed to each
# DSCTools function if you want to configure your DSC system with parameters other than the default.
# This is the name of the pull server that will be used if no pull server parameter is passed to functions
# Setting this value is a lazy way of using a different pull server (rather than passing the pullserver parameter)
# to each function that needs it.
[System.String] $Script:DSCTools_DefaultPullServerName = 'localhost'
# This is the protocol that will be used by the DSC machines to connect to the pull server. This must be HTTP or HTTPS.
# If HTTPS is used then the HTTPS certificate on your Pull server must be trusted by all DSC Machines.
# This can also be set to SMB to use a pull server SMB share.
[System.String] $Script:DSCTools_DefaultPullServerProtocol = 'HTTP'
# This is the default endpoint name a Pull server will be created as when it is installed by Enable-DSCPullServer.
[System.String] $Script:DSCTools_DefaultPullServerEndpointName = 'PSDSCPullServer'
# This is the default endpoint name a Compliance server will be created as when it is installed by Enable-DSCPullServer.
[System.String] $Script:DSCTools_DefaultComplianceServerEndpointName = 'PSDSCComplianceServer'
# This is the location of the powershell modules folder where all the resources can be found that will be
# Installed into the pull server by the Publish-DscPullResources function.
[System.String] $Script:DSCTools_DefaultResourcePath = "$($ENV:PROGRAMFILES)\WindowsPowerShell\Modules\All Resources\"
# This is the default folder on your pull server where any resources will get copied to by the
# Publish-DscPullResources function. This can be a UNC path to a network share if required.
# This path may also be used by the Enable-DSCPullServer cmdlet as well.
[System.String] $Script:DSCTools_DefaultPullServerResourcePath = "$($ENV:PROGRAMFILES)\WindowsPowerShell\DscService\Modules\"
# This is the default folder where a DSC Pull Server will try and locate node configuraiton files.
# This should usually be a local path accessebile by the DSC Pull Server.
[System.String] $Script:DSCTools_DefaultPullServerConfigurationPath = "$($ENV:PROGRAMFILES)\WindowsPowerShell\DscService\Configuration\"
# This is the path and svc name component of the uRL used to access the Pull server.
[System.String] $Script:DSCTools_DefaultPullServerPath = 'PSDSCPullServer.svc'
# This is the default folder where a new DSC Pull Server IIS Web Site will be installed.
# This should always be a folder on the local DSC Pull Server.
[System.String] $Script:DSCTools_DefaultPullServerPhysicalPath = "$($ENV:SystemDrive)\inetpub\wwwroot\PSDSCPullServer\"
# This is the port the Pull server is running on.
[System.Uint32] $Script:DSCTools_DefaultPullServerPort = 8080
# This is the default folder where a new DSC Compliance Server IIS Web Site will be installed.
# This should always be a folder on the local DSC Pull Server.
[System.String] $Script:DSCTools_DefaultComplianceServerPhysicalPath = "$($ENV:SystemDrive)\inetpub\wwwroot\PSDSCComplianceServer\"
# This is the port the Compliance server is running on.
[System.Uint32] $Script:DSCTools_DefaultComplianceServerPort = 8090
# This is the URL to download the current version of the DSC Resource Kit.
# It may change when newer versions of the resource kit are released.
[System.String] $Script:DSCTools_ResourceKitURL = "https://gallery.technet.microsoft.com/scriptcenter/DSC-Resource-Kit-All-c449312d/file/131371/4/DSC%20Resource%20Kit%20Wave%2010%2004012015.zip"
# This is the default folder the functions Start-DSCPull, Start-DSCPush and Update-DSCNodeConfiguration functions will look for
# MOF files for node configuration. In future they may also look for PS1 files that can be converted to MOF files.
[System.String] $Script:DSCTools_DefaultNodeConfigSourceFolder = "$HOME\Documents\"
# This is the version of PowerShell that the Configuration files should be built to use.
# This is for future use when WMF 5.0 is available the LCM configuration files can be
# written in a more elegant fashion. Currently this should always be set to 4.0
[Float] $Script:DSCTools_PSVersion = 4.0
##########################################################################################################################################
# Internal Module Variables/Constants
##########################################################################################################################################
# Get the PS Version to a variable for easier access.
[System.Uint32] $Script:PSVersion = $PSVersionTable.PSVersion.Major
# This is the location the latest version of the DSCTools module can be downloaded from.
[System.String] $Script:DSCTools_ModuleDownloadURL = 'https://github.com/PlagueHO/Powershell/raw/master/DSCTools/Package/DSCTools.zip'
##########################################################################################################################################
##########################################################################################################################################
# Support Functions
##########################################################################################################################################
function InitZip
{
# If PS is version 4 or less then we require the PSCX Module to unzip/zip files
if ($Script:PSVersion -lt 5)
{
# Is the PSCX Module Available?
if ( (Get-Module -ListAvailable PSCX | Measure-Object).Count -eq 0)
{
throw "PSCX Module is not available. Please download it from http://pscx.codeplex.com/"
} # If
Import-Module PSCX
} # If
} # function InitZip
function UnzipFile ([System.String] $ZipFileName, [System.String] $DestinationPath)
{
if ($Script:PSVersion -lt 5)
{
Expand-Archive -Path $ZipFileName -OutputPath $DestinationPath -Force
}
else
{
Expand-Archive -Path $ZipFileName -DestinationPath $DestinationPath -Force
} # If
} # function UnzipFile
function ZipFolder ([System.String] $ZipFileName, [System.String] $SourcePath)
{
if ($Script:PSVersion -lt 5)
{
Get-ChildItem -Path $SourcePath -Recurse | Write-Zip -IncludeEmptyDirectories -OutputPath $ZipFileName -EntryPathRoot $SourcePath -Level 9
}
else
{
Compress-Archive -DestinationPath $ZipFileName -Path "$SourcePath\*" -CompressionLevel Optimal
} # If
} # function ZipFolder
function IsLocalHost ([System.String] $Name)
{
Return (($Name -match 'localhost') -or ($Name -match '127.0.0.1') -or ("$Name." -match "$ENV:COMPUTERNAME\."))
} # function IsLocalHost
##########################################################################################################################################
<#
.SYNOPSIS
Checks for updated versions of the DSCTools module and installs the udpated version if it is available.
.DESCRIPTION
This will look online for an updated version of the DSC Tools module and download it and install it if it is available.
It currently always downloads and installs the latest version from the GitHub Repository:
https://github.com/PlagueHO/Powershell/raw/master/DSCTools/Package/DSCTools.zip
However, once the PowerShell Gallery is publicly available and if WMF 5.0 is installed then the Install-Module/Update-Module can be used.
If PS 4 is used then this function requires the PSCX module to be available and installed on this computer.
PSCX Module can be downloaded from http://pscx.codeplex.com/
.EXAMPLE
Update-DSCTools
Will update the DSCTools module.
.LINK
http://pscx.codeplex.com/
#>
function Update-DSCTools
{
[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'High')]
param ()
if ($pscmdlet.ShouldProcess($ENV:COMPUTERNAME, "Install the latest version of DSCTools module?"))
{
InitZip
# Download the module zip file DSCTools.zip
[System.String] $TempPath = Join-Path -Path $ENV:TEMP -ChildPath DSCTools.zip
Write-Verbose -Message "Update-DSCTools: Downloading $Script:DSCTools_ModuleDownloadURL to $TempPath"
try
{
Invoke-WebRequest $Script:DSCTools_ModuleDownloadURL -OutFile $TempPath
}
catch
{
throw
}
# Unzip the Module
[System.String] $ModuleDest = Split-Path $PSScriptRoot
Write-Verbose -Message "Update-DSCTools: Unzipping $TempPath to $ModuleDest"
UnzipFile -ZipFileName $TempPath -DestinationPath $ModuleDest
# Reload the module
Write-Verbose -Message "Update-DSCTools: Unloading current DSCTools Module"
Remove-Module DSCTools
Write-Verbose -Message "Update-DSCTools: Loading new DSCTools Module"
Import-Module (Join-Path -Path $PSScriptRoot -ChildPath 'DSCTools.psm1')
Write-Verbose -Message "Update-DSCTools: Deleting Module Package $TempPath"
Import-Module (Join-Path -Path $PSScriptRoot -ChildPath 'DSCTools.psm1')
}
} # function Update-DSCTools
<#
.SYNOPSIS
Forces the LCM on the specified nodes to trigger a DSC check.
.DESCRIPTION
This function will cause the Local Configuration Manager on the nodes provided to trigger a DSC check. If a node is set for pull mode
then the latest DSC configuration will be pulled down from the pull server. If a node is in push mode then the current DSC configuration
will be used.
The command is executed via a call to Invoke-Command on the destination computer's LCM which will be called via WinRM.
Therefore WinRM must be enabled on the destination computer's LCM and the appropriate firewall ports opened.
.PARAMETER ComputerName
This parameter should contain a list of computers that will have the a DSC check triggered.
.PARAMETER Nodes
This must contain an array of hash tables. Each hash table will represent a node that a DSC check should be triggered.
This parameter is provided to be consistent with the Start-DSCPullMode and Start-DSCPushMode functions.
The hash table must contain the following entries (other entries will be ignored):
Name =
For example:
@(@{Name='SERVER01'},@{Name='SERVER02'})
.PARAMETER SkipConnectionCheck
Some machines will falsely return that they are not contactable when they are actually able to be contacted. This swtich
causes the cmdlet to skip the connection test to each node and will always allow the check to be performed.
.EXAMPLE
Invoke-DSCCheck -ComputerName SERVER01,SERVER02,SERVER03
Causes the LCMs on computers SERVER01, SERVER02 and SERVER03 to repull DSC Configuration MOF files from the DSC Pull server.
.EXAMPLE
Invoke-DSCCheck -Nodes @(@{Name='SERVER01'},@{Name='SERVER02'})
Causes the LCMs on computers SERVER01 and SERVER02 to repull DSC Configuration MOF files from the DSC Pull server.
#>
function Invoke-DSCCheck
{
[CmdletBinding()]
param (
[Parameter(
ParameterSetName = 'ComputerName',
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true
)]
[String[]]$ComputerName,
[Parameter(
ParameterSetName = 'Nodes'
)]
[Array] $Nodes,
[Switch] $SkipConnectionCheck = $false
) # Param
Begin
{
}
Process
{
if ($null -eq $ComputerName)
{
# Load all the nodes into the computername array.
$ComputerName = @()
foreach ($Node In $Nodes)
{
$ComputerName += $Node.Name
} # foreach
} # foreach
foreach ($Computer In $ComputerName)
{
# If PS5 is installed then the Update-DscConfiguration command can be called -otherwise we need to
# use Invoke-CimMethod on the remote host.
if (IsLocalHost($Computer))
{
if ($Script:PSVersion -lt 5)
{
Write-Verbose -Message "Invoke-DSCCheck: Invoking Method PerformRequiredConfigurationChecks on Localhost"
# For some reason using the Invoke-CimMethod cmdlet with the -ComputerName parameter doesn't work
# So the Invoke-Command is used instead to execute the command on the destination computer.
Invoke-CimMethod `
-Namespace 'root/Microsoft/Windows/DesiredStateConfiguration' `
-ClassName 'MSFT_DSCLocalConfigurationManager' `
-MethodName 'PerformRequiredConfigurationChecks' `
-Arguments @{ Flags = [uint32]1 }
}
else
{
Write-Verbose -Message "Invoke-DSCCheck: Calling Update-DscConfigration on Localhost"
Update-DscConfiguration
} # If
}
else
{
if (($SkipConnectionCheck) -or (Test-Connection -ComputerName $Computer -Count 1 -Quiet))
{
if ($Script:PSVersion -lt 5)
{
Write-Verbose -Message "Invoke-DSCCheck: Invoking Method PerformRequiredConfigurationChecks on node $Computer"
# For some reason using the Invoke-CimMethod cmdlet with the -ComputerName parameter doesn't work
# So the Invoke-Command is used instead to execute the command on the destination computer.
Invoke-Command -ComputerName $Computer { `
Invoke-CimMethod `
-Namespace 'root/Microsoft/Windows/DesiredStateConfiguration' `
-ClassName 'MSFT_DSCLocalConfigurationManager' `
-MethodName 'PerformRequiredConfigurationChecks' `
-Arguments @{ Flags = [uint32]1 }
} # Invoke-Command
}
else
{
Write-Verbose -Message "Invoke-DSCCheck: Calling Update-DscConfigration on node $Computer"
Update-DscConfiguration -ComputerName $Computer
} # If
}
else
{
Write-Error -Message "Invoke-DSCCheck: Error contacting $Computer. DSC check could not be triggered."
}
} # If
} # foreach ($Computer In $ComputerName)
} # Process
End
{
}
} # function Invoke-DSCCheck
<#
.SYNOPSIS
Publishes DSC Resources to a DSC pull server.
.DESCRIPTION
This function takes a path where all the source DSC resources are contained in subfolders.
These resources will then be zipped up and renamed based on the manifest version found in the resource.
A checksum file will also be created for each resource zip.
The resource zip and checksum will then be moved into the folder provided in the PullServerResourcePath paramater.
If PS 4 is used then this function requires the PSCX module to be available and installed on this computer.
PSCX Module can be downloaded from http://pscx.codeplex.com/
.PARAMETER ModulePath
This is the path containing the folders containing all the DSC resources.
If this is not passed the default path of "c:\program files\windowspowershell\modules\" will be used.
.PARAMETER PullServerResourcePath
This is the destination path to which the zipped resources and checksum files will be written to.
The user running this command must have write access to this folder.
If this parameter is not set the path will be set to:
c:\Program Files\WindowsPowerShell\DscService\Modules
.EXAMPLE
Publish-DscPullResources -ModulePath 'c:\program files\windowspowershell\modules\all resources\a*' `
-PullServerResourcePath '\\DSCPullServer\c$\program files\windowspowershell\DSCService\Modules'
This will cause all resources found in the c:\program files\windowspowershell\modules\all resources\ folder
starting with the letter A to be zipped up and copied into the folder
\\DSCPullServer\c$\program files\windowspowershell\DSCService\Modules
A checksum file will also be created for each zipped resource.
.EXAMPLE
Publish-DscPullResources -ModulePath 'c:\program files\windowspowershell\modules\all resources\*'
This will cause all resources found in the c:\program files\windowspowershell\modules\all resources\ folder
to be zipped up and copied into the folder found in the default variable $Script:DSCTools_DefaultPullServerResourcePath.
A checksum file will also be created for each zipped resource.
.EXAMPLE
'c:\program files\windowspowershell\modules\all resources\','c:\powershell\modules\' | Publish-DscPullResources `
-PullServerResourcePath '\\DSCPullServer\c$\program files\windowspowershell\DSCService\Modules'
This will cause all resources found in either the c:\program files\windowspowershell\modules\all resources folder or
c:\powershell\modules\ folder to be zipped up and copied into the folder
\\DSCPullServer\c$\program files\windowspowershell\DSCService\Modules
A checksum file will also be created for each zipped resource.
.LINK
http://pscx.codeplex.com/
#>
function Publish-DscPullResources
{
[CmdletBinding()]
param (
[Parameter(ValueFromPipeline = $true)]
[Alias('FullName')]
[System.String[]] $ModulePath = $Script:DSCTools_DefaultResourcePath,
[Parameter()]
[ValidateNotNullOrEmpty()]
[System.String] $PullServerResourcePath = $Script:DSCTools_DefaultPullServerResourcePath
) # Param
Begin
{
InitZip
# Check the Pull Server Resource Path exists.
if ((Test-Path -Path $PullServerResourcePath -PathType Container) -eq $false)
{
throw "Folder $PullServerResourcePath could not be found."
}
} # Begin
Process
{
foreach ($path in $ModulePath)
{
Write-Verbose -Message "Publish-DscPullResources: Examining $Path for Resource Folders"
if (Test-Path -Path $path -PathType Container)
{
# This path in the source path array is a folder
Write-Verbose -Message "Publish-DscPullResources: Folder $Path Found"
# Get all the subfolders
$resources = Get-ChildItem -Path $path -Attributes Directory
foreach ($resource in $resources)
{
Write-Verbose -Message "Publish-DscPullResources: Possible Resource Folder $resource Found"
# A folder was found inside the source path - does it contain a resource?
$resourceName = Split-Path -Path $resource -Leaf
$manifests = Get-ChildItem -Path $resource -Filter "$resourceName.psd1" -Recurse
foreach ($manifest in $manifests)
{
$resourcePath = Split-Path -Path ($manifest.FullName) -Parent
$dscResourcesFolder = Join-Path -Path $resourcePath -ChildPath 'DSCResources'
if ((Test-Path -Path $resourcePath -PathType Container) -and (Test-Path -Path $dscResourcesFolder -PathType Container))
{
Write-Verbose -Message "Publish-DscPullResources: Resource $resourceName in Resource Folder $resourcePath Found"
# This folder appears to contain a valid DSC Resource
# Get the version number out of the manifest file
$manifestContent = Invoke-Expression -Command (Get-Content -Path $($manifest.FullName) -Raw)
$moduleVersion = $manifestContent.ModuleVersion
Write-Verbose -Message "Publish-DscPullResources: Resource $resourceName in Resource Folder $resourcePath is Version $moduleVersion"
# Generate the Zip file name (including the destination to the pull server folder)
$zipFileName = Join-Path -Path $PullServerResourcePath -ChildPath "$($resourceName)_$($moduleVersion).zip"
# Zip up the resource straight into the pull server resources path
if (Test-Path -Path $zipFileName)
{
Write-Verbose -Message "Publish-DscPullResources: Deleting Existing Resource File $zipFileName"
Remove-Item -Path $zipFileName
}
Write-Verbose -Message "Publish-DscPullResources: Zipping $resourcePath to $zipFileName"
ZipFolder -ZipFileName $zipFileName -SourcePath $resourcePath
# Generate the checksum for the zip file
$null = New-DSCCheckSum -ConfigurationPath $zipFileName -Force
Write-Verbose -Message "Publish-DscPullResources: Checksum for Resource File $zipFileName Created"
} # If
} # foreach ($manifest in $manifests)
} # foreach ($resource in $resources)
}
else
{
Write-Verbose -Message "Publish-DscPullResources: File $path Is Ignored"
} # If
} # foreach ($path in $modulePath)
} # Process
End
{
}
} # function Publish-DscPullResources
<#
.SYNOPSIS
Downloads and installs the DSC Resource Kit. It can also optionally publish the Resources to a pull server.
.DESCRIPTION
The DSC Resource Kit is a set of DSC Resources and other tools that are commonly used by DSC servers and nodes. It can be downloaded
manually from the Microsoft Script Center Gallery.
This function will attempt to download this file automatically and install it to the c:\program files\windows powershell\modules folder
on this computer.
If PS 4 is used then this function requires the PSCX module to be available and installed on this computer.
PSCX Module can be downloaded from http://pscx.codeplex.com/
.PARAMETER ResourceKitURL
This is the URL to use to download the DSC Resource Kit from. It defaults to the URL contained in $Script:DSCTools_ResourceKitURL.
.PARAMETER ModulePath
This optional parameter allows an alternate folder to install the DSC Resource Kit into. By default it will be installed into
$($ENV:PROGRAMFILES)\windowspowershell\modules
The Resouce Kit zip file contains a single folder called All Resources that will be created within the Modules folder.
All Resources will be inside this folder. All other cmdlets default to using this folder.
.PARAMETER Publish
If this switch is set to $true the DSC Resorce Kit files will also be published using Publish-DscPullResources.
.PARAMETER UseCache
If this switch is set to $true then the DSC Resouce Kit File will not be redownloaded if one already exists in the temp folder.
If one does not exist it will be downloaded and it will not be deleted after the cmdlet finishes.
.PARAMETER PullServerResourcePath
This is the destination path to which the zipped resources and checksum files will be written to. The user running this command must have write access to this folder.
Note: If this is a SMB Pull Server then resources should be installed into the same folder as the configuration files.
.EXAMPLE
Install-DSCResourceKit -Publish
.LINK
http://pscx.codeplex.com/
#>
function Install-DSCResourceKit
{
[CmdletBinding()]
param (
[ValidateNotNullOrEmpty()]
[System.String] $ResourceKitURL = $Script:DSCTools_ResourceKitURL,
[ValidateNotNullOrEmpty()]
[System.String] $ModulePath = "$($ENV:PROGRAMFILES)\windowspowershell\modules",
[Switch] $Publish = $false,
[Switch] $UseCache = $false,
[ValidateNotNullOrEmpty()]
[System.String] $PullServerResourcePath = $Script:DSCTools_DefaultPullServerResourcePath
) # Param
InitZip
if ($Publish)
{
# Check the Pull Server Resource Path exists.
if ((Test-Path -Path $PullServerResourcePath -PathType Container) -eq $false)
{
throw "$PullServerResourcePath could not be found."
}
}
# Attempt to download the Resource kit file to the temp folder.
$TempPath = "$Env:TEMP\DSCResourceKit.zip"
if ((Test-Path -Path $TempPath) -and ($UseCache))
{
Write-Verbose -Message "Install-DSCResourceKit: Using Cached Resource Kit File $TempPath"
}
else
{
Write-Verbose -Message "Install-DSCResourceKit: Downloading $ResourceKitURL to $TempPath"
try
{
Invoke-WebRequest $ResourceKitURL -OutFile $TempPath
}
catch
{
throw
}
}
# Unzip the Resouce Kit File
Write-Verbose -Message "Install-DSCResourceKit: Extracting $TempPath to $ModulePath"
try
{
UnzipFile -ZipFileName $TempPath -DestinationPath $ModulePath
}
catch
{
throw
} # try
if ($Publish)
{
# Publish the Resources from the Resource Kit
Write-Verbose -Message "Install-DSCResourceKit: Publishing Resources from $ModulePath to $PullServerResourcePath"
Publish-DscPullResources -ModulePath (Join-Path -Path $ModulePath -ChildPath "All Resources") -PullServerResourcePath $PullServerResourcePath
} # If
if ($UseCache -eq $false)
{
Write-Verbose -Message "Install-DSCResourceKit: Deleting Resource Kit File $TempPath"
Remove-Item -Path $TempPath
} # If
} # function Install-DSCResourceKit
<#
.SYNOPSIS
Installs and configures one or more servers as a DSC Pull Servers.
.DESCRIPTION
This function will create a MOF file for configuring a Windows Server computer to be a DSC Pull Server and then force DSC to apply the MOF to the server.
The name of as least one computer to install as a Pull Server is mandatory. Multiple computers can be specified to install more than one Pull Server.
Important Note: The server that will be installed onto must contain the DSC module xPSDesiredStateConfiguration installed into the PowerShell Module path. This module is part of the DSC Resource kit found here: https://gallery.technet.microsoft.com/scriptcenter/DSC-Resource-Kit-All-c449312d
The function will:
1. Create the node DSC Pull Server configuration MOF file for the server.
2. Execute the node DSC Pull Server configuration MOF on the server.
If the Pull Server Protocol is set to SMB then the Ports, Endpoint,
.PARAMETER Nodes
Must contain an array of hash tables. Each hash table will represent a node that should be configured as a DSC Pull Server.
The hash table must contain the following entries:
Name = Name of the computer to install as a DSC Pull Server.
Each hash entry can also contain the following optional items. If each item is not specified it will default.
PullServerProtocol = The protocol the Pull Server will use. Defaults to $Script:DSCTools_DefaultPullServerProtocol.
PullServerPort = The port the Pull Server will run on. Defaults to $Script:DSCTools_DefaultPullServerPort.
ComplianceServerPort = The port the Complaince Server will run on. Defaults to $Script:DSCTools_DefaultComplianceServerPort.
CertificateThumbprint = The certificate thumbprint to use if HTTPS should be used. Defaults to using HTTP.
PullServerEndpointName = The endpoint name to use when creating the Pull Server web site. Defaults to $Script:DSCTools_DefaultPullServerEndpointName.
PullServerResourcePath = The path the DSC Pull Server will look for resource files in. Defaults to $Script:DSCTools_DefaultPullServerResourcePath.
PullServerConfigurationPath = The path the DSC Pull Server will use look for configuration (MOF) files in. Defaults to $Script:DSCTools_DefaultPullServerConfigurationPath.
PullServerPhysicalPath = The local path to where the DSC Pull Server web site will be created. Defaults to $Script:DSCTools_DefaultPullServerPhysicalPath.
ComplianceServerEndpointName = The endpoint name to use when creating the Compliance Server web site. Defaults to $Script:DSCTools_DefaultComplianceServerEndpointName.
ComplianceServerPhysicalPath = The local path to where the DSC Compliance Server web site will be created. Defaults to $Script:DSCTools_DefaultComplianceServerPhysicalPath.
Credential = Credentials to use to configure the DSC Pull Server using. Defaults to none.
For example:
@(@{Name='DSCPULLSRV01';},@{Name='DSCPULLSRV01';})
.PARAMETER ComputerName
Name of the computer to install as a DSC Pull Server.
.PARAMETER PullServerProtocol
The protocol the Pull Server will use. Defaults to $Script:DSCTools_DefaultPullServerProtocol.
.PARAMETER PullServerPort
The port the Pull Server will run on. Defaults to $Script:DSCTools_DefaultPullServerPort.
.PARAMETER ComplianceServerPort
The port the Complaince Server will run on. Defaults to $Script:DSCTools_DefaultComplianceServerPort.
.PARAMETER CertificateThumbprint
The certificate thumbprint to use if HTTPS should be used. Defaults to using HTTP.
.PARAMETER PullServerEndpointName
The endpoint name to use when creating the Pull Server web site. Defaults to $Script:DSCTools_DefaultPullServerEndpointName.
.PARAMETER PullServerResourcePath
The path the DSC Pull Server will look for resource files in. Defaults to $Script:DSCTools_DefaultPullServerResourcePath.
.PARAMETER PullServerConfigurationPath
The path the DSC Pull Server will use look for configuration (MOF) files in. Defaults to $Script:DSCTools_DefaultPullServerConfigurationPath.
.PARAMETER PullServerPhysicalPath
The local path to where the DSC Pull Server web site will be created. Defaults to $Script:DSCTools_DefaultPullServerPhysicalPath.
.PARAMETER ComplianceServerEndpointName
The endpoint name to use when creating the Compliance Server web site. Defaults to $Script:DSCTools_DefaultComplianceServerEndpointName.
.PARAMETER ComplianceServerPhysicalPath
The local path to where the DSC Compliance Server web site will be created. Defaults to $Script:DSCTools_DefaultComplianceServerPhysicalPath.
.PARAMETER Credential
Credentials to use to configure the DSC Pull Server using. Defaults to none.
.PARAMETER SkipConnectionCheck
Some machines will falsely return that they are not contactable when they are actually able to be contacted. This swtich
causes the cmdlet to skip the connection test to each node and will always allow the set up to be performed.
.EXAMPLE
Enable-DSCPullServer -Nodes @(@{Name='DSCPULLSRV01';},@{Name='DSCPULLSRV01';})
This command will install and configure a DSC Pull Server onto machines DSCPULLSRV01 and DSCPULLSRV02.
.EXAMPLE
Enable-DSCPullServer -ComputerName DSCPULLSRV01
This command will install and configure a DSC Pull Server onto machine DSCPULLSRV01
#>
function Enable-DSCPullServer
{
[CmdletBinding(DefaultParameterSetName = 'ComputerName')]
param (
[Parameter(ParameterSetName = 'ComputerName')]
[ValidateNotNullOrEmpty()]
[System.String] $ComputerName = 'localhost',
[Parameter(ParameterSetName = 'ComputerName')]
[ValidateSet('HTTP', 'HTTPS', 'SMB')]
[System.String] $PullServerProtocol,
[Parameter(ParameterSetName = 'ComputerName')]
[ValidateNotNullOrEmpty()]
[System.Uint32] $PullServerPort,
[Parameter(ParameterSetName = 'ComputerName')]
[ValidateNotNullOrEmpty()]
[System.Uint32] $ComplianceServerPort,
[Parameter(ParameterSetName = 'ComputerName')]
[ValidateNotNullOrEmpty()]
[System.String] $CertificateThumbprint,
[Parameter(ParameterSetName = 'ComputerName')]
[ValidateNotNullOrEmpty()]
[System.String] $PullServerEndpointName,
[Parameter(ParameterSetName = 'ComputerName')]
[ValidateNotNullOrEmpty()]
[System.String] $PullServerResourcePath,
[Parameter(ParameterSetName = 'ComputerName')]
[ValidateNotNullOrEmpty()]
[System.String] $PullServerConfigurationPath,
[Parameter(ParameterSetName = 'ComputerName')]
[ValidateNotNullOrEmpty()]
[System.String] $PullServerPhysicalPath,
[Parameter(ParameterSetName = 'ComputerName')]
[ValidateNotNullOrEmpty()]
[System.String] $ComplianceServerEndpointName,
[Parameter(ParameterSetName = 'ComputerName')]
[ValidateNotNullOrEmpty()]
[System.String] $ComplianceServerPhysicalPath,
[Parameter(ParameterSetName = 'ComputerName')]
[ValidateNotNullOrEmpty()]
[PSCredential]$Credential,
[Parameter(ParameterSetName = 'Nodes')]
[Array] $Nodes,
[Switch] $SkipConnectionCheck = $false
)
# Set up a temporary path
$TempPath = "$Env:TEMP\Enable-DSCPullServer"
Write-Verbose -Message "Enable-DSCPullServer: Creating Temporary Folder $TempPath."
$null = New-Item -Path $TempPath -ItemType 'Directory' -Force
if (-not $Nodes)
{
$Nodes = @{
Name = $ComputerName;
PullServerProtocol = $PullServerProtocol;
PullServerPort = $PullServerPort;
ComplianceServerPort = $ComplianceServerPort;
CertificateThumbprint = $CertificateThumbprint;
PullServerEndpointName = $PullServerEndpointName;
PullServerResourcePath = $PullServerResourcePath;
PullServerConfigurationPath = $PullServerConfigurationPath;
PullServerPhysicalPath = $PullServerPhysicalPath;
ComplianceServerEndpointName = $ComplianceServerEndpointName;
ComplianceServerPhysicalPath = $ComplianceServerPhysicalPath;
Credential = $Credential;
}
} # If
foreach ($Node In $Nodes)
{
# Create the Pull Mode MOF that will configure the elements on this computer needed for Pull Mode
[System.String] $NodeName = $Node.Name
if (($NodeName -eq '') -or ($null -eq $NodeName))
{
$NodeName = $ENV:COMPUTERNAME
} # If
# Get the Pull Server Protocol
[System.String] $PullServerProtocol = $Node.PullServerProtocol
if (($PullServerProtocol -eq '') -or ($null -eq $PullServerProtocol))
{
$PullServerProtocol = $Script:DSCTools_DefaultPullServerProtocol
} # if
Write-Verbose -Message "Enable-DSCPullServer: Enabling $PullServerProtocol Pull Server $NodeName"
# Get the credentials that need to be used to apply the DSC Config to the Pull Server
[PSCredential]$Credential = $Node.Credential
if ($PullServerProtocol -match 'http')
{
# An HTTP/HTTPS Pull Server is required
[System.String] $CertificateThumbprint = $Node.CertificateThumbprint
# Get the certificate thumbprint
if (($CertificateThumbprint -eq '') -or ($null -eq $CertificateThumbprint))
{
# If the pull server is HTTPS and no certificate thumbprint was provided then throw an error.
if ($PullServerProtocol -match 'https')
{
throw "A certificate thumbprint must be provided if the Pull Server protocol is set to HTTPS"
}
$CertificateThumbprint = 'AllowUnencryptedTraffic'
} # if
# Get all the Pull Server properties from the node or use defaults.
[System.Uint32] $PullServerPort = $Node.PullServerPort
if (($PullServerPort -eq 0) -or ($null -eq $PullServerPort))
{
$PullServerPort = $Script:DSCTools_DefaultPullServerPort
} # if
[System.Uint32] $ComplianceServerPort = $Node.ComplianceServerPort
if (($ComplianceServerPort -eq 0) -or ($null -eq $ComplianceServerPort))
{
$ComplianceServerPort = $Script:DSCTools_DefaultComplianceServerPort
} # if
[System.String] $PullServerEndpointName = $Node.PullServerEndpointName
if (($PullServerEndpointName -eq '') -or ($null -eq $PullServerEndpointName))
{
$PullServerEndpointName = $Script:DSCTools_DefaultPullServerEndpointName
} # if
[System.String] $PullServerResourcePath = $Node.PullServerResourcePath
if (($PullServerResourcePath -eq '') -or ($null -eq $PullServerResourcePath))
{
$PullServerResourcePath = $Script:DSCTools_DefaultPullServerResourcePath
} # if
[System.String] $PullServerConfigurationPath = $Node.PullServerConfigurationPath
if (($PullServerConfigurationPath -eq '') -or ($null -eq $PullServerConfigurationPath))
{
$PullServerConfigurationPath = $Script:DSCTools_DefaultPullServerConfigurationPath
} # if
[System.String] $PullServerPhysicalPath = $Node.PullServerPhysicalPath
if (($PullServerPhysicalPath -eq '') -or ($null -eq $PullServerPhysicalPath))
{
$PullServerPhysicalPath = $Script:DSCTools_DefaultPullServerPhysicalPath
} # if
[System.String] $ComplianceServerEndpointName = $Node.ComplianceServerEndpointName
if (($ComplianceServerEndpointName -eq '') -or ($null -eq $ComplianceServerEndpointName))
{
$ComplianceServerEndpointName = $Script:DSCTools_DefaultComplianceServerEndpointName
} # if
[System.String] $ComplianceServerPhysicalPath = $Node.ComplianceServerPhysicalPath
if (($ComplianceServerPhysicalPath -eq '') -or ($null -eq $ComplianceServerPhysicalPath))
{
$ComplianceServerPhysicalPath = $Script:DSCTools_DefaultComplianceServerPhysicalPath
} # if
try
{
Write-Verbose -Message "Enable-DSCPullServer: HTTP Pull Server MOF $TempPath\$NodeName.MOF for $NodeName Begin Creation"
# Load the CreatePullServer Configuration into memory (dot source it)
# The file should be in Configuration folder beneath the folder the module is in.
. "$(Join-Path -Path $PSScriptRoot -ChildPath 'Configuration\Config_EnablePullServerHTTP.ps1')"
$null = Config_EnablePullServerHTTP `
-NodeName $NodeName `
-Output $TempPath `
-PullServerPort $PullServerPort `
-ComplianceServerPort $ComplianceServerPort `
-CertificateThumbprint $CertificateThumbprint `
-PullServerEndpointName $PullServerEndpointName `
-PullServerResourcePath $PullServerResourcePath `
-PullServerConfigurationPath $PullServerConfigurationPath `
-PullServerPhysicalPath $PullServerPhysicalPath `
-ComplianceServerEndpointName $ComplianceServerEndpointName `
-ComplianceServerPhysicalPath $ComplianceServerPhysicalPath
}
catch
{
throw
} # try
Write-Verbose -Message "Enable-DSCPullServer: HTTP Pull Server MOF $TempPath\$NodeName.MOF for $NodeName Created Successfully"
}
else
{
[System.String] $PullServerConfigurationPath = $Node.PullServerConfigurationPath
if (($PullServerConfigurationPath -eq '') -or ($null -eq $PullServerConfigurationPath))
{
$PullServerConfigurationPath = $Script:DSCTools_DefaultPullServerConfigurationPath
}
[System.String] $PullServerEndpointName = $Node.PullServerEndpointName
if (($PullServerEndpointName -eq '') -or ($null -eq $PullServerEndpointName))
{
$PullServerEndpointName = $Script:DSCTools_DefaultPullServerEndpointName
}
try
{
Write-Verbose -Message "Enable-DSCPullServer: SMB Pull Server MOF $TempPath\$NodeName.MOF for $NodeName Begin Creation"
# Load the CreatePullServer Configuration into memory (dot source it)
# The file should be in Configuration folder beneath the folder the module is in.
. "$(Join-Path -Path $PSScriptRoot -ChildPath 'Configuration\Config_EnablePullServerSMB.ps1')"
$null = Config_EnablePullServerSMB `
-NodeName $NodeName `
-Output $TempPath `
-PullServerEndpointName $PullServerEndpointName `
-PullServerConfigurationPath $PullServerConfigurationPath
}
catch
{
throw
}
Write-Verbose -Message "Enable-DSCPullServer: SMB Pull Server MOF $TempPath\$NodeName.MOF for $NodeName Created Successfully"
}
# Apply the Pull Server MOF File to the Server
if (IsLocalHost($NodeName))
{
# Apply the Pull Server MOF File to the localhost
if ($NodeName -match '\.')
{
Write-Warning "Enable-DSCPullServer: Warning Applying MOF $TempPath\$NodeName.MOF may fail because an FQDN name was used for Pull Server."
}
try
{
Write-Verbose -Message "Enable-DSCPullServer: Applying Pull Server MOF $TempPath\$NodeName.MOF to Localhost"
Start-DSCConfiguration -Path $TempPath -Wait -Force
Write-Verbose -Message "Enable-DSCPullServer: Pull Server MOF $TempPath\$NodeName.MOF Applied to Localhost Successfully"
}
catch
{
Write-Warning "Enable-DSCPullServer: Error Applying Pull Server MOF $TempPath\$NodeName.MOF to Localhost"
throw
} # try
}
else
{
# Apply the LCM MOF File to a remote node
if (($SkipConnectionCheck) -or (Test-Connection -ComputerName $NodeName -Count 1 -Quiet))
{
try
{
Write-Verbose -Message "Enable-DSCPullServer: Applying Pull Server MOF $TempPath\$NodeName.MOF to $NodeName"
if ($Credential)
{
Start-DSCConfiguration -Path $TempPath -ComputerName $NodeName -Credential $Credential -Wait -Force
}
else
{
Start-DSCConfiguration -Path $TempPath -ComputerName $NodeName -Wait -Force
} # If
Write-Verbose -Message "Enable-DSCPullServer: Pull Server MOF $TempPath\$NodeName.MOF Applied to $NodeName Successfully"
}
catch
{
Write-Warning "Enable-DSCPullServer: Error Applying Pull Server MOF $TempPath\$NodeName.MOF to $NodeName"
throw
} # try
}
else
{
Write-Error -Message "Enable-DSCPullServer: Error contacting $NodeName. Push Mode Configuration was not applied."
} # If
} # If
# Reove the LCM MOF File
Remove-Item -Path "$TempPath\$NodeName.MOF" -Force
Write-Verbose -Message "Enable-DSCPullServer: MOF $TempPath\$NodeName.MOF for $NodeName Deleted"
} # foreach
Remove-Item -Path $TempPath -Recurse -Force
Write-Verbose -Message "Enable-DSCPullServer: Temporary Folder $TempPath Deleted"
} # Enable-DSCPullServer
<#
.SYNOPSIS
Enable/Disable DSC pull server logging on one or more DSC Pull Servers.
.DESCRIPTION
.PARAMETER Nodes
Must contain an array of hash tables. Each hash table will represent a pull server node that should be configured with logging.
The hash table must contain the following entries:
Name = Name of the DSC Pull Server
Each hash entry can also contain the following optional items. If each item is not specified it will default.
AnalyticLog = a boolean value used to enable/disable Analytic logging.
DebugLog = a boolean value used to enable/disable Debug logging.
OperationalLog = a boolean value used to enable/disable Operational logging.
For example:
@(@{Name='DSCPULLSRV01';Analytic=$True;Debug=$False;Operational=$True;},@{Name='DSCPULLSRV01';Analytic=$True;Debug=$False;Operational=$True;})
.PARAMETER ComputerName