The Power of PowerShell

Slalom Consultant Marc Schuricht

Slalom Consultant Marc Schuricht is a Denver based software Solution Architect with over 12 years of professional experience in IT projects ranging from configuration and change management to custom enterprise application development.

With the new SharePoint PowerShell extensions that come with SharePoint 2010, we can do some pretty powerful stuff that was a little bit awkward with earlier versions of SharePoint.  In the past, I may have written a one-off console applications to iterate over sites looking for content, modifying that content and disposing of those objects.  That means, that I now have a full-blown executable that I need to put on the production server polluting what should be a pristine environment.  I could have also lazy-loaded the SharePoint assemblies with PowerShell and written the same scripts, which would not be very secure and possible cause memory leaks if I do not dispose of my variables correctly.  Now, I have native PowerShell commandlets to provide this functionality.  Here are a couple examples:

  1. I have a slew of webs which are all built off of the same site definition and all contain a list of events with the name “Case Schedule”.  I need a one-off mechanism to build a report showing the details of all events for a particular date range.
    $web = Get-SPWeb http://<Site Url>”
    “””Title””,””Event Date””,””Location””,””Description””” > Briefs.csv
    $web.Webs | %{
    $w = $_
    $l = $w.Lists[“Case Schedule”]
    $l.Items | Where-Object { $_[“EventDate”] -gt [System.DateTime]::Parse(“1/1/2003”) } | %{
    $li = $_
    System.DateTime] $eventDate = $li[“EventDate”]
    $startDate = $eventDate.ToLocalTime()
    “””$($w.Title)””,””$($eventDate)””,””$($li[‘Location’])””$($li[‘Description’])””” >> Briefs.csv


    With Get-SPWeb I can now safely get access to a native SPWeb object as well as all of its members including .Webs, .AllProperties, .Lists, etc.  When this script is complete, I have a nicely formatted CSV.

  2. I need to push down data from production to my test environment so that I can keep them in sync.  This will encompass, un-mounting my content databases(es), restoring from my .bak file, re-mounting the databases and finally performing an IISRESET.  I can automate all of these tasks with a simple PowerShell script:
    param([System.String] $contentDatabase, `
    System.String] $webApplication, `
    System.String] $database, `
    System.String] $instance, `
    System.String] $location) 

    #Load SQL Server 2008 Snapins
    Add-PSSnapin “SqlServerProviderSnapin100”
    Add-PSSnapin “SqlServerCmdletSnapin100”$restore = “RESTORE DATABASE $($contentDatabase) FROM  DISK = N’$($location)’ WITH  FILE = 1,
    MOVE N’$($contentDatabase)’ TO N’C:\Program Files\Microsoft SQL Server\MSSQL10.OFFICESERVERS\MSSQL\DATA\$($contentDatabase).mdf’,
    MOVE N’$($contentDatabase)_log’ TO N’C:\Program Files\Micro
    soft SQL Server\MSSQL10.OFFICESERVERS\MSSQL\DATA\$($contentDatabase).LDF’,
    Remove-SPContentDatabase $contentDatabase -Confirm $false
    Invoke-Sqlcmd -Database $database -ServerInstance $instance -Query $restore
    Mount-SPContentDatabase $contentDatabase -WebApplication $webApplication -Confirm $false

Anyway, I have seen so many examples online for provisioning SP2010 service applications, I thought it would be nice to show some other real-world examples of how to use PowerShell in conjunction with SharePoint 2010 and other snap-ins.


Slalom Consulting's Denver office Slalom Consulting has 6 Gold Microsoft Partner Competencies
More about Slalom Consulting’s Denver office. More about Slalom Consulting’s Microsoft Partnership.

subscribe by emailSubscribe to be emailed about new SharePoint posts.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: