Book Image

Microsoft SharePoint 2010 and Windows PowerShell 2.0: Expert Cookbook

By : Yaroslav Pentsarskyy
Book Image

Microsoft SharePoint 2010 and Windows PowerShell 2.0: Expert Cookbook

By: Yaroslav Pentsarskyy

Overview of this book

PowerShell is tightly integrated with SharePoint 2010, demonstrating an important alliance between the fastest growing collaboration and web publishing platform, and the latest task automation framework. The advantages of PowerShell and SharePoint integration help administrators and infrastructure specialists achieve everyday enterprise tasks more efficiently, and this book will ensure you get the most out of SharePoint configuration and management. When it comes to custom SharePoint 2010 solution configuration, creating robust PowerShell scripts is the best option for saving time and providing a point of reference as to the changes made in the server environment. This practical expert cookbook translates the most commonly found scenarios into a series of immediately usable recipes, allowing you to get up and running straight away with writing powerful PowerShell scripts for SharePoint. “Microsoft SharePoint 2010 and Windows PowerShell 2.0: Expert Cookbook” focuses on a range of distinct areas of SharePoint administration, with expert recipes targeting unique business examples.You will learn exactly how solutions were achieved for managing SharePoint list settings with PowerShell, PowerShell configuration of SharePoint FAST Search, and more. You will also learn how to tailor the recipe to your own business needs.With this advanced cookbook in hand, you will be fully equipped with the source code as a starting point for creating your scripts in order to take advantage of the integration between SharePoint and PowerShell.
Table of Contents (15 chapters)
Microsoft SharePoint 2010 and Windows PowerShell 2.0: Expert Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface

Authoring, debugging, and executing script accessing farm settings with PowerGUI and PowerShell ISE


As you can see from the previous recipe, authoring and executing a PowerShell script is a simple task that can be done right from the command line. In this recipe, we'll take a look at how you can author and debug your PowerShell scripts using two of the most popular tools: PowerShell ISE and PowerGUI. Using these tools, we'll execute a script accessing farm settings of the SharePoint site.

Getting ready

First, let's ensure you have installed PowerShell ISE:

  1. 1. On the target Virtual Machine, click Start | Administrative Tools | Server Manager.

  2. 2. On the left-hand side panel of the Server Manager window, click the Features node.

  3. 3. In the main window of the Server Manager, click Add Features.

  4. 4. From the Add Features Wizard, ensure Windows PowerShell Integrated Scripting Environment (ISE) is selected. If it is selected and grayed out, as seen in the following screenshot, skip to Step 6 in this sequence.

  5. 5. Click Next and Install on the following window to install the feature.

  6. 6. Upon installation completion, close the Server Manager window.

Let's now install PowerGUI:

  1. 1. Navigate to http://www.powergui.org or search the Internet with PowerGUI.

  2. 2. Download the latest version of PowerGUI installer.

  3. 3. Run the installation package on your development environment and install the PowerGUI tool using the default installation options.

Now that you have all of the tools installed, let's use PowerShell ISE and PowerGUI to author, debug, and execute our new script.

How to do it...

Let's see how PowerShell ISE and PowerGUI can help with your script authoring.

  1. 1. On your development environment, click Start | All Programs | Accessories | Windows PowerShell | Windows PowerShell ISE.

  2. 2. In the PowerShell ISE window's top section, type in the following script:

    $siteUrl = "http://intranet.contoso.com"
    $snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}
    if ($snapin -eq $null) {
    Write-Host "Loading SharePoint Powershell Snapin"
    Add-PSSnapin "Microsoft.SharePoint.Powershell"
    }
    $site = Get-SPSite | Where-Object {$_.Url -eq $siteUrl}
    $site.WebApplication.QueryFeatures("00BFEA71-EC85-4903-972D-EBE475780106")
    

    Note

    Downloading the example code

    You can download the example code fles for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the fles e-mailed directly to you.

  3. 3. Press F5 on your keyboard.

  4. 4. Take a note of the results returned by the script which will contain multiple instances in the following format:

    DefinitionId : 00bfea71-ec85-4903-972d-ebe475780106
    Parent : My
    Properties : {}
    Definition : SPFeatureDefinition Name=FeatureDefinition/00bfea71-ec85-4903-972d-ebe475780106
    Version : 3.0.0.0
    FeatureDefinitionScope : Farm
    
  5. 5. Now let's see the result with PowerGUI. On your development environment, click Start | All Programs | PowerGUI | PowerGUI Script Editor.

  6. 6. In the top section of the PowerGUI editor, insert the same code we used in step 2 of this sequence:

    $siteUrl = "http://intranet.contoso.com"
    $snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}
    if ($snapin -eq $null) {
    Write-Host "Loading SharePoint Powershell Snapin"
    Add-PSSnapin "Microsoft.SharePoint.Powershell"
    }
    $site = Get-SPSite | Where-Object {$_.Url -eq $siteUrl}
    $site.WebApplication.QueryFeatures("00BFEA71-EC85-4903-972D-EBE475780106")
    
  7. 7. Press F5 to execute your script.

  8. 8. Take a note of the same result set in the PowerShell Console window right below the editor, seen in the previous image.

  9. 9. Switch back to the script editor section of the screen and set your cursor on the last line of the code.

  10. 10. Press F9 to set the breakpoint on the last line of the code.

  11. 11. Press F5 to execute the script up to the breakpoint.

  12. 12. Take a note of the script editor window when the script has been executed up to the breakpoint. Your PowerGUI editor will look similar to the following screenshot:

  13. 13. At this point you can press F5 on your keyboard to continue execution.

How it works...

We launched the PowerShell ISE to execute our custom script. The first thing our script is going to do is load the PowerShell cmdlet library for SharePoint. This extension library holds various PowerShell functions allowing us to work with SharePoint objects from within PowerShell. Once the library is loaded, our script connects to our SharePoint site, http://intranet.contoso.com, and gets a hold of the current site. Further, the script calls a function which enumerates all of the SharePoint sites and their basic details which have a specified featured ID active in them, as seen in the following screenshot

This function can be pretty handy when you're trying to locate problem features, or determine which site will be affected by the planned feature upgrade.

Our PowerShell script has been executed first in PowerShell ISE to see what capabilities you have in this Integrated Scripting Environment (ISE).

We then used PowerGUI to see how the same script can be executed and debugged. As you can see, PowerGUI has a few more features facilitating the script authoring process.

The debug option available in the script editor is particularly handy when your script doesn't quite yet work to your standards, and you want to figure out potential problems in it. If you're a developer, you already know all about debugging and its benefits.

Once you're satisfied with the script, you can execute it and run it on the current environment.

There's more

Let's take a look at how we can author and execute scripts with PowerGUI.

Script authoring with PowerGUI

One of the other advantages to PowerGUI is the ability to see values of variables in your script as it executes. The Variables panel is, by default, on the right-hand side of your PowerGUI window as seen here:

Without this panel, if you wanted to list the variable value, you would typically need to call it in a command line. If the variables in question are complex objects, you get to see the value of all the properties too, as shown in the following screenshot:

Also, to aid you with script authoring, PowerGUI has a collection of handy snippets which you can access with the Edit | Insert Snippet option.

Note

For more tips on working with PowerGUI user interface and features, check out http://www.Powergui.org. For more tips on PowerShell ISE, search TechNet for Windows PowerShell Integrated Scripting Environment.