Skip to content

Instantly share code, notes, and snippets.

Created July 8, 2014 10:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/e630bb5ecc56f3462819 to your computer and use it in GitHub Desktop.
Save anonymous/e630bb5ecc56f3462819 to your computer and use it in GitHub Desktop.
###################################################################################################
# This script deploys a feature to a SharePoint 2010 farm. #
###################################################################################################
Add-PSSnapin "Microsoft.SharePoint.PowerShell" -ea "SilentlyContinue"
$packageName = "XXX.wsp" # The name of the package to deploy
$targetDirectory = "C:\XXX" # The directory that contains the package to deploy
$siteUrl = "XXX" # The url of the site/site collection the feature is deployed to
$webApp = "XXX" # The name of the application or the GUID of the application
$featureName = "XXX" # The name of the feature
###################################################################################################
# Waits for the solution deployment timer job to finish. #
###################################################################################################
Function Wait-TimerJob
{
[CmdletBinding()]
param ()
$job = Get-SPTimerJob | ?{ $_.Name -like "*solution-deployment*" }
if ($job -eq $null)
{
Write-Host "Solution deployment job not running."
}
else
{
# If it's taking too long, throw an error so problem can be investigated.
$timeOut = (Get-Date).AddMinutes(5)
while ((Get-SPTimerJob $job.Name) -ne $null)
{
Write-Host -NoNewLine .
Start-Sleep -Seconds 2
if ((Get-Date) -gt $timeOut)
{
$message = "Operation timed out while waiting for the solution deployment timer job to complete."
$exception = New-Object System.OperationCanceledException $message
$errorId = "TimeOutWaitingForJob"
$errorCategory = "OperationTimeout"
$target = "Wait-TimerJob"
$errorRecord1 = New-Object Management.Automation.ErrorRecord $exception, $errorId, $errorCategory, $target
$PSCmdlet.ThrowTerminatingError($errorRecord1)
}
}
Write-Host ""
}
}
if ((Get-SPFeature -Site $siteUrl -Identity $featureName -EA SilentlyContinue) -ne $null)
{
Write-Host "Disabling feature..."
Disable-SPFeature $featureName -Url $siteUrl -force -confirm:$false
Write-Host "Feature disabled."
}
$solutionRef = Get-SPSolution -Identity $packageName -EA SilentlyContinue
if ($solutionRef -ne $null)
{
if ($solutionRef.Deployed)
{
Write-Host "Uninstalling solution..."
Uninstall-SPSolution –Identity $packageName –WebApplication $webApp -confirm:$false
Wait-TimerJob
Write-Host "Solution uninstalled."
}
Write-Host "Removing solution..."
Remove-SPSolution –Identity $packageName -confirm:$false
Write-Host "Solution removed."
}
Write-Host "Adding new solution..."
$result = Add-SPSolution $targetDirectory\$packageName
Write-Host "Solution added."
Write-Host "Installing solution..."
Install-SPSolution –Identity $packageName –WebApplication $webApp -GACDeployment -CASPolicies -Force
Wait-TimerJob
Write-Host "Solution installed."
Write-Host "Enabling feature..."
Enable-SPFeature $featureName -Url $siteUrl -force
Write-Host "Feature enabled."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment