Tag Archives: powershell

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.

    OfficeWriter for the IT Pro: Automated Dell Warranty Lookup using Powershell and ExcelTemplate

    OfficeWriter for the IT Pro posts are aimed at exploring ways to extend the use of OfficeWriter to the IT work space.

    This script will dynamically query Dell’s Warranty web-service via PowerShell and export the results to an Excel (xlsx) file using OfficeWriter’s ExcelTemplate object. I’ve added colored conditional formatting depending on how many days are left before the warranty expires.

    In the script, we leverage two external community provided PowerShell functions, Out-DataTable and Get-DellWarranty. Get-DellWarranty accepts a computer name then returns the results as a PowerShell object. The ExcelTemplate object will not bind a PowerShell object so we use Out-DataTable to convert the object into a .NET DataTable.

    You will need proper permissions and PowerShell access to run the script against remote servers. You will need to modify the $myComputerList variable to include the computers that you want to query. You will need to download the resources.zip file attached to this post. It contains the required PowerShell modules, DellWarrantyExporttoExcel script, DellWarrantyLook.xlsx excel template, and a sample excel output file (output.xlsx). The final requirement to run the script is a copy of OfficeWriter Standard. You can download a free evaluation here . Continue reading OfficeWriter for the IT Pro: Automated Dell Warranty Lookup using Powershell and ExcelTemplate

    Create an Excel Spreadsheet in Powershell

    This post shows you how to create an excel spreadsheet in Powershell with OfficeWriter.

    PowerShell


    ##################################################################

    ##

    ## Create an Excel Spreadsheet with ExcelApplication in Powershell ##

    ## by Jim Stallings (http://www.officewriter.com)

    ##

    ##################################################################

    # Add the assembly Add-Type -Path ‘C:\Program Files (x86)\SoftArtisans\OfficeWriter\bin\dotnet\SoftArtisans.OfficeWriter.ExcelWriter.dll

    # Create a new ExcelApplication object

    $xla = New-Object “SoftArtisans.OfficeWriter.ExcelWriter.ExcelApplication

    # Create a new workbook

    $wb = $xla.Create()

    # Add a worksheet to the workbook

    $ws = $wb.Worksheets[0]

    # Add some text to the first cell in the sheet

    $ws.Cells[‘A1’].Value = “Welcome to SoftArtisans OfficeWriter!

    # Save the workbook to disk

    $xla.Save($wb, “C:\myfile.xls“)

    Simple powershell automated FTP upload script

    [This post was originally posted on my personal blog, http://simplystallings.com]

    At work, we manually patch our web servers. Part of the process requires ftping the updates to each server. This process is tedious considering the number of web servers and number of steps required. I decided this past patch Tuesday that I would invest a little time into automating the process using powershell.

    I located a simple ftp upload script located @ http://poshcode.org/1134. With a few modifications I had my script:

    $file = "somefilename"
    $filePath = "C:\" + $file
    for ($i=0;$i-lt$servers.length;$i++)
    {
    "ftp url: $ftp"
    #FTP URL syntax
    $ftp = $servers[$i]+$file
    $webclient = New-Object System.Net.WebClient
    $uri = New-Object System.Uri($ftp)
    "Uploading $File..."
    $webclient.UploadFile($uri, $filePath)
    }

    I added an array that contains the ftp URL syntax for each web server then I iterate over the array using a for loop. Continue reading Simple powershell automated FTP upload script

    Automating Installation and Uninstallation for SharePoint 2010 Farm Solution

    I’ve been trying to figure out how to automate the process for uninstalling a farm solution in SharePoint 2010. This is a two-step process:

    1. Retract the solution from the farm
    2. Remove it from the solution store

    Unfortunately, you can’t perform the second operation right after the first, because there’s a delay when SharePoint retracts a solution. The delay can be several seconds, so you can either wait, or do something else while remembering to perform the second step.

    Similarly, when installing a farm solution, you must first add it to the solution store before deploying it. Deployment can also take several seconds. In this case the delay is less inconvenient because you can let it run and work on something else, but it’d be nice to know when deployment completes. Continue reading Automating Installation and Uninstallation for SharePoint 2010 Farm Solution

    Managing SharePoint 2010 Farm Solutions with Windows PowerShell

    In SharePoint 2007, you can manage solutions using the stsadm command. In SharePoint 2010, stsadm still works, but is deprecated in favor of PowerShell cmdlets. Whereas with stsadm you use a command-line switch to specify the operation to perform, with PowerShell you use separate cmdlets. Below are the list of stsadm commands and equivalent PowerShell commands for common tasks to manage a farm solution. SharePoint 2010 also includes support for sandbox solutions. While they are not discussed here, you’d use similar cmdlets to manage them as well.

    When running a cmdlet, the cmdlet returns right away, but the operation will take some time to complete. If you attempt to execute one cmdlet immediately after another, you may get an error if the previous operation hasn’t finished running.

    To get further information on a cmdlet, for instance add-spsolution, just type get-help add-spsolution in PowerShell. Continue reading Managing SharePoint 2010 Farm Solutions with Windows PowerShell