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.
First, let's ensure you have installed PowerShell ISE:
Let's now install PowerGUI:
Now that you have all of the tools installed, let's use PowerShell ISE and PowerGUI to author, debug, and execute our new script.
Let's see how PowerShell ISE and PowerGUI can help with your script authoring.
$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")
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.
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
$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")
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.
Let's take a look at how we can author and execute scripts 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.
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.
3.23.101.60