Simple PowerShell Deployment Scripts

Stsadm is the standard for MOSS 2007. Although still available in SP2010, it is deprecated and should not be used. So let’s start with some basics of PS for SP.

  1. One does not simply use PowerShell: The commands are specifically geared for the SharePoint Management Shell. If you open Management Shell as an administrator and type each command in, it’ll work fine. However, when it comes to running scripts there are some issues.
  • You have to load the Microsoft.SharePoint.PowerShell snapin, like so:  “Add-PsSnapin Microsoft.SharePoint.PowerShell”
    • This may give you an error: “The local farm is not accessible.” You need to have your account enabled as a SharePoint Admin (see below)
  • Make sure you’ve set the execution policy. This one is easy, just use the “Set-ExecutionPolicy” command. The possible values are:
    • AllSigned – You can only run trusted signed scripts.
    • RemoteSigned – If the script is downloaded, it must be signed, otherwise just run it.
    • Restricted – This one is self explanatory. No one gets to run scripts. No nobody. Not nohow.
    • Unrestricted – Again, self explanatory. As a very wise English professor once told me, “Unrestricted is the opposite of restricted.” I never forgot that…
  • You always need root: You have to be an SP administrator in order to run SP commands from PowerShell. You can check your local permissions by:
    • Right clicking on SP Management Shell and running it as an administrator. This will give you access to admin privileges, but if your login isn’t a SP admin, then you can’t do any of this in regular PowerShell.
    • Typing “Get-SpShellAdmin”
    • Looking for your login.
    • If you aren’t on the list, you can use the current Shell to add yourself, like so: “Add-SpShellAdmin -UserName YOUR\UserName”
    • Now you should be able to execute SharePoint scripts in PowerShell.
  • Remember – All Files .Ps1.: The script has to be saved with extension .ps1.
    • You  shoud save as “All File Types” rather than “*.txt,” otherwise it may not work.
  • No execadmsvcjobs: That command is no longer available. So timer jobs have to be waited for a little differently.
  • ## Get the solution
    $Solution = Get-SPSolution -identity $SolutionFileName
    $lastStatus = ""
    ## loop whilst there is a Job attached to the Solution
    while ($Solution.JobExists -eq $True)
    {
     $currentStatus = $Solution.JobStatus
    	 if ($currentStatus -ne $lastStatus)
    	 {
    		Write-Host "$currentStatus…" -foreground green -nonewline
    		 $lastStatus = $currentStatus
    	 }
    	 Write-Host "." -foreground green -nonewline
      sleep 1
     }
     ## completed
     Write-Host ""
     Write-Host " "$Solution.LastOperationDetails -foreground green

    The Script
    I wrote a simple script that prompts for file name, webapp name, and what function you’d like to perform. It is based on the script found on Nik Patel’s SharePoint World blog. The difference between Nik’s and mine is that mine doesn’t check for everything before performing operations. If you choose to use it, you have to be aware that you might get an error; however, it is a very simple version and much easier to use/understand/modify.

    #####
    # Script to deploy Webparts/Solutions. Does not make use of feature installation
    # Kate Mogel 2012
    #
    # To use:
    # Place in folder with WSP file. Open powershell and cd into solution folder
    # Run script as admin, refering to local file (e.g. ./DeployScript.ps1)
    # Enter full file name (e.g. solution.wsp), web app name, and choose an operation
    #####
    param(
    [string]$solName = "$(Read-Host 'Enter WSP File name ')",
    [string]$webName = "$(Read-Host 'Enter WebAppName. i.e. http://SP2010APP ')",
    [string]$whichFunct = "$(Read-Host 'Enter IS to install, RS to remove, US to upgrade ')"
    )
    
    function main{
    
     #Check for snapin. Add if not present
     $snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}
     if ($snapin -eq $null)
     {
            Add-PSSnapin Microsoft.SharePoint.Powershell
     }
    
    #Get location
    $solLoc = Get-Location 
    
      #Check the operation
      if($whichFunct -eq "IS")
      {
     	addSol
      }
      if($whichFunct -eq "RS")
      {
          Uninstall-SPSolution -Identity $solName -WebApplication $webName
      }
      if($whichFunct -eq "US")
      {
          Update-SPSolution -Identity $solName -LiteralPath $solLoc\$solName -GACDeployment
      }
    
    }
    
    #Function to add and install solution
    function addSol{
    	$Sol = Get-SPSolution | where-object {$_.Name -eq $solName}
    	if($Sol -eq $null){
    	    Add-SPSolution -LiteralPath "$solLoc\$solName" -Confirm:$false
    	    Install-SPSolution -Identity $solName -WebApplication $webName -GACDeployment
     	}
    }
    #Run Main Method
    main

    Okay. Go deploy your WSP.

    Related posts: