Monday, February 4, 2013

SharePoint 2013 : PowerShell : Complete script to deploy WSP and activate features


The following script updates/installs solutions package and activates required features. Have been using such script for a long time but never blogged. NOTE: Few variables removed from the script. Need to initialize them properly.
# SharePoint cmdlets
cls
Write-Host ""
Write-Host ""
Write-Progress -Activity "Setting up environment" -Status "Loading Modules"
Write-Host "Setting up environment....." -foregroundcolor "Cyan"
if(!(Get-PSSnapin Microsoft.SharePoint.PowerShell -ea 0)) 
{ 
    Write-Progress -Activity "Loading Modules" -Status "Loading Microsoft.SharePoint.PowerShell" 
    Write-Host -NoNewline "Loading Modules...." -foregroundcolor "Cyan"
    Add-PsSnapin Microsoft.SharePoint.PowerShell 
    Write-Host "Done." -ForegroundColor Green
} 
Write-Host -NoNewline "Loading Assemblies...." -foregroundcolor "Cyan"
[void][system.reflection.assembly]::loadfrom("C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.SharePoint\v4.0_15.0.0.0__71e9bce111e9429c\Microsoft.SharePoint.dll")
Write-Host "Done." -ForegroundColor Green
Write-Progress -Activity "Setting up environment" -Status "Setting up Variables ...."
Write-Host -NoNewline "Setting up Variables...." -foregroundcolor "Cyan"
# Call the Function to setup environment and variables
SetupEnvironment
Write-Host "Done." -ForegroundColor Green

# ValidateURL $SiteCollectionURL
if ($WebApp -eq $null){
    Write-Host "Could not get access to "$WebApp.Url.ToString()".Please contact Administrator." -ForegroundColor Red
    Break;
}

# Check if Web Application is a null object
Write-Host "Web Application Url:"$WebApp.Url.ToString()

# Create web application configuration path variables
$global:WebAppZone = $WebApp.AlternateUrls[0].UrlZone
$global:IISSettings = $WebApp.IisSettings[$WebAppZone]
$global:WebAppConfigPath = $IISSettings.Path.ToString() + "\Web.Config"

try
{
    Write-Progress -Activity "Deploying solution package..." -Status "Adding solution to the farm"
    Write-Host "Checking if solution packages are deployed/installed." -foregroundcolor "Cyan"
    #Check if the solution package is already added and installed
    Write-Host $SolutionPackageName
    $Solution = Get-SPSolution | Where{$_.Name -eq $SolutionPackageName}
    if ($Solution -ne $null){
        if ($Solution.Deployed -eq $true){
            Write-Host -NoNewline "Updating the solution...." -foregroundcolor "Cyan" 
            Update-SPSolution -identity $Solution.SolutionId -LiteralPath ($SolutionPackageFolder + $SolutionPackageName) -GACDeployment
            Write-Host "Done." -foregroundcolor "Green"
        }
        else
        {
            Write-Host -NoNewline "Removing the solution...." -foregroundcolor "Cyan" 
            Remove-SPSolution -Identity $Solution.SolutionId -Confirm:$false
            Write-Host "Done." -foregroundcolor "Green"
            Write-Host -NoNewline "Adding the solution package" $SolutionPackageFolder$SolutionPackageName " to the store...." -foregroundcolor "Cyan"
            Write-Host "Done." -foregroundcolor "Green"
            Add-SPSolution -LiteralPath "$SolutionPackageFolder$SolutionPackageName"
            WaitForInsallation -Name $Solution.Name
        }
    } 
    else
    {
        Write-Host -NoNewline "Adding the solution package" $SolutionPackageFolder$SolutionPackageName " to the store...." -foregroundcolor "Cyan"
        Write-Host "Done." -foregroundcolor "Green"
        Add-SPSolution -LiteralPath "$SolutionPackageFolder$SolutionPackageName"
    }
}
catch 
{
    Write-Host "Errors while deploying solution package. Please contact Administrator."
    Write-Host $Error
    Break
}

WaitForInsallation -Name $Solution.Name 

Write-Progress -Activity "Deploying solution package..." -Status "Deploying solution to the web application"
Write-Host -NoNewline "Deploying" $SolutionPackageFolder$SolutionPackageName ".." -foregroundcolor "Cyan"
Install-SPSolution -Identity xxx.WSP -WebApplication $WebAppURL -GACDeployment -force  -CompatibilityLevel 15
Write-Host "Done." -foregroundcolor "Green"

Write-Host "Restart IIS..." -foregroundcolor "Cyan"
iisreset 
Write-Host "IIS restarted successfully..." -ForegroundColor "Green"

try
{
    Write-Host "Enabling features at"$SiteCollectionURL.... -ForegroundColor "Cyan"
    Write-Host -NoNewline "Activating xxx Web Part..." -ForegroundColor "Cyan"
    Enable-SPFeature -Identity "ede5b57b-714f-49cb-bb18-6f1098ee1cff" -Url $SiteCollectionURL -Force
    Write-Host "Done." -ForegroundColor "Green"
    Write-Host -NoNewline "Activating xxxx list..." -ForegroundColor "Cyan"
    Enable-SPFeature -Identity "f769d0b0-60b7-4854-ad96-6f20d17d3e7a" -Url $SiteCollectionURL -Force
    Write-Host "Done." -ForegroundColor "Green"
    Write-Host -NoNewline "Activating xxxx Master Pages..." -ForegroundColor "Cyan"
    Enable-SPFeature -Identity "70ff55bc-e010-4a51-948f-14f9c67e8d51" -Url $SiteCollectionURL -Force
    Write-Host "Done." -ForegroundColor "Green"
    Write-Host -NoNewline "Activating xxxx Pages..." -ForegroundColor "Cyan"
    Enable-SPFeature -Identity "618ced67-224e-4db1-b405-a457217b16bc" -Url $SiteCollectionURL -Force
    Write-Host "Done." -ForegroundColor "Green"
    Write-Host -NoNewline "Activating xxxx timer job..." -ForegroundColor "Cyan"
    Enable-SPFeature -Identity "818d1dd2-fd11-4a9a-bba6-8d49ec47f194" -Url $SiteCollectionURL -Force
    Write-Host "Done." -ForegroundColor "Green"
    Write-Host "All the features activated at"$SiteCollectionURL.... -ForegroundColor "Green"

}
catch 
{
    Write-Host "Error while deploying and installing features . Please contact Administrator."
    Write-Host $error
    Break
}

Write-Host ""
Write-Host ""

try
{
    #Get web page for SharePoint site
    [Microsoft.SharePoint.SPSecurity]::RunWithElevatedPrivileges({
     Write-Host "Uploading necessary documents...." -NoNewline -ForegroundColor Cyan
    # Upload required documents to document libraries
    $Web = Get-SPWeb $SiteCollectionURL
    $List = $Web.GetFolder($DocumentLibraryName)

    if ($List -ne $null)
    {
        $FullLocalFileName = $DocumentsLocalPath + $DocumentLibraryFile1 

        # Read the file
        $File = Get-ChildItem $FullLocalFileName 

        # Add file to the collection
        $UploadedFile = $Files.Add($DocumentLibraryName + "/" + $DocumentLibraryFile1, $File.OpenRead(),$true)
    }
    Write-Host "Done." -ForegroundColor Green
    })
}
catch 
{
    Write-Host "Done." -ForegroundColor Green
}



Function WaitForInsallation([string] $Name){
 Write-Host -NoNewline "Waiting for deployment job to complete" $Name "."
 $wspSol = get-SpSolution $Name
 while($wspSol.JobExists)
 {
  sleep 2
  Write-Host -NoNewline "."
  $wspSol = get-SpSolution $Name
 }
 Write-Host ""
 Write-Host -ForegroundColor Green "Deployment job is finished" 
}

Function WriteLine{
 Write-Host -ForegroundColor White "--------------------------------------------------------------"
}

No comments:

Post a Comment