-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPublishModulesAndMofsToPullServer.psm1
146 lines (128 loc) · 6.11 KB
/
PublishModulesAndMofsToPullServer.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
<#
.Synopsis
Package DSC modules and mof configuration document and publish them on enterprise DSC pull server in the required format
.DESCRIPTION
Uses Publish-DSCModulesAndMofs cmdlet to package DSC modules into zip files with the version info. If
Publishes the zip modules on "$env:ProgramFiles\WindowsPowerShell\DscService\Modules"
Publishes all mof configuration documents that present in $Source folder on "$env:ProgramFiles\WindowsPowerShell\DscService\Configuration"
Use $Force to overwrite the version of the module that exists in powershell module path with the version from $source folder
Use $ModuleNameList to specify the names of the modules to be published if the modules do not exist in $Source folder
.EXAMPLE
$moduleList = @("xWebAdministration", "xPhp")
Publish-DSCModulesAndMofs -Source C:\LocalDepot -ModuleNameList $moduleList
.EXAMPLE
Publish-DSCModulesAndMofs -Source C:\LocalDepot -Force
#>
# Tools to use to package DSC modules and mof configuration document and publish them on enterprise DSC pull server in the required format
function Publish-DSCModulesAndMofs
{
param(
[Parameter(Mandatory=$True)]
[string]$Source = $pwd, # The folder that contains the configuration mof documents and modules to be published on pull server. Everything in this folder will be packaged and published.
[switch]$Force, #switch to overwrite the module in PSModulePath with the version provided in $Sources
[string[]]$ModuleNameList # Package and publish the modules listed in $ModuleNameList based on powershell module path content
)
#Create a working directory
$tempFolder = "$pwd\temp"
New-Item -Path $tempFolder -ItemType Directory -Force -ErrorAction SilentlyContinue
#Copy the mof documents from the $Source to working dir
Copy-Item -Path "$Source\*.mof" -Destination $tempFolder -Force -Verbose
#Start Deployment!
Write-Host "Start deployment"
CreateZipFromPSModulePath -listModuleNames $ModuleNameList -destination $tempFolder
CreateZipFromSource -source $Source -destination $tempFolder
# Generate the checkSum file for all the zip and mof files.
New-DSCCheckSum $tempFolder -Force
# Publish mof and modules to pull server repositories
PublishModulesAndChecksum -source $tempFolder
PublishMofDocuments -source $tempFolder
#Deployment is complete!
Remove-Item -Path $tempFolder -Recurse -Force -ErrorAction SilentlyContinue
Write-Host "End deployment"
}
#Package the modules using powershell module path
function CreateZipFromPSModulePath
{
param($listModuleNames, $destination)
# Move all required modules from powershell module path to a temp folder and package them
if(($listModuleNames -eq $null) -or ($listModuleNames.Count -eq 0))
{
Write-Host "No additional modules are specified to be packaged."
}
foreach ($module in $listModuleNames)
{
$allVersions = Get-Module -Name $module -ListAvailable -Verbose
#package all versions of the module
foreach($moduleVersion in $allVersions)
{
$name = $moduleVersion.Name
$source = "$destination\$name"
#Create package zip
$path = $moduleVersion.ModuleBase
Compress-Archive -Path "$path\*" -DestinationPath "$source.zip" -Verbose -Force
$version = $moduleVersion.Version.ToString()
$newName = "$destination\$name" + "_" + "$version" + ".zip"
# Rename the module folder to contain the version info.
if(Test-Path($newName))
{
Remove-Item $newName -Recurse -Force
}
Rename-Item -Path "$source.zip" -NewName $newName -Force
}
}
}
#Function to package modules using a given folder after installing to ps module path.
function CreateZipFromSource
{
param($source, $destination)
# for each module under $Source folder create a zip package that has the same name as the folder.
$allModulesInSource = Get-ChildItem $source -Directory
$modules = @()
foreach ($item in $allModulesInSource)
{
$name = $item.Name
$alreadyExists = Get-Module -Name $name -ListAvailable -Verbose
if(($alreadyExists -eq $null) -or ($Force))
{
#install the modules into powershell module path and overwrite the content
Copy-Item $item.FullName -Recurse -Force -Destination "$env:ProgramFiles\WindowsPowerShell\Modules" -Verbose
}
else
{
Write-Host "Skipping module overwrite. Module with the name $name already exists. Please specify -Force to overwrite the module with the local version of the module located in $source or list names of the modules in ModuleNameList parameter to be packaged from powershell module pat instead and remove them from $source folder" -Fore Red
}
$modules+= @("$name")
}
#Package the module in $destination
CreateZipFromPSModulePath -listModuleNames $modules -destination $destination
}
# Deploy modules to the pullsever repository.
function PublishModulesAndChecksum
{
param($source)
# Check if the current machine is a server sku.
$moduleRepository = "$env:ProgramFiles\WindowsPowerShell\DscService\Modules"
if( (Get-Module ServerManager -ListAvailable) -and (Test-Path ($moduleRepository)))
{
Copy "$source\*.zip*" $moduleRepository -Force -Verbose
}
else
{
Write-Host "Copying modules to pullserver module repository skipped because the machine is not a server sku or Pull server endpoint is not deployed." -Fore Yellow
}
}
# function deploy configuratoin and thier checksum.
function PublishMofDocuments
{
param($source)
# Check if the current machine is a server sku.
$mofRepository = "$env:ProgramFiles\WindowsPowerShell\DscService\Configuration"
if( (Get-Module ServerManager -ListAvailable) -and (Test-Path ($mofRepository)) )
{
Copy-Item "$source\*.mof*" $mofRepository -Force -Verbose
}
else
{
Write-Host "Copying configuration(s) to pullserver configuration repository skipped because the machine is not a server sku or Pull server endpoint is not deployed." -Fore Yellow
}
}