PowerShell and SharePoint Automation Examples Part 3

Automating your Microsoft SharePoint Products and Technologies installations can provide both consistency and efficiency while reducing the potential for human error.  This is the third post on the subject of leveraging PowerShell to provide configuration logic for your environment(s).  In this example we cover the configuration of Diagnostic Logging through PowerShell and a source Xml manifest.  For previous examples see http://blogs.technet.com/wbaer/archive/tags/Powershell/default.aspx.


  1. Copy the source below into somefile.ps1.
  2. Copy the Xml source in DiagnosticsLogging.xml.

  3. In the Windows Powershell console call ./somefile.ps1


# Function: Main
# Description: Main entry point for the script.  Loads the configuration source
#  Xml and initializes the foreach loop to iterate over a
#   collection of Xml nodes.
# Parameters: None

function Main()
  [xml]$cfg = Get-Content .DiagnosticLogging.xml

  if( $? -eq $false ) {
    Write-Host “Cannot load configuration source Xml $cfg.”
    return $false

  $cfg.Configuration.DiagnosticLogging | ForEach-Object {
    new-DiagnosticLogging( $_ )

# Function:  new-DiagnosticLogging
# Description:  This script enables you to do programmatically what you can do
#   manually through the user interface in the Logging and
#   Reporting section on the Central Administration Diagnostics
#   Logging page.
# Parameters:  None

function new-DiagnosticLogging( [object] $cfg )

  if (!(Test-Path -Path $cfg.LogLocation))
    New-Item $cfg.LogLocation -type directory

  $SPDiagnosticsService = [Microsoft.SharePoint.Administration.SPDiagnosticsService]::Local
  $SPDiagnosticsService.LogLocation = $cfg.LogLocation
  $SPDiagnosticsService.LogsToKeep = $cfg.LogsToKeep
  $SPDiagnosticsService.LogCutInterval = $cfg.LogCutInterval

  Write-Host ([Environment]::NewLine+”DiagnosticsService.PS1 is making the requested changes.  This may take several minutes to complete.”)


  $ErrorReporting = [Microsoft.SharePoint.Administration.SPWebService]::AdministrationService
  if( $cfg.ErrorReportingEnabled.ToString().ToLower() -eq “true” ) {
    $ErrorReporting.Farm.ErrorReportingEnabled = $true
  elseif( $cfg.ErrorReportingEnabled.ToString().ToLower() -eq “false” ) {
    $ErrorReporting.Farm.ErrorReportingEnabled = $false

  if( $cfg.ErrorReportingAutomaticUpload.ToString().ToLower() -eq “true” ) {
    $ErrorReporting.Farm.ErrorReportingAutomaticUpload = $true
  elseif( $cfg.ErrorReportingAutomaticUpload.ToString().ToLower() -eq “false” ) {
    $ErrorReporting.Farm.ErrorReportingAutomaticUpload = $false


Source Xml

<?xml version=”1.0″ encoding=”utf-8″?>
  <![CDATA[ SPDiagnosticsService (Microsoft.SharePoint.Administration) ]]>

Conference season is upon us…

Conference season is upon us and it’s gearing up to be a great way to close out the year – join me at the 2009 Microsoft SharePoint Conference in Las Vegas at the Mandalay Bay Convention Center October 19th through October 22nd, 2009 or for those locally and can’t make the conference join me at the 2009 Pass Community Summit with Burzin Patel November 2nd through November 5th, 2009 at the Washington State Convention and Trade Center in Seattle.  For those enrolled in the November Office SharePoint Server 2007 Microsoft Certified Masters rotation, congratulations and you’ll be seeing more of me ;-), and some time in between hosting a Windows 7 Launch Party!  Finally there may be something in between as well as a Spring 2010 SoCal trip.


Performance Tuning Microsoft SharePoint Products and Technologies (SQL Layer)

Business Continuity Management (HA/DR)

Windows 7

and more…

Developing SharePoint Applications

Do you have questions or are you looking for best practices on integrating line of business applications with Microsoft SharePoint Products and Technologies, taking advantage of publishing and content oriented capabilities, or creating collaborative interactions around business processes?  If you answered yes to any of these questions, I’d recommend reading the just published content, Developing SharePoint Applications, in the Patterns and Practices Developer Center

This guidance will help developers and architects accelerate construction of advanced applications through both examples and documentation, ensures alignment with recommendations and best practices and compliments existing platform documentation.

Related Resources

Microsoft Office SharePoint Server Developer Center

Windows SharePoint Services 3.0 SDK

SharePoint Server 2007 SDK