Wednesday, March 7, 2012

How to deploy to sharepoint using powershell

In my first blog post I will have closer look at how to deploy .wsp to Sharepoint 2010 using a powershell script. My inspiration has been these gentlemen
  • http://sharepintblog.com/2011/06/04/exporting-solutions-packages-wsp-with-powershell/
  • http://www.projectserver2010blog.com/2010/01/sharepoint-2010-solution-deployment.html
  • https://nickhobbs.wordpress.com/2012/02/24/sharepoint-2010-powershell-to-wait-for-wsp-solution-deployment-timer-job-to-complete/

So first up I have an setup file with that includes the properties that is specific for a solution and the feature that it contains. Which the same as i sharepintblog


$DeploymentPackageFolder="C:\slv\Main\Samhandling\SLV.Samh\SLV.Samh.KReg.SL.SP.DataConnLib\bin\Debug"

$SiteUrl="http://localhost/sites/kunderegisteret/config"
$SolutionFolder="./"
$SolutionName="DataConnLib.wsp" #constant
$FeatureName="DataConnLibFeature" #constant

cd $DeploymentPackageFolder

PowerShell -file C:\your\path\deploywithfeatureactivation.ps1 $SiteUrl $SolutionFolder $SolutionName $FeatureName

cd C C:\your\path\InstallScripts\

Then Ive added the wait function to the deploy script as I didnt think the simple while loop seemed to do the trick

function WaitForSPSolutionJobToComplete([string]$solutionName)
{
$solution = Get-SPSolution -Identity $solutionName -ErrorAction SilentlyContinue

if ($solution)
{
if ($solution.JobExists)
{
Write-Host -NoNewLine "Waiting for timer job to complete for solution '$solutionName'."
}

# Check if there is a timer job still associated with this solution and wait until it has finished
while ($solution.JobExists)
{
$jobStatus = $solution.JobStatus

# If the timer job succeeded then proceed
if ($jobStatus -eq [Microsoft.SharePoint.Administration.SPRunningJobStatus]::Succeeded)
{
Write-Host "Solution '$solutionName' timer job suceeded"
return $true
}

# If the timer job failed or was aborted then fail
if ($jobStatus -eq [Microsoft.SharePoint.Administration.SPRunningJobStatus]::Aborted -or
$jobStatus -eq [Microsoft.SharePoint.Administration.SPRunningJobStatus]::Failed)
{
Write-Host "Solution '$solutionName' has timer job status '$jobStatus'."
return $false
}

# Otherwise wait for the timer job to finish
Write-Host -NoNewLine "."
Sleep 1
}

# Write a new line to the end of the '.....'
Write-Host
}

return $true
}

$SiteUrl = $args[0]
$SolutionFolder = $args[1]
$SolutionName = $args[2]
$FeatureName = $args[3]

$AdminServiceName = "SPAdminV4"
$IsAdminServiceWasRunning = $true;

Add-PSSnapin microsoft.sharepoint.powershell

if ($(Get-Service $AdminServiceName).Status -eq "Stopped")
{
$IsAdminServiceWasRunning = $false;
Start-Service $AdminServiceName
}

Write-Host "Analyzing feature: $FeatureName"
if ((Get-SPFeature | ? {($_.Name -eq $FeatureName)}) -ne $null)
{
Write-Host "Deactivating feature: $FeatureName" -NoNewline

# Synchronous call
Disable-SPFeature $FeatureName -Url $SiteUrl -Confirm:$false

Write-Host " - Done."
}

$Solution = Get-SPSolution | ? {($_.Name -eq $SolutionName) -and ($_.Deployed -eq $true)}
if ($Solution -ne $null)
{
Write-Host "Really rectracting solution: $SolutionName"
Uninstall-SPSolution $SolutionName -Confirm:$false

WaitForSPSolutionJobToComplete($SolutionName)

Write-Host " - Done."

Write-Host "Removing solution: $SolutionName"

# Sychrouous call - no need to wait for it
Remove-SPSolution $SolutionName -Confirm:$false

Write-Host " - Done."
}


Write-Host "Adding solution: $SolutionName" -NoNewline
$SolutionPath = $SolutionFolder + $SolutionName
Add-SPSolution $SolutionPath # | Out-Null
Write-Host " - Done."

#Paranoia, just for fun
WaitForSPSolutionJobToComplete($SolutionName)

Write-Host "Deploying solution: $SolutionName"
$Solution = Get-SPSolution | ? {($_.Name -eq $SolutionName) -and ($_.Deployed -eq $false)}
Install-SPSolution $SolutionName -GACDeployment -Confirm:$false

WaitForSPSolutionJobToComplete($SolutionName)
Write-Host " - Done."

# Could have been enabled by the install. Must check. Darn.
if ( $FeatureName -ne $null )
{
Write-Host "Activate feature: $FeatureName" -NoNewline
Enable-SPFeature $FeatureName -Url $SiteUrl -force
Write-Host " - Done."
}

WaitForSPSolutionJobToComplete($SolutionName)


if (-not $IsAdminServiceWasRunning)
{
Stop-Service $AdminServiceName
}


1 comment:

  1. This is just awesome! I've searched the interwebz forever after this. Thank u!

    ReplyDelete