Skip to content

Instantly share code, notes, and snippets.

@estruyf
Created May 2, 2014 12:29
Show Gist options
  • Save estruyf/c32c9f7af2cf71de5f32 to your computer and use it in GitHub Desktop.
Save estruyf/c32c9f7af2cf71de5f32 to your computer and use it in GitHub Desktop.
cls
Write-Host "Loading SharePoint Commandlets"
Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue
Write-Host -ForegroundColor Green " Commandlets Loaded ..."
Write-Host
## Global Functions
function Get-ScriptDirectory {
$Invocation = (Get-Variable MyInvocation -Scope 1).Value
Split-Path $Invocation.MyCommand.Path
}
## Global Variables
$path = Get-ScriptDirectory
Function BackUpSolution([string]$SolutionName) {
Write-Host "Backup solution: $SolutionName" -NoNewline
$farm = Get-SPFarm
$solution = $farm.Solutions.Item($SolutionName).SolutionFile
$backupFilename = "backup_" + $SolutionName
$backupPath = $path + "\" + $backupFilename
$solution.SaveAs($backupPath)
Write-Host " - " -NoNewLine
Write-Host "Done" -ForegroundColor Yellow
}
Function RetractSolution([string]$SolutionName) {
Write-Host "Rectracting solution: $SolutionName" -NoNewline
$Solution = Get-SPSolution | ? {($_.Name -eq $SolutionName) -and ($_.Deployed -eq $true)}
if ($Solution -ne $null)
{
if($Solution.ContainsWebApplicationResource)
{
Uninstall-SPSolution $SolutionName -AllWebApplications -Confirm:$false
}
else
{
Uninstall-SPSolution $SolutionName -Confirm:$false
}
}
while ($Solution.JobExists)
{
Start-Sleep 2
}
Write-Host " - " -NoNewLine
Write-Host "Done" -ForegroundColor Yellow
}
Function RemoveSolution([string]$SolutionName) {
Write-Host "Removing solution: $SolutionName" -NoNewline
if ($(Get-SPSolution | ? {$_.Name -eq $SolutionName}).Deployed -eq $false)
{
Remove-SPSolution $SolutionName -Confirm:$false
}
Write-Host " - " -NoNewLine
Write-Host "Done" -ForegroundColor Yellow
}
Function AddSolution([string]$SolutionName) {
Write-Host "Adding solution: $SolutionName" -NoNewline
$SolutionPath = $path + "/" + $SolutionName
Add-SPSolution $SolutionPath | Out-Null
Write-Host " - " -NoNewLine
Write-Host "Done" -ForegroundColor Yellow
}
Function DeploySolution([string]$SolutionName) {
Write-Host "Deploying solution: $SolutionName" -NoNewline
$Solution = Get-SPSolution | ? {($_.Name -eq $SolutionName) -and ($_.Deployed -eq $false)}
if(($Solution -ne $null) -and ($Solution.ContainsWebApplicationResource))
{
Install-SPSolution $SolutionName -AllWebApplications -GACDeployment -Confirm:$false
}
else
{
Install-SPSolution $SolutionName -GACDeployment -Confirm:$false
}
while ($Solution.Deployed -eq $false)
{
Start-Sleep 2
}
Write-Host " - " -NoNewLine
Write-Host "Done" -ForegroundColor Yellow
}
$solution = "yourfarmsolution.wsp"
Write-Host "Processing"$solution -ForegroundColor White
Write-Host
BackUpSolution $solution
RetractSolution $solution
RemoveSolution $solution
AddSolution $solution
DeploySolution $solution
Write-Host
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment