The Importance of Knowing How to Read PowerShell Scripts

04/07/2010

PowerShell

Not everyone is going to completely master PowerShell to the point where they are writing their own scripts or functions. You may be tasked with far too much in your organization to have the time to learn the ins and outs of how PowerShell can interact with SharePoint 2010. What I do expect to happen is that people will find PowerShell scripts and functions online that should result in what they need to accomplish. However, what you are downloading or copying and pasting is in essence truly a script, and like all scripts, you MUST know what they do before you execute them. To showcase how I would read a script, let’s take a look at a very useful one provided in a recent blog post by Todd Klindt that backs up all the site collections in a farm:

Get-SPWebApplication | Get-SPSite | ForEach-Object{$FilePath = “C:\Backup\” + $_.Url.Replace(“http://”,””).Replace(“/”,”-“) + “.bak” ; Backup-SPSite -Identity $_.Url -Path $FilePath}

I would bet that most of the scripts you’ll find online will include the “|” character. This character is called the Pipeline. It “pipes” results or objects to the next command following it. When trying to understand the functionality of a script, I like to read the pipeline as “from those results”. So, based on the above, I would read the script as the following:

Get each web application, and from those results, get each site collection, and from those results, FOR EACH, set the path and filename format and backup the site collection.

Of course, you will have to be familiar with what the SharePoint PowerShell command does in order to know what it pipes to the next command. If you need to know what one does, simply open up the SharePoint 2010 Management Shell and type the name of the command with “-?” after it to get a description of its functionality (ex. Get-SPWebApplication -?).

I hope this is able to assist you when locating and understanding scripts found online that might solve a given task.

Advertisements

Subscribe

Subscribe to our RSS feed and social profiles to receive updates.

One Comment on “The Importance of Knowing How to Read PowerShell Scripts”

  1. iHunger Says:

    I agree with you that you can find many PowerShell scripts online to use as a starting point for many scripting needs and that for a n00b it can be difficult to understand the PowerShell scripts. That is one of the benefits of a product I helped develop called PowerWF. PowerWF can import PowerShell scripts and turn them into visual workflows that are very easy to understand. You can modify the script parameters and run it directly in PowerWF or deploy the scripts as PowerShell CMDLETs and Modules.

    Reply

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: