12/04/2012

SharePoint 2007 PowerShell cmdlets

 

This article includes the 2007 functions to emulate the SharePoint 2010 cmdlets used in my series of articles in SharePoint Pro Magazine. The first article, Exploring and Inventorying SharePoint Using PowerShell, is here: http://www.sharepointpromag.com/article/sharepoint-server-2010/exploring-inventorying-sharepoint-using-powershell-144834 . Part 2 is here: http://sharepointpromag.com/sharepoint/windows-powershell-scripts-sharepoint-info-files-pagesweb-parts with part 3 to follow.

Check back over the next week or two as I expand this content and add links to download the PowerShell module…

While this little project will in no way include all of the 500 SharePoint 2010 cmdlets, it will provide a core set of cmdlets needed to run my auditing and inventorying SharePoint 2010 PowerShell scripts. These cmdlets are implemented as PowerShell functions so you can study them and enhance them or even use them as models to create additional cmdlets. There will also be links to a download you can simply load them as a PowerShell module.

 

As a teaser… here's a starter set…   (check back in a few days for the full article)

Save the following as SP2007cmdlets.psm1:

#Notes:
# The scripts below are from http://TechTrainingNotes.blogspot.com 
# These scripts may be used in your projects but may not be republised in any form
# Formatting data found in SP2007cmdlets.format.ps1xml  (optional)

function Get-SP2007help
{
"Currently implemented cmdlets:"
"  Get-SPFarm"
"  Get-SPWebApplication"
"  Get-SPSite"
"  Get-SPWeb"
"  Get-SP"
}

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")


function Get-SPFarm()
{
  [Microsoft.SharePoint.Administration.SPFarm]::Local
}


function Get-SPWebApplication ($url)
{
  begin
  {  $foundit=$false
     if ($url -ne $null) { 
       $foundit=$true; 
       [Microsoft.SharePoint.Administration.SPWebApplication]::Lookup("$url") }
  }
  process
  {
  }
  end
  {
    if ($foundit -eq $false)
    {
       [Microsoft.SharePoint.Administration.SPWebService]::ContentService.WebApplications
    }
  }
}



function Get-SPSite ([string]$Identity, [string]$ContentDatabase)
{
<#
.SYNOPSIS
 Returns all site collections that match the given criteria.
.PARAMETER computername
  The computer name to query. Just one.
.DESCRIPTION
 The Get-SPSite cmdlet returns either a single site that matches the Identity parameter, or all the sites that match the Filter parameter for the specified scope. The scopes are the WebApplication, ContentDatabase , and SiteSubscription parameters. If none of these scopes is provided, the scope is the farm. If the scope is specified with parameter, all sites in that scope are returned.
.PARAMETER Identity 
 Specifies the URL or GUID of the site collection to get. The type must be a valid URL, in the form http://server_name or http://server_name/sites/sitename, or a valid GUID (for example, 12345678-90ab-cdef-1234-567890bcdefgh).
.PARAMETER ContentDatabase
Specifies the GUID of the content database from which to list site collections.

The type must be a valid database name, in the form  SPContentDB01, or a valid GUID (for example, 12345678-90ab-cdef-1234-567890bcdefgh).
#>

  begin
  {  $foundit=$false
     if ($Identity -ne "" -or $ContentDatabase -ne "") { 
       $foundit=$true;
       if ($ContentDatabase -ne "")
       {
         if(Test-TTNIsGuid($ContentDatabase))
         {
          Get-SPWebApplication | select -ExpandProperty ContentDatabases | where { $_.id -eq "$ContentDatabase"} | get-spsite
         }
         else 
         { 
          Get-SPWebApplication | select -ExpandProperty ContentDatabases | where { $_.name -eq "$ContentDatabase"} | get-spsite
         }
       }
       else
       {
         if ($Identity.toLower().StartsWith("http"))
           { New-Object Microsoft.SharePoint.SPSite("$Identity") }
         else 
           { New-Object Microsoft.SharePoint.SPSite([guid]"$Identity") }
       }
     }
  }
  process
  {
    if ($_ -ne $null)
    {
      $foundit=$true;  $_.Sites
    }
  }
  end
  {
    if ($foundit -eq $false)
    {
       Get-SPWebApplication | Get-SPSite
    }
  }
}


function Get-SPWeb ($url)
{
  begin
  {
     if ($url -ne $null) { (New-Object Microsoft.SharePoint.SPSite("$url")).OpenWeb() }
  }
  process
  {
     $_.AllWebs
  }
}


function Test-TTNIsGuid ($guid)
{
  # TechTrainingNotes helper function
  ($guid -match "^[A-Fa-f0-9]{32}$|({|\()?[A-Fa-f0-9]{8}-([A-Fa-f0-9]{4}-){3}[A-Fa-f0-9]{12}(}|\))?$|^({)?[0xA-Fa-f0-9]{3,10}(, {0,1}[0xA-Fa-f0-9]{3,6}){2}, {0,1}({)([0xA-Fa-f0-9]{3,4}, {0,1}){7}[0xA-Fa-f0-9]{3,4}(}})$")
}

Save the following as SP2007cmdlets.format.ps1xml: 
( file to be posted with the complete article )
 
. 

3 comments:

Unknown said...

Hi Mike this is a very cool resource thanks for taking the time to post this. I would be interested in learning how to inventory different type of sharepoint site objects... like lists. How many lists in the farm and how big are they? Can you provide any pointers on how this can be done wuth Powershell in sharepoint 2007?

Mike Smith said...

Tristan,

Getting a list of lists is easy to do. When you say "how big are they" are you asking about the number of items in the list or the number of bytes needed to store the list. The number of items is easy.

Using the functions in the article above (or the built-in cmdlets for SP2010):

Get-SPSite | Get-SPWeb | Select -ExpandProperty Lists | Select ParentWebUrl, Title, {$_.Items.Count}

Note, if using the SP2010 cmdlets remember to add -Limit All:

Get-SPSite -Limit All | Get-SPWeb -Limit All | Select -ExpandProperty Lists | Select ParentWebUrl, Title, {$_.Items.Count}


The next article in SharePoint Pro will include examples like these: listing all libraries, their file counts, and total file size or finding all libraries that use a specific content type; or finding all customized (unghosted) pages or all pages that use a selected Web Part.

Mike

Larry W. Virden said...

1. There was a comment about finding a psmxml file posted with the article, but I could not find said file - is it available online somewhere? And how is it used?

2. I created the 2007SPcmdlets.psm1 file as you indicated in your blog. However, it was not clear to me how to go about reading it into my session or script so that I could use the new functions.

Is this the topic of a different article?

Thank you so much for the helpful articles. I really am interesting in learning how to use powershell with sharepoint.

Note to spammers!

Spammers, don't waste your time... all posts are moderated. If your comment includes unrelated links, is advertising, or just pure spam, it will never be seen.