Deploying your solution with PowerShell – Part 2

In this second post of my PowerShell series I want to focus on creating your SharePoint sites and provisioning them. In my previous post I showed you how to deploy your solutions and how to remove them. I have refactored the code in order to have re-usable code and to keep our next script clean and readable.

Refactored code

For deploying and removing solution I created a separate script file:

Solutions.ps1 contains the following code:

param([string]$solution, [string]$url, [switch]$deploy = $false,[switch]$remove = $false)

$sleeptime = 2
$maximum = 50

# Locate the Solution File:
$solutionFile = Get-ChildItem $solution
# Get the Solution Name
$solutionName = [System.IO.Path]::GetFilename($solutionFile)

if ( $url -ne "" ) {
    Write-Host ""
    Write-Host -ForegroundColor Yellow " - target is $url"
    Write-Host ""
}

if ($remove) {
    $sol = Get-SPSolution $solutionName

    if($sol){

        Write-Host -ForegroundColor Green "Removing $solutionName"
        Write-Host ""
   
       if( $sol.Deployed -eq $true) {
        
            if ( $sol.ContainsWebApplicationResource ) {
                Uninstall-SPSolution -Identity $solutionName -Confirm:0 -Webapplication $url
            }
            else {
                Uninstall-SPSolution -Identity $solutionName -Confirm:0
            }
            $counter = 1
            while( $sol.JobExists -and ( $counter -lt $safeguard ) ) {
                Write-Host -ForegroundColor yellow "Retracting $solutionName. Please wait..."
                sleep $sleeptime
                $counter++
            }
        }
        
        Write-Host ""
        Write-Host -ForegroundColor Green "$solutionName is retracted."
        Write-Host ""

        Remove-SPSolution -Identity $solutionName -Force -Confirm:0
        Write-Host -ForegroundColor Green "$solutionName is removed."
    }
    else {
        Write-Host -ForegroundColor Yellow " $solutionName is not present in this farm"
    }
}
if($deploy){

    Write-Host -ForegroundColor Green "Installing $solutionName "
    $sol = Add-SPSolution $solutionFile

    if ( $sol.ContainsWebApplicationResource ) {
        Write-Host -ForegroundColor Green "Deploying $solutionName to $url"
        Install-SPSolution -Identity $solutionName -GacDeployment -CasPolicies -Force -Webapplication $url
    }
    else {
        Write-Host -ForegroundColor Green "Deploying $solutionName"
        Install-SPSolution -Identity $solutionName -GacDeployment -CasPolicies -Force
    }

    
    $sol = Get-SPSolution $solutionName

    if ($sol.Deployed -eq $false ) {
        $counter = 1
        while( ($sol.JobExists -eq $true ) -and ( $counter -lt $maximum  ) ) {
            write-host -ForegroundColor yellow "Please wait..."
            sleep $sleeptime
            $counter++
        }
    }

    Write-Host ""
    Write-Host -ForegroundColor Green " $solutionName is deployed"
}

The Param keyword makes it possible to call the script with parameters. The values that are provided are the default ones.

In our Deploy.ps1 script we can now use the following:

.solution.ps1 -solution "Octavie.SharePoint.Intranet.wsp" –deploy –url "http://intranet.ochadev.local"

The same for the Remove.ps1 script, but now using the –remove switch. Of course, you can also deploy multiple solutions, just by calling the script again.

Creating your sites

Let’s extend our Deploy.ps1 script file. After deploying our solution, we can now create a site collection. To do this we have a cmdlet New-SPSite. How convenient…

    param([string]$url="http://intranet.ochadev.local",[string]$siteowner="Octavie",[int]$language=1033)

    .solution.ps1 -solution "Octavie.SharePoint.Intranet.wsp" –deploy –url $url

    Write-Host ""
    Write-Host -ForegroundColor Green "Creating Intranet Home site $url"
    Write-Host ""
    
    # Create the Home site
    $site = New-SPSite -Url $url –OwnerAlias $siteowner -Name "Home" -Description "Intranet" -Language $language -Confirm:$false

For enabling features you can use the cmdlet Enable-SPFeature.

    Write-Host -ForegroundColor Green "Activating feature Intranet - WebParts"
    Enable-SPFeature -id 0fbe96fd-2b1d-4446-9285-4dd322f5947a -url $url -Confirm:$false

For creating subsite you can use the cmdlet New-SPWeb.

    # Create the site structure
    Write-Host -ForegroundColor Green "Creating Intranet site structure"
    Write-Host -ForegroundColor Green "- Web1"
    $newWeb1 = New-SPWeb -Name "Web1" -Description "Web1" -Url "$url/Web1" -Language $language -UniquePermissions:$false -Confirm:$false

    Write-Host -ForegroundColor Green "- Web2"
    $newWeb2 = New-SPWeb -Name "Web2" -Description "Web2" -Url "$url/Web2" -Language $language -UniquePermissions:$false -Confirm:$false

    Write-Host -ForegroundColor Green "- Web3"
    $newWeb3 = New-SPWeb -Name "Web3" -Description "Web3" -Url "$url/Web3" -Language $language -UniquePermissions:$false -Confirm:$false

As you can see, it’s not that hard at all. Imagine the possibilities… Next time we take the script to the next level. We will create lists, views and maybe publishing pages with webparts.

Share

2 thoughts on “Deploying your solution with PowerShell – Part 2

  1. Pingback: Deploying your solution with PowerShell – Part 3 (Final) » blog.octavie.nl

  2. Pingback: Deploying Solutions with Language Packs with PowerShell » *

Leave a Reply

Your email address will not be published. Required fields are marked *