-
Notifications
You must be signed in to change notification settings - Fork 0
/
Deploy-SPSolutions.ps1
226 lines (200 loc) · 9.94 KB
/
Deploy-SPSolutions.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
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
# Requires:
# Log.ps1
# Invoke-Actions.ps1
$snapin = {
if ((Get-PSSnapin | ? { $_.Name -eq "Microsoft.SharePoint.PowerShell" }) -eq $null) {
Add-PSSnapin Microsoft.SharePoint.PowerShell
}
}
&$snapin
function Block-SPDeployment($solution, [bool]$deploying, [string]$status, [int]$percentComplete) {
do {
Start-Sleep 2
Write-Progress -Activity "Deploying solution $($solution.Name)" -Status $status -PercentComplete $percentComplete
$solution = Get-SPSolution $solution
if ($solution.LastOperationResult -like "*Failed*") { throw "An error occurred during the solution retraction, deployment, or update." }
if (!$solution.JobExists -and (($deploying -and $solution.Deployed) -or (!$deploying -and !$solution.Deployed))) { break }
} while ($true)
sleep 5
}
function global:Deploy-SPSolutions() {
<#
.Synopsis
Deploys one or more Farm Solution Packages to the Farm.
.Description
Specify either a directory containing WSP files, a single WSP file, or an XML configuration file containing the WSP files to deploy.
If using an XML configuration file, the format of the file must match the following:
<Solutions>
<Solution Path="<full path and filename to WSP>" UpgradeExisting="false">
<WebApplications>
<WebApplication>http://example.com/</WebApplication>
</WebApplications>
</Solution>
</Solutions>
Multiple <Solution> and <WebApplication> nodes can be added. The UpgradeExisting attribute is optional and should be specified if the WSP should be udpated and not retracted and redeployed.
.Example
PS C:\> . .\Deploy-SPSolutions.ps1
PS C:\> Deploy-SPSolutions -Identity C:\WSPs -WebApplication http://demo
This example loads the function into memory and then deploys all the WSP files in the specified directory to the http://demo Web Application (if applicable).
.Example
PS C:\> . .\Deploy-SPSolutions.ps1
PS C:\> Deploy-SPSolutions -Identity C:\WSPs -WebApplication http://demo,http://mysites
This example loads the function into memory and then deploys all the WSP files in the specified directory to the http://demo and http://mysites Web Applications (if applicable).
.Example
PS C:\> . .\Deploy-SPSolutions.ps1
PS C:\> Deploy-SPSolutions -Identity C:\WSPs -AllWebApplications
This example loads the function into memory and then deploys all the WSP files in the specified directory to all Web Applications (if applicable).
.Example
PS C:\> . .\Deploy-SPSolutions.ps1
PS C:\> Deploy-SPSolutions -Identity C:\WSPs\MyCustomSolution.wsp -AllWebApplications
This example loads the function into memory and then deploys the specified WSP to all Web Applications (if applicable).
.Example
PS C:\> . .\Deploy-SPSolutions.ps1
PS C:\> Deploy-SPSolutions -Identity C:\WSPs\MyCustomSolution.wsp -AllWebApplications -UpgradeExisting
This example loads the function into memory and then deploys the specified WSP to all Web Applications (if applicable); existing deployments will be upgraded and not retracted and redeployed.
.Example
PS C:\> . .\Deploy-SPSolutions.ps1
PS C:\> Deploy-SPSolutions C:\Solutions.xml
This example loads the function into memory and then deploys all the WSP files specified by the Solutions.xml configuration file.
.Parameter Config
The XML configuration file containing the WSP files to deploy.
.Parameter Identity
The directory, WSP file, or XML configuration file containing the WSP files to deploy.
.Parameter UpgradeExisting
If specified, the WSP file(s) will be updated and not retracted and redeployed (if the WSP does not exist in the Farm then this parameter has no effect).
.Parameter AllWebApplications
If specified, the WSP file(s) will be deployed to all Web Applications in the Farm (if applicable).
.Parameter WebApplication
Specifies the Web Application(s) to deploy the WSP file to.
.Link
Get-Content
Get-SPSolution
Add-SPSolution
Install-SPSolution
Update-SPSolution
Uninstall-SPSolution
Remove-SPSolution
#>
[CmdletBinding(DefaultParameterSetName="FileOrDirectory")]
param (
[Parameter(Mandatory=$true, Position=0, ParameterSetName="Xml")]
[ValidateNotNullOrEmpty()]
[xml]$Config,
[Parameter(Mandatory=$true, Position=0, ParameterSetName="FileOrDirectory")]
[ValidateNotNullOrEmpty()]
[string]$Identity,
[Parameter(Mandatory=$false, Position=1, ParameterSetName="FileOrDirectory")]
[switch]$UpgradeExisting,
[Parameter(Mandatory=$false, Position=2, ParameterSetName="FileOrDirectory")]
[switch]$AllWebApplications,
[Parameter(Mandatory=$false, Position=3, ParameterSetName="FileOrDirectory")]
[Microsoft.SharePoint.PowerShell.SPWebApplicationPipeBind[]]$WebApplication,
[Parameter(Mandatory=$false)]
[string]$Log,
[Parameter(Mandatory=$false)]
[switch]$Force
)
switch ($PsCmdlet.ParameterSetName) {
"Xml" {
# An XML document was provided so (3 steps):
# 1- Interate through all tasks (nodes) under BeforeDeploy (only Features and Provision are supported)
if ($Config.Deployment.BeforeDeploy -ne $null) {
Invoke-Actions $Config.Deployment.BeforeDeploy "Predeployment"
}
# 2- Iterate through all the defined solutions and call the other parameter set version of the function.
if ($Config.Deployment.Deploy.Solutions -ne $null) {
Log "Information" "Deployment..."
$Config.Deployment.Deploy.Solutions.Solution |
ForEach-Object {
[string]$path = $_.Path
[bool]$upgrade = $false
if (![string]::IsNullOrEmpty($_.UpgradeExisting)) {
$upgrade = [bool]::Parse($_.UpgradeExisting)
}
$webApps = $_.WebApplications.WebApplication
$force = [boolean]$_.Force
Deploy-SPSolutions -Identity $path -UpgradeExisting:$upgrade -WebApplication $webApps -AllWebApplications:$(($webApps -eq $null) -or ($webApps.Length -eq 0)) -Force:$force
}
Log "Information" "Deployment done."
}
# 3- Iterate through all tasks (nodes) under AfterDeploy (only Features, IIS and TimerJobs are supported)
if ($Config.Deployment.AfterDeploy -ne $null) {
Invoke-Actions $Config.Deployment.AfterDeploy "Postdeployment"
}
break
}
"FileOrDirectory" {
$item = Get-Item (Resolve-Path $Identity)
if ($item -is [System.IO.DirectoryInfo]) {
# A directory was provided so iterate through all files in the directory and deploy if the file is a WSP (based on the extension)
Get-ChildItem $item | ForEach-Object {
if ($_.Name.ToLower().EndsWith(".wsp")) {
Deploy-SPSolutions -Identity $_.FullName -UpgradeExisting:$UpgradeExisting -WebApplication $WebApplication
}
}
}
elseif ($item -is [System.IO.FileInfo]) {
# A specific file was provided so assume that the file is a WSP if it does not have an XML extension.
[string]$name = $item.Name
if ($name.ToLower().EndsWith(".xml")) {
Deploy-SPSolutions -Config ([xml](Get-Content $item.FullName)) -Log $Log
return
}
$solution = Get-SPSolution $name -ErrorAction SilentlyContinue
if ($solution -ne $null -and $UpgradeExisting) {
# Just update the solution, don't retract and redeploy.
Write-Progress -Activity "Deploying solution $name" -Status "Updating $name" -PercentComplete -1
$solution | Update-SPSolution -CASPolicies:$($solution.ContainsCasPolicy) `
-GACDeployment:$($solution.ContainsGlobalAssembly) `
-LiteralPath $item.FullName
Block-SPDeployment $solution $true "Updating $name" -1
Write-Progress -Activity "Deploying solution $name" -Status "Updated" -Completed
return
}
if ($solution -ne $null) {
#Retract the solution
if ($solution.Deployed) {
Write-Progress -Activity "Deploying solution $name" -Status "Retracting $name" -PercentComplete 0
if ($solution.ContainsWebApplicationResource) {
$solution | Uninstall-SPSolution -AllWebApplications -Confirm:$false
} else {
$solution | Uninstall-SPSolution -Confirm:$false
}
#Block until we're sure the solution is no longer deployed.
Block-SPDeployment $solution $false "Retracting $name" 12
Write-Progress -Activity "Deploying solution $name" -Status "Solution retracted" -PercentComplete 25
}
#Delete the solution
Write-Progress -Activity "Deploying solution $name" -Status "Removing $name" -PercentComplete 30
Get-SPSolution $name | Remove-SPSolution -Confirm:$false
Write-Progress -Activity "Deploying solution $name" -Status "Solution removed" -PercentComplete 50
}
#Add the solution
Write-Progress -Activity "Deploying solution $name" -Status "Adding $name" -PercentComplete 50
$solution = Add-SPSolution $item.FullName
Write-Progress -Activity "Deploying solution $name" -Status "Solution added" -PercentComplete 75
#Deploy the solution
if (!$solution.ContainsWebApplicationResource) {
Write-Progress -Activity "Deploying solution $name" -Status "Installing $name" -PercentComplete 75
$solution | Install-SPSolution -GACDeployment:$($solution.ContainsGlobalAssembly) -CASPolicies:$($solution.ContainsCasPolicy) -Confirm:$false
Block-SPDeployment $solution $true "Installing $name" 85
} else {
if ($WebApplication -eq $null -or $WebApplication.Length -eq 0) {
Write-Progress -Activity "Deploying solution $name" -Status "Installing $name to all Web Applications" -PercentComplete 75
$solution | Install-SPSolution -GACDeployment:$($solution.ContainsGlobalAssembly) -CASPolicies:$($solution.ContainsCasPolicy) -AllWebApplications -Confirm:$false -Force:$Force
Block-SPDeployment $solution $true "Installing $name to all Web Applications" 85
} else {
$WebApplication | ForEach-Object {
$webApp = $_.Read()
Write-Progress -Activity "Deploying solution $name" -Status "Installing $name to $($webApp.Url)" -PercentComplete 75
$solution | Install-SPSolution -GACDeployment:$gac -CASPolicies:$cas -WebApplication $webApp -Confirm:$false -Force:$Force
Block-SPDeployment $solution $true "Installing $name to $($webApp.Url)" 85
}
}
}
Write-Progress -Activity "Deploying solution $name" -Status "Deployed" -Completed
}
break
}
}
}