Subscribe:

Labels

Thursday, September 27, 2018

Solution deployment for Web Application level


1.     

Param ($WebAppUrl = $null, $MaxWait = 180)

Try
{

Add-PsSnapin Microsoft.SharePoint.PowerShell

if($WebAppUrl -eq $null)
{
throw "-WebApp is required"
}

if((Get-SPWebApplication $WebAppUrl) -eq $null)
{
throw [System.String]::Format("{0} is not a valid web application on this farm", $WebAppUrl)
}

Get-Service sptimerv4 | ?{ $_.Status -eq "Stopped" } | Start-Service

function Get-ScriptDirectory
{
    $Invocation = (Get-Variable MyInvocation -Scope 1).Value
    Split-Path $Invocation.MyCommand.Path
}

function WaitForJobToFinish
{        
Write-Host -NoNewLine "Finding timer jobs"
$RemainingWait = $MaxWait;

while (($jd = Get-SPTimerJob | ?{ ($_.Name -like "*solution-deployment*") -or ($_.Name -like "*solution-retraction*") }) -ne $null)
{
# got multiple, wait for the first one
if($jd.Count -gt 1)
{
$jd = $jd[0]
}
$jdName = $jd.Name
Write-Host "`njob: $jdName"
Write-Host -NoNewLine Waiting to finish
while ((Get-SPTimerJob $jdName) -ne $null)
{
if($RemainingWait -le 0)
{
throw [System.String]::Format("Timed out waiting for {0} to complete", $jdName);
}
Write-Host -NoNewLine .
Start-Sleep -Seconds 1
$RemainingWait--;
}
Write-Host
$jd.HistoryEntries | %{ Write-Host job history: $_.Status }
Write-Host
}
Write-Host
}

WaitForJobToFinish

$farm = Get-SPFarm;

$WSPS=@("Publishing.wsp","Webparts.wsp");
$DeployedApps=@();

foreach($WSP in $WSPS)
{
$s = $farm.Solutions[$WSP];
if(($s -ne $null) -and ($s.Deployed))
{
if($s.DeployedWebApplications -eq $null)
{
Write-Host "Retracting", $WSP;
Uninstall-SPSolution -Identity $WSP;
            WaitForJobToFinish
}
else
{
foreach($webapp in $s.DeployedWebApplications)
{
                if($s.Name -eq "RSG.UI.Corporate.Webparts.wsp")
                {
                    $DeployedApps=$DeployedApps + $webapp.Url;
    Write-Host "Retracting", $WSP, "from", $webapp.Url;
    Uninstall-SPSolution -Identity $WSP -WebApplication $webapp.Url;
                    WaitForJobToFinish
                }
                else
                {
                    Write-Host "Retracting", $WSP;
    Uninstall-SPSolution -Identity $WSP -AllWebApplications;
                    WaitForJobToFinish
                }
}
}
}
}

foreach($WSP in $WSPS)
{
$s = $farm.Solutions[$WSP];
if(($s -ne $null) -and !($s.Deployed))
{
Write-Host "Deleting", $WSP;
Remove-SPSolution -Identity $WSP;
}
}
WaitForJobToFinish

foreach($WSP in $WSPS)
{
    $s = $farm.Solutions[$WSP];
    if($s -eq $null)
    {
   Write-Host "Adding", $WSP;
        $Path=Join-Path (Get-ScriptDirectory) $WSP;
   $s = Add-SPSolution $Path;
}
if($null -eq $s)
{
throw "Unable to find solution " + $WSP + " after adding it"
}
}

function VerifySolutionDeployment($wspName)
{
$s = $farm.Solutions[$wspName];
if($null -eq $s)
{
throw "Unable to find solution " + $wspName
}

Write-Host $s.Name "last operation result: " $s.LastOperationResult "@" $s.LastOperationEndTime

if($s.LastOperationResult -ne [Microsoft.SharePoint.Administration.SPSolutionOperationResult]::DeploymentSucceeded)
{
Write-Host "Details:" $s.LastOperationDetails
throw "Deployment failed for" + $wspName
}
}

$DeployedApps=$DeployedApps+$WebAppUrl;

foreach($WSP in $WSPS)
{
$s = $farm.Solutions[$WSP];
if(($s -ne $null) -and !($s.Deployed) -and !($s.ContainsWebApplicationResource))
{
        Write-Host -ForegroundColor yellow "Installing " $WSP "globally"
        Install-SPSolution -Identity $s -GACDeployment
        WaitForJobToFinish
    }
    else
    {
        if(!($DeployedApps -eq $null))
        {
            if($s.ContainsWebApplicationResource)
            {
            foreach($AppUrl in $DeployedApps)
            {
                Write-Host -ForegroundColor yellow "Installing "  $WSP  "to " $AppUrl
                Install-SPSolution -Identity $s -WebApplication $AppUrl -GACDeployment -force
                WaitForJobToFinish
            }
            }
        }
    }
}

foreach($WSP in $WSPS)
{
VerifySolutionDeployment($WSP)
}

Write-Host 'Restarting timer service...'
net stop SPTimerV4
sleep 5
net start SPTimerV4

Write-Host 'Resetting IIS...'
iisreset

foreach($deployedUrl in $DeployedApps)
{
    Enable-SPFeature -identity "f5583763-f04f-4b2a-b285-90ac262e0271" -Url $deployedUrl -force
    WaitForJobToFinish
    sleep 5
    Enable-SPFeature -identity "76e4b4ff-acb1-4c95-9580-e217ad8826bb" -Url $deployedUrl -force
    WaitForJobToFinish
    Enable-SPFeature -identity "a0e43b40-a562-4cf4-935c-b43a58d3d83c" -Url $deployedUrl -force
    WaitForJobToFinish
    Write-host -ForegroundColor yellow 'Features enabled for ' $deployedUrl
}

Write-Host -ForegroundColor Green 'Finished Deployment'
}
Catch
{
    write-host -ForegroundColor red "Error";
    Write-Error $_;

}
Finally
{
    Remove-PsSnapin Microsoft.SharePoint.PowerShell
}

No comments:

Post a Comment