Bill Baer /bɛːr/

Skip to main content


Bill Baer /bɛːr/
Bill Baer is a Senior Product Manager for Microsoft 365 at Microsoft in Redmond, Washington.

PowerShell and SharePoint Automation Examples Part 3

PowerShell and SharePoint Automation Examples Part 3

PowerShell and SharePoint Automation Examples Part 3

  Code Samples Windows Powershell

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


  • Copy the source below into somefile.ps1.
  • Copy the Xml source in DiagnosticsLogging.xml.
  1. 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

              True       True         96     C:SomePath     10  
| | Permalink to this article
Fingerprint for this article411bab7e905e0a809180342622adf571


Skip to footer

Social Links