PowerShell Install Multiple WSPs

The script enumerates a folder structure based off the input section of the PowerShell script. It then detects all .wsp files in any folder at the root of the folder and below. Once the list of files is compiled by PowerShell, it begins to either upgrade or installs each detected wsp. The script has a bit of logic when attempting an install or upgrade. If the PowerShell script detects that the solution is already deployed to the farm, it will first attempt to retract the solution, once completed it then removes the solution before it attempts to install and deploys the feature to all web applications.

 

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
######################################
######## Set Variables ###############
######################################
$InstallDIR = "C:\install"
 
######################################
#### CODE, No Changes Necessary ######
######################################
Write-Host "Working, Please wait...."
Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue
 
$Dir = get-childitem $InstallDIR -Recurse
$WSPList = $Dir | where {$_.Name -like "*.wsp*"}
Foreach ($wsp in $WSPList )
{
	$WSPFullFileName = $wsp.FullName
	$WSPFileName = $wsp.Name
	clear
	Write-Host -ForegroundColor White -BackgroundColor Blue "Working on $WSPFileName" 
 
	try
	{
		Write-Host -ForegroundColor Green "Checking Status of Solution"
		$output = Get-SPSolution -Identity $WSPFileName -ErrorAction Stop
	}
	Catch
	{
		$DoesSolutionExists = $_
	}
	If (($DoesSolutionExists -like "*Cannot find an SPSolution*") -and ($output.Name -notlike  "*$WSPFileName*"))
	{
		Try
		{
			Write-Host -ForegroundColor Green "Adding solution to farm"
			Add-SPSolution "$WSPFullFileName" -Confirm:$false -ErrorAction Stop | Out-Null
 
			Write-Host -ForegroundColor Green "Checking Status of Solution"
			$output = Get-SPSolution -Identity $WSPFileName -ErrorAction Stop
			$gobal = $null
			if ($output.Deployed -eq $false)
			{
				try
				{
					Write-Host -ForegroundColor Green "Deploy solution to all Web Apps, will skip if this solution is globally deployed"
					Install-SPSolution -Identity "$WSPFileName" -GACDeployment -AllWebApplications -Force -Confirm:$false -ErrorAction Stop | Out-Null
				}
				Catch
				{
					$gobal = $_
				}
				If ($gobal -like "*This solution contains*")
				{
					Write-Host -ForegroundColor Green "Solution requires global deployment, Deploying now"
					Install-SPSolution -Identity "$WSPFileName" -GACDeployment -Force -Confirm:$false -ErrorAction Stop | Out-Null
				}
			}
 
			Sleep 1
			$dpjobs = Get-SPTimerJob | Where { $_.Name -like "*$WSPFileName*" }
			If ($dpjobs -eq $null)
    		{
        		Write-Host -ForegroundColor Green "No solution deployment jobs found"
    		}
			Else
			{
				If ($dpjobs -is [Array])
				{
					Foreach ($job in $dpjobs)
					{
						$jobName = $job.Name
						While ((Get-SPTimerJob $jobName -Debug:$false) -ne $null)
						{
							Write-Host -ForegroundColor Yellow -NoNewLine "."
							Start-Sleep -Seconds 5
						}
						Write-Host
					}
				}
    			Else
    			{
					$jobName = $dpjobs.Name
					While ((Get-SPTimerJob $jobName -Debug:$false) -ne $null)
					{
						Write-Host -ForegroundColor Yellow -NoNewLine "."
						Start-Sleep -Seconds 5
					}
					Write-Host
    			}
			}
		}
		Catch
		{
			Write-Error $_
			Write-Host -ForegroundColor Red "Skipping $WSPFileName, Due to an error"
			Read-Host
		}
	}
	Else
	{
		$skip = $null
		$tryagain = $null
		Try
		{
			if ($output.Deployed -eq $true)
			{
			Write-Host -ForegroundColor Green "Retracting Solution"
			Uninstall-SPSolution -AllWebApplications -Identity $WSPFileName -Confirm:$false -ErrorAction Stop
			}
		}
		Catch
		{
			$tryagain = $_
		}
		Try
		{
			if ($tryagain -ne $null)
			{
				Uninstall-SPSolution -Identity $WSPFileName -Confirm:$false -ErrorAction Stop
			}
		}
		Catch
		{
			Write-Host -ForegroundColor Red "Could not Retract Solution"
		}
 
		Sleep 1
		$dpjobs = Get-SPTimerJob | Where { $_.Name -like "*$WSPFileName*" }
		If ($dpjobs -eq $null)
    	{
        	Write-Host -ForegroundColor Green "No solution deployment jobs found"
    	}
		Else
		{
			If ($dpjobs -is [Array])
			{
				Foreach ($job in $dpjobs)
				{
					$jobName = $job.Name
					While ((Get-SPTimerJob $jobName -Debug:$false) -ne $null)
					{
						Write-Host -ForegroundColor Yellow -NoNewLine "."
						Start-Sleep -Seconds 5
					}
					Write-Host
				}
			}
    		Else
    		{
				$jobName = $dpjobs.Name
				While ((Get-SPTimerJob $jobName -Debug:$false) -ne $null)
				{
					Write-Host -ForegroundColor Yellow -NoNewLine "."
					Start-Sleep -Seconds 5
				}
				Write-Host
    		}
		}		
 
		Try
		{
			Write-Host -ForegroundColor Green "Removing Solution from farm"
			Remove-SPSolution -Identity $WSPFileName -Confirm:$false -ErrorAction Stop
		}
		Catch
		{
			$skip = $_
			Write-Host -ForegroundColor Red "Could not Remove Solution"
			Read-Host
		}
		if ($skip -eq $null)
		{
			Try
			{
				Write-Host -ForegroundColor Green "Adding solution to farm"
				Add-SPSolution "$WSPFullFileName" -Confirm:$false -ErrorAction Stop | Out-Null
				$gobal = $null
				try
				{
					Write-Host -ForegroundColor Green "Deploy solution to all Web Apps, will skip if this solution is globally deployed"
					Install-SPSolution -Identity "$WSPFileName" -GACDeployment -AllWebApplications -Force -Confirm:$false -ErrorAction Stop | Out-Null
				}
				Catch
				{
					$gobal = $_
				}
				If ($gobal -like "*This solution contains*")
				{
					Write-Host -ForegroundColor Green "Solution requires global deployment, Deploying now"
					Install-SPSolution -Identity "$WSPFileName" -GACDeployment -Force -Confirm:$false -ErrorAction Stop | Out-Null
				}
			}
			Catch
			{
				Write-Error $_
				Write-Host -ForegroundColor Red "Skipping $WSPFileName, Due to an error"
				Read-Host
			}
 
			Sleep 1
			$dpjobs = Get-SPTimerJob | Where { $_.Name -like "*$WSPFileName*" }
			If ($dpjobs -eq $null)
    		{
        		Write-Host -ForegroundColor Green "No solution deployment jobs found"
    		}
			Else
			{
				If ($dpjobs -is [Array])
				{
					Foreach ($job in $dpjobs)
					{
						$jobName = $job.Name
						While ((Get-SPTimerJob $jobName -Debug:$false) -ne $null)
						{
							Write-Host -ForegroundColor Yellow -NoNewLine "."
							Start-Sleep -Seconds 5
						}
						Write-Host
					}
				}
    			Else
    			{
					$jobName = $dpjobs.Name
					While ((Get-SPTimerJob $jobName -Debug:$false) -ne $null)
					{
						Write-Host -ForegroundColor Yellow -NoNewLine "."
						Start-Sleep -Seconds 5
					}
					Write-Host
    			}
			}
	}
	Else
	{
		Write-Host -ForegroundColor Red "Cannot Install $WSPFileName, Please try manually"
		Read-Host
	}
}
}

2 Comments

  1. Medi says

    This one worked..
    Thanks,
    Ahmed Gadir

    Reply
  2. hemanth kumar hankarjal says

    works like a rocket ….just one change – physical path of my wsp’s ….beautiful script…Thanks a lot for sharing….

    Reply

Leave a Reply

Leave a Reply
  • (required)
  • (required) (will not be published)

  • Tags

  • Subscribe
  • Pages

  • More

  • Disclaimer…

    This is my personal weblog. The opinions expressed herein are my own and are not representative of any 3rd party influence. The owner of this blog reserves the right to edit or delete any comments submitted to this blog without notice if they are deemed to be spam, offensive or otherwise inappropriate. The owner of this blog makes no representations as to the accuracy or completeness of any information on this site or found by following any link on this site. The owner will not be liable for any errors or omissions in this information nor for the availability of this information. The owner will not be liable for any losses, injuries, or damages from the display or use of this information.


    Lastly, I do my best to document my sources if the article is not of my own creation. If I have missed or forgotten to source your work. I would love feedback via the comments section. Thank you.