Skip Ribbon Commands
Skip to main content
Sign In
SharePoint Program Manager, Infrastructure
Zach Rosenfield's SharePoint Blog > Posts > SP 2010: Intro to PowerShell Part 4
November 08
SP 2010: Intro to PowerShell Part 4

A quick post today on a rather important new functionality: Server-Side Filtering.  This functionality is all about letting administrators quickly get the sites and site collections they were looking for—something that in STSADM could take hours! 

Before we discuss the new feature, we need to discuss the problem that this is helping to solve.  The root issue is that iterating over every site collection looking at properties could take a really long time.  For example, what if you wanted to enable a new blogging feature on every “blog” site in a very large farm?  Well, first you have to look at all sites that were deployed in the farm to figure out which ones are Blog sites.  With STSADM this would require custom code—but with PowerShell you might say, why wouldn’t the following line work?

PS> Get-SPSite –Limit ALL | Get-SPWeb –Limit ALL |?{ $_.WebTemplate –eq “BLOG#0”}

The answer is: it does, but slowly!  And while it might work fine on a small farm—it will take a very long time on a large farm as you are essentially opening every website in the farm, one at a time.  The better way to do this is to let SQL do the searching for you with Server-Side filtering! While only subsets of object properties are available, these filters make sorting and searching your farm very achievable.  Let’s do some demos of the various types of filters:

1.       Wildcard URLs.  These can be used on the Get-SPSite and Get-SPSiteAdministration* (see NOTE) cmdlets.  Here I am getting all sites under the “My” managed path in the “MySharePoint” web application
PS> Get-SPSiteAdministration “http://MySharePoint/my/*”

2.       Regular Expression URLS  (supported by Get-SPSite and Get-SPSiteAdministration).  By provided this basic Regular Expression in the “identity” field and then giving the RegEx flag I will get all sites named “Demo1” under both the sites and teams managed path. 
PS> Get-SPSite “http://MySharePoint/(sites|teams)/demo1 -RegEx

3.       Filter Parameter for “special” properties (Supported by Get-SPSite, Get-SPSiteAdministration and Get-SPWeb).  First I will get all Site Collections with “Contoso\JDoe” as the owner:
PS> Get-SPSite -Filter {$_.Owner –eq “CONTOSO\JDoe”}

Note that the above command uses the same syntax as “Where-Object”, just it runs these server-side to make them almost instantaneous.  Here’s another example where I solve the original problem we discussed—getting all blog sites in the farm:
PS> Get-SPSite -Limit All | Get-SPWeb -Limit All -Filter {$_.Template -eq "BLOG#0"}


Here’s a real version of the previous examples running in a 2010 farm…



The filter parameter support is as follows:

a.       SPWeb supports:

                                                               i.      Title and Template

b.      SPSite and SPSiteAdministration supports:

                                                               i.      Owner, SecondaryContact, and LockState

Any of the above get commands can be piped to additional cmdlets to perform operations.  Remember to use WhatIf first to make sure you got your filter right!

*NOTE: I used the Site Administration cmdlets in this post—and you might be saying to yourself, what’s this?!  These are cmdlets that allow farm administrators to manage SPSites that they don’t have access to explicitly.  While Get-SPSite will work for all sites, if you (the admin) do not have explicit access to the SPSite then you will be unable to see anything other than the URL of the SPSite.  Also, in this situation all set cmdlets will fail.  The SPSiteAdministration cmdlets will always allow a farm admin to configure some basic properties (owner, quota, etc) on a SPSite.  Remove-SPSite will still work in all cases as this does not require accessing content inside the site collection (spsite and spsiteadministration cmdlets can be piped to each other).  Note that the SPSiteAdministration object is what you’re using managing SPSites in Central Admin—so that’s a good reference for the configurations that can be done

Comments

HOL

Zach

I just downloaded and reviewed your presentation at SPC2009. It was very informative.
How do you recommend starting building PS scripts for 2010?
Should I just wait for the SP 2010 beta to be released?
Any particular development tools?
How about access to a SP 2010 virtual server?

Thanks 
Gilles UrenaNo presence information on 11/11/2009 9:03 AM

Re: HOL

Gilles, the best way to script is on an actual 2010 farm--so wait for the beta and use that. 

As for development tools--none are required. I personally enjoy the PowerGUI Script Editor, but this is purely a personal preference.

Thanks,
Zach RosenfieldNo presence information on 11/17/2009 1:31 PM

Automating PS scripts for demos

Hi in you SPC presentation you mentioned that you were using demo.ps1 so you didn't have to type in your PS demos. That it was a PS script based on someone else's script that you have amended for some of the SharePoint commands. Can you post it up on you blog?
 on 11/24/2009 2:39 PM

stsadm Equivalent directory

I need to change SP2010 to use Kerberos.
Where can I find which PS script to use for the stsadm Equivalent:
STSADM -o Set-ECSsecurity -SSP sharedservices1 -AccessModel Delegation 
and
STSADM -o SetSharedWebServiceAuthN -negotiate
Ofer GalNo presence information on 1/6/2010 1:42 PM

Add Comment

Items on this list require content approval. Your submission will not appear in public views until approved by someone with proper rights. More information on content approval.

Title


Comments *


Name (required) *


Human Test


Checking if you're human: enter "1234" (no quotes)

Attachments