Powershell – Script to Deploy Solution on Sharepoint

Many times we need create a script to deploy solutions on SharePoint via Powershell.

So I would like to share my script.

You just need change the Solution Name and Web Application Url.

How to use it:

Save the script in a file with the extension .ps1 > Starts Windows Shell with Administration and run it.

Copy the script below or make download: http://1drv.ms/1HRNeHR

function WaitForJobToFinish([string]$SolutionFileName)
{ 
    $JobName = "*solution-deployment*$SolutionFileName*"
    $job = Get-SPTimerJob | ?{ $_.Name -like $JobName }
    if ($job -eq $null) 
    {
        Write-Host 'Timer job not found'
    }
    else
    {
        $JobFullName = $job.Name
        Write-Host -NoNewLine "Waiting to finish job $JobFullName"
        
        while ((Get-SPTimerJob $JobFullName) -ne $null) 
        {
            Write-Host -NoNewLine .
            Start-Sleep -Seconds 2
        }
        Write-Host "Finished waiting for job.."
    }
}

Add-PsSnapin Microsoft.SharePoint.PowerShell


#-------------------------VARIABLES START-----------------
#default Variables - Always needed
$CurrentDir= split-path -parent $MyInvocation.MyCommand.Definition
$solutionName="YOURSOLUTION.WSP"
$SolutionPath=$CurrentDir + "\"+$solutionName

#variable for dedicated WebApp solutions
$WebAppUrl="HTTP://WEBAPPLICATION:port"

#Only used case features need some Activation/Deactivation
$FeatureName="Feature"
#---------------------VARIABLES END-----------------

#---------------------Deploy Script START-----------------
#Write-Host 'Going to disable feature'
#disable-spfeature -identity $FeatureName -confirm:$false -url $WebAppUrl

#Write-Host 'Going to uninstall feature'
 #uninstall-spfeature -identity $FeatureName -confirm:$false -force
Write-Host 'Going to uninstall solution'
#Comment one of the next 2 script lines
 #1 - For global WSP
 #Uninstall-SPSolution -identity $solutionName -confirm:$false
#2 - For WebApp solutions, I.E. Webparts
Uninstall-SPSolution -identity $solutionName -confirm:$false -WebApplication $WebAppUrl

Write-Host 'Waiting for job to finish'
WaitForJobToFinish

Write-Host 'Going to remove solution'
Remove-SPSolution –identity $solutionName -confirm:$false

Write-Host 'Going to add solution'
Add-SPSolution $SolutionPath

Write-Host 'Going to install solution to all web applications'
#Comment one of the next 2 script lines
 #1 - For global WSP
 #Install-SPSolution –identity $solutionName –GACDeployment -force
 #2 - For WebApp solutions, I.E. Webparts
Install-SPSolution –identity $solutionName -WebApplication $WebAppUrl –GACDeployment -force

Write-Host 'Waiting for job to finish'
WaitForJobToFinish

#Write-Host 'Going to enable Feature'
#Enable-spfeature -identity $FeatureName -confirm:$false -url $WebAppUrl

Remove-PsSnapin Microsoft.SharePoint.PowerShell
Advertisements

Posted on 27/03/2015, in PowerShell and tagged , , , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: