PowerShell Script to deploy WSP Solution in SharePoint 2013

PowerShell Script to deploy WSP Solution in SharePoint 2013

Requirement:
From SharePoint 2010 onwards Microsoft has improved the ability of PowerShell scripting more and more to make SharePoint admin life easy and smooth. In this article I would like to present a one single PowerShell script to perform:

-> Deactivate feature(s) in SharePoint 2013
-> Retract the solution from a specific web application(s) in SharePoint 2013
-> Deploy solution to a specific web application(s) in SharePoint 2013
-> Activate feature(s) in SharePoint 2013

 

Usually we provide couple of PowerShell commands to admin for deploying a SharePoint solution but my requirement is to deploy solution trough scheduler. To accomplish that task I have created the below PowerShell script.

Solution:
In this script I am using sleep, do while looping to ensure that SharePoint perform the required operation.



try {
$solution = “MStechnologySolution.wsp”
$slnFlag = Get-SPSolution -Identity $solution
if ($slnFlag -ne $null) {
Write-Host “`nDisabling MTGHubFinance feature…”
do {
$ftr = Disable-SPFeature –Identity “c41791bf-c5c1-430c-8fb1-2584799d6091” –url http://intranet.mstechnology.com/ –Confirm:$false
Start-Sleep -Seconds 10
}
while ($ftr -eq $null)
Write-Host “`nMTGHubFinance feature Disabled Sucessfully”
$slnStat = Uninstall-SPSolution -Identity $solution -WebApplication http://intranet.mstechnology.com/ –Confirm:$false
if ($slnStat.Deployed -eq $null) {
Write-Host “`nUnInstalling $solution solution…”
$flgWSPSln = $false
do {
$wspID = Get-SPSolution -Identity $solution
$tmJob = Get-SPSolution -Identity $solution
if ($wspID.Deployed -eq $null) {
$flgWSPSln = $false
}
else {
if (!($wspID.Deployed -eq $False -And $tmJob.JobExists -eq – $False)) {
$flgWSPSln = $true
Start-Sleep -Seconds 10
}
else {
$flgWSPSln = $false
}
}
}
while ($flgWSPSln -eq $true)
Write-Host “`n$solution solution Uninstalled Sucessfully…”
Remove-SPSolution -Identity $solution –Confirm:$false
Start-Sleep -Seconds 2
Write-Host “`n$solution solution Removed Sucessfully…”
}
}
$slnFlag = Get-SPSolution -Identity $solution
if ($slnFlag -eq $null) {
Add-SPSolution “D:\MStechnologySolution.wsp”
Start-Sleep -Seconds 2
Write-Host “`n$solution Added Sucessfully…”
Install-SPSolution -Identity $solution -WebApplication http://intranet.mstechnology.com/ -GACDeployment
Write-Host “`nInstalling $solution …”
$flgWSPSln = $false
do {
$wspID = Get-SPSolution -Identity $solution
$tmJob = Get-SPSolution -Identity $solution
if ($wspID.Deployed -eq $True -And $tmJob.JobExists -eq – $False) {
$flgWSPSln = $true
}
else {
Start-Sleep -Seconds 10
}
}
while ($flgWSPSln -eq $false)
Write-Host “`nInstalled $solution solution Sucessfully…”
Write-Host “`nMTGHubFinance feature Enabling….”
Enable-SPFeature –Identity “c41791bf-c5c1-430c-8fb1-2584799d6091” –url http://intranet.mstechnology.com/ –PassThru
Write-Host “`nMTGHubFinance feature Enabled Sucessfully”
}
}
catch {
Write-Host “`nError:: $($_.Exception.Message)” -foregroundcolor red -BackgroundColor Yellow
}

Read SharePoint 2013 tutorials

Let us walk through the script:



1. If you see, the entire code has been wrapped inside try catch block and we are also writing the error in output windows in case of any with the below.
Write-Host “`nError:: $($_.Exception.Message)” -foregroundcolor red -BackgroundColor Yellow

2. With the below code block, I am deactivating the feature with the ID” c41791bf-c5c1-430c-8fb1-2584799d6091” in do while loop. If the feature is not deactivated I am forcing the PowerShell to sleep for 10 sec
do {
$ftr = Disable-SPFeature –Identity “c41791bf-c5c1-430c-8fb1-2584799d6091” –url http://intranet.mstechnology.com/ –Confirm:$false
Start-Sleep -Seconds 10
}
while ($ftr -eq $null)

3. As soon as the feature is deactivated I am first checking is the solution existed in the FARM or not
$slnFlag = Get-SPSolution -Identity $solution

4. Once the feature is deactivated, I am retracting the solution in a do while loop also checking the status of the timer job to ensure the solution retracted successfully. If the solution is not retracted I am placing the script on sleep.
$wspID = Get-SPSolution -Identity $solution
$tmJob = Get-SPSolution -Identity $solution
if ($wspID.Deployed -eq $True -And $tmJob.JobExists -eq – $False) {
$flgWSPSln = $true
}
else {
Start-Sleep -Seconds 10
}

5. Now that I have completed the solution retract I am adding the solution and activating the required features by using timer jobs and do while loops.



Hope this helps, happy administration in SharePoint !!!

Check out Best Alternative to InfoPath -> Try Now

You May Also like the Following SharePoint Online Tutorials:

About Krishna Vandanapu

I am Krishna.Vandanapu a SharePoint architect working in IT from last 12 years, I worked in SharePoint 2007, 2010, 2013, 2016 and Office 365. I have extensive hands on experience in customizing SharePoint sites from end to end. Expertise in SharePoint migration tools like Sharegate, Doc Ave and Metalogix. Migrated SharePoint sites from SharePoint 2007 to 2010 and 2010 to 2013 several times seamlessly. Implementing CSOM with Microsoft best practices. Spent quality time in configuring SharePoint application services like User Profile, Search, Managed Meta data services etc. Now exploring SharePoint Framework and SharePoint 2019

View all posts by Krishna Vandanapu →