TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Deploy Multiple WSPs at Farm or Web Application Level
Krishna Murthy
Jul 16
2015
Code
1.5
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
PowerShellInstallMul
#region OPTIONS
# CHECKS THE POWERSHELL VERSION
# ReuseThread: Creates a new thread for the first invocation and then re-uses that thread in
# subsequent invocations. This field is introduced in Windows PowerShell 2.0.
$ver = $host | select version
if
($ver.Version.Major -gt 1) {$host.Runspace.ThreadOptions =
"ReuseThread"
}
# CHECKS FOR THE SHAREPOINT POWERSHELL SNAPIN, IF ITS NOT PRESENT IT LOADS IT
if
((Get-PSSnapin
"Microsoft.SharePoint.PowerShell"
-ErrorAction SilentlyContinue) -eq $
null
)
{
Add-PSSnapin
"Microsoft.SharePoint.PowerShell"
-ErrorVariable SnapinErr -ErrorAction SilentlyContinue
if
($SnapinErr)
{
Write-Host
"`r`n Error: Cannot install Microsoft.SharePoint.PowerShell Snapin. $SnapinErr"
-ForegroundColor Red
Write-Host
"`r`n It is now safe to exit the console or press any button to exit..."
[System.Console]::Read()
exit
}
}
# CHANGE THE TITLE OF THE CONSOLE
$a = (Get-Host).UI.RawUI
$a.WindowTitle =
"Running: PowerShellInstallMultipleWSPs.ps1"
#endregion
#region VARIABLES
$InstallDIR =
"C:\WSPs"
#WSPs directory
$InstallScope =
"LOCAL"
#
"GLOBAL"
- Farm or
"LOCAL"
- Web application Level
$WebAppName =
"http://timesdash_dev.timesgroup.com/"
#endregion
#region CODE
Write-Host
"Working, Please wait...."
$Dir =
get
-childitem $InstallDIR -Recurse
$WSPList = $Dir | where {$_.Name -like
"*.wsp*"
}
Foreach ($wsp
in
$WSPList )
{
$WSPFullFileName = $wsp.FullName
$WSPFileName = $wsp.Name
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"
if
($InstallScope -eq
"GLOBAL"
)
{
Install-SPSolution -Identity
"$WSPFileName"
-GACDeployment -AllWebApplications -Force -Confirm:$
false
-ErrorAction Stop | Out-Null
}
else
{
Install-SPSolution -Identity
"$WSPFileName"
-GACDeployment -WebApplication
"$WebAppName"
-Force -Confirm:$
false
-ErrorAction Stop | Out-Null
}
}
Catch
{
$gobal = $_
}
}
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"
if
($InstallScope -eq
"GLOBAL"
)
{
Install-SPSolution -Identity
"$WSPFileName"
-GACDeployment -AllWebApplications -Force -Confirm:$
false
-ErrorAction Stop | Out-Null
}
else
{
Install-SPSolution -Identity
"$WSPFileName"
-GACDeployment -WebApplication
"$WebAppName"
-Force -Confirm:$
false
-ErrorAction Stop | Out-Null
}
}
Catch
{
$gobal = $_
}
}
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
}
}
}
#endregion
# CHANGE THE TITLE OF THE CONSOLE
$a = (Get-Host).UI.RawUI
$a.WindowTitle =
"Completed: PowerShellInstallMultipleWSPs.ps1"
#Write-Host "It is now safe to exit the console or press any button to continue..."
#[System.Console]::Read()
SharePoint
WSP
Web Application