PowerShell has a rich set of advanced features that provide an Administrator with a multitude of options depending on what they would like to do. This nugget will take a more focused look into the many ways objects can be used. Remember, an object in this case is a variable. From Part 1 of this nugget, you know that variables are objects that hold something. This nugget will conclude with some PowerShell scripting basics. Thanks goes to Sheyi for providing a technical review.

 

PipeBind

PipeBind is a concept where a parameter accepts an object “variable” of a specific type. If you look at the syntax of get-help new-spsite –full, you will discover some parameters accept pipe bind:

New-SPSite -Url <String> -OwnerAlias <String> [-AssignmentCollection <SPAssignmentCollection>] [-Confirm <SwitchParameter>]] [-ContentDatabase <SPContentDatabasePipeBind>] [-Description <String>] [-HostHeaderWebApplication <SPWebApplicationPipeBind>] [-Language <UInt32>] [-Name <String>] [-OwnerEmail <String>] [-QuotaTemplate <SPQuotaTemplatePipeBind>] [-SecondaryEmail <String>] [-SecondaryOwnerAlias <String>] [-SiteSubscription <SPSiteSubscriptionPipeBind>] [-Template <SPWebTemplatePipeBind>] [-WhatIf [<SwitchParameter>]] [<CommonParameters>]

In this example, there are several parameters that accept pipe binds. Here is an example of passing a variable to the template parameter which accepts a pipe bind.

$tem = get-spwebtemplate –identity STS#0

New-spsite –url http://contosoweb –owneralias contoso\administrator –name contososite –template $tem

 

Properties and Methods

Variables have properties and method’s available based on the type of the variable. For example, declaring a variable and assigning it a web application will contain all properties and functions for type SPWebApplication. To view all of the available properties and methods, you can pipeline the get-member cmdlet.

For Example:

$web = get-spwebapplication

$web | get-member

 

clip_image002

clip_image004

Note: This is not a complete list of all method’s and properties available for this type.

It’s possible to add conditions to get more desirable results. For Example, you can use the where alias if you just want to see a specific member type. To view only members of type Method punch in the following:

$web | get-member | where {$_.MemberType –eq “Method”}

Note: It is possible to do more than one pipeline J

 

Viewing Properties

Before updating and viewing specific properties of an object, it’s important to know how to first view all properties and there corresponding values. By default, only a few properties are exposed by simply printing a variable.

For Example:

clip_image006

 

Use get-object cmdlet to view all properties of a given object. An alias for the get-object cmdlet is available called select which is what I prefer to use.

For Example:

clip_image008

Note: Another option to view all properties of a given object is available.

For Example: $web | fl

 

If you happen to know which property to specify, simply append the property name with the variable.

For Example:

clip_image010

In this example, two content databases are outputted because the Web Application has two content databases attached. What if you only need to make changes to one of them? Where-object command-let is available which assists in filtering down results. An alias exists for the where-object command-let called where which is what I prefer to use.

$web.contentdatabases | where {$_.name –eq “WSS_Content”}

The example above demonstrates how to view and filter properties against a single object\variable. What if you know which property to use and simply want to filter directly from the command-let. Some command-lets offer parameters which allow filtering on the fly.

For Example, the –filter parameter is available running Get-SpSite command-let.

Get-spsite –filter {$_.owner –eq “contosoweb\administrator”}

This example outputs every site collection where the owner is set as contosoweb\administrator. This is a much faster query when dealing with a large # of sites.

Note: You can also use the –like wild card instead of –eq.

For Example: Get-spsite –filter {$_.owner –like “contoso\ad*”}

 

Setting Properties

Besides viewing properties, it’s possible to update properties as well.

For Example, setting a property called name and updating it using update method:

$web.name

Output: SharePoint – 80

$web.name = “My Renamed Site!”

$web.update()

$web.name

Output: My Renamed Site!

  

Running Methods

Besides using the update method above to set a property, you have a variety of methods available for a specific object type. For Example:

clip_image012

 

 

Scripting with PowerShell

PowerShell scripts provide an automated approach to group one or more command-lets in order to accomplish one or more tasks. For Example, PowerShell scripts can provision new Web Applications\Site Collections, service applications, or backup\restore farm. Again, the possibilities are endless depending on what an Administrator wants to do. A powershell script is as easy as dropping your command-lets in notepad and saving the file with a .ps1 extension. Variables are often used in PowerShell scripts because they save time in writing scripts as well as provide a cleaner look to the script. The following example is a simple script to provision a new site collection:

$tem = get-spwebtemplate –identity STS#0

$russmax = get-spuser –identity contoso\administrator -web http://contosoweb

$DB = get-contentdatabase

new-spsite -url http://contosoweb/sites/dancan -template $tem -Name DanCanSite -Description DQ -owneralias $russmax -contentdatabase $db

After saving this script as newsite.ps1, launch powershell and run the following from the directory where the script resides:

.\newsite.ps1

Also, custom command-lets and functions can be created and used within PowerShell scripts to achieve a more desirable result. Advanced PowerShell scripts for SharePoint 2010 will be available on codeplex soon.

http://www.codeplex.com/SharePointPSScripts/

 

Russ Maxwell, MSFT