SharePoint 2010: PowerShell to Wait for WSP Solution Deployment Timer Job to Complete
Following on from my post “SharePoint 2010: PowerShell to wait for Web.Config Modifications One-Time Timer Job to Complete“, here is another useful PowerShell script that waits for a WSP Solution install or uninstall to complete, as that too runs asynchronously.
PowerShell scripts cannot simply deploy a WSP solution then immediately afterwards activate a feature deployed by it, as the chances are that the WSP solution will not have completed installing so the feature will be unavailable. I have seen many scripts where people simply add a sleep for a few seconds and hope that will be enough. On a multi-server farm environment, WSP solution deployments take much longer than on single server or stand-along environments, as the deployments have to execute on each front-end web server to drop files into the 14 hive of each server.
To have a reliable deployment, especially when deploying lots of solutions, the script really should wait until the timer job for each WSP solution is complete, whether that be for install or uninstall, before proceeding with the rest of the script.
To use this PowerShell function, simply call it with the filename (without path) of the WSP solution file. It will output progress as it waits for it to complete as it can take a while depending on the complexity of the solution, and will return true or false to indicate whether the deployment was successful.
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
}
My LinkedIn Profile
Thanks for sharing, it’s a useful method
Thanks a lot …Its a very useful method..
Thanks, just what I was looking for and didn’t have to do it myself.
Thanks a lot.