28.6.16

SharePoint and PowerShell Scripts

Introduction

PowerShell scripts are very powerful, you can do almost everything with them. Also, there are some features cannot be managed through UI and only available through PowerShell. 

Writing scripts to automate deployments and configuration becomes very useful for repeatable tasks which will save you much time. Also, writing PowerShell scripts reduces human errors.

It is most useful when the deployment package will be deployed repeatedly and in different environments like (Testing environment and then production environment). 

I believe as a SharePoint developer you should become familiar with PowerShell scripts and start writing them eventually.

Sample Deployment Script

The following sample script deploys a solution to SharePoint site:


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

$siteUrl = "http://sp2013:8000"

# it is always better to deactivate features in the solution if they already exist
$features = Get-SPFeature | Where-Object {$_.DisplayName -like "*HelloWorldWebPart*"}
foreach($feature in $features)
{
    Disable-SPFeature $feature -Url $siteUrl -Confirm:$false
    Write-Host Feature $feature.DisplayName was deactivated
}

$solutionPackageName = "HelloWorldWebPart.wsp"
$solution = Get-SPSolution | Where-Object {$_.Name -eq $solutionPackageName}
#check to see if the solution package has been installed
if ($solution -ne $null)
{
    # check to see if solution package is currently deployed
    if ($solution.Deployed -eq $true)
    {
        Uninstall-SPSolution -Identity $solutionPackageName -WebApplication $siteUrl -Confirm:$false
        Write-Host Solution $solutionPackageName is retracting.
    }
    
    # wait utill the solution is retracted
    $solution = Get-SPSolution | Where-Object {$_.Name -eq $solutionPackageName}
    while ($solution.Deployed -eq $true)
    {
        sleep -s 10
        $solution = Get-SPSolution | Where-Object {$_.Name -eq $solutionPackageName}
    }

    Remove-SPSolution -Identity $solutionPackageName -Confirm:$false
    Write-Host Solution $solutionPackageName was removed.
}

$solutionPath = $PSScriptRoot + "\" + $solutionPackageName
Add-SPSolution -Literal $solutionPath
Write-Host Solution $solutionPackageName was added.
Install-SPSolution -Identity $solutionPackageName -Local -GACDeployment -WebApplication "http://sp2013:8000"
Write-Host Solution $solutionPackageName was deployed.

$features = Get-SPFeature | Where-Object {$_.DisplayName -like "*HelloWorldWebPart*"}
foreach($feature in $features)
{
    Enable-SPFeature $feature -Url $siteUrl
    Write-Host $feature.DisplayName was activated.
}

Saving / Editing and Running the Script

Copy the script into notepad then save it with .ps1 extension. Make sure you edit the SiteUrl to your testing site before you save the file.

To run the script you can right click the file and click Run with PowerShell. If you want to keep the command window open once the script is finished add Read-Host at the end of the script.

The output of running the script will look like this:

0 comments:

Post a Comment