Uncategorized

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.


Instructions




  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

Source


#——————————————————————————-
# 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 )
{
  [Void][System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.Sharepoint”)


  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.”)


  $SPDiagnosticsService.Update()


  $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
  }
}


main

Source Xml


<?xml version=”1.0″ encoding=”utf-8″?>
<Configuration>
  <![CDATA[ SPDiagnosticsService (Microsoft.SharePoint.Administration) ]]>
  <DiagnosticLogging>
    <ErrorReporting>
      <ErrorReportingEnabled>True</ErrorReportingEnabled>
      <ErrorReportingAutomaticUpload>True</ErrorReportingAutomaticUpload>
    </ErrorReporting>
    <LogCutInterval>96</LogCutInterval>
    <LogLocation>C:SomePath</LogLocation>
    <LogsToKeep>10</LogsToKeep>
  </DiagnosticLogging>
</Configuration>

Standard
Uncategorized

Quota Templates and Powershell

Continuing the series using Powershell and Microsoft SharePoint Products and Technologies, this weeks script sample illustrates how Powershell can be leveraged to programmatically provision Quota Templates in Windows SharePoint Services 3.0 and/or Microsoft Office SharePoint Server 2007.

Instructions

  1. Copy the source below into somefile.ps1.
  2. Copy the Xml source in QuotaTemplates.xml.
  3. In the Windows Powershell console call ./somefile.ps1

Source

#——————————————————————————-
# 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 .QuotaTemplates.xml

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

  $cfg.Configuration.QuotaTemplates.QuotaTemplate | ForEach-Object {
    new-QuotaTemplates( $_ )
  }
}

#——————————————————————————-
# Function:     new-QuotaTemplates
#
# Description:     This script enables you to do programmatically what you can do
#         manually through the user interface in the SharePoint Site
#         Management section on the Central Administration Quota
#         Templates page.
#
# Parameters:     None
#——————————————————————————-

function New-QuotaTemplates( [object] $cfg )
{
  [Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Sharepoint")

  $webService = [Microsoft.SharePoint.Administration.SPWebService]::ContentService
  $quota=New-Object Microsoft.SharePoint.Administration.SPQuotaTemplate

  $quota.Name = $cfg.Name
  $quota.StorageMaximumLevel = $cfg.StorageMaximumLevel
  $quota.StorageWarningLevel = $cfg.StorageWarningLevel

  $webService.QuotaTemplates.Add($quota);

  $webService.Update();
}

main

Source Xml

<?xml version="1.0" encoding="utf-8"?>
<Configuration>
  <![CDATA[SPQuotaTemplate (Microsoft.SharePoint.Administration)]]>
  <QuotaTemplates>
    <QuotaTemplate Name="2GB">
      <StorageMaximumLevel>2097152000</StorageMaximumLevel>
      <StorageWarningLevel>1887436800</StorageWarningLevel>
    </QuotaTemplate>
    <QuotaTemplate Name="3GB">
      <StorageMaximumLevel>3145728000</StorageMaximumLevel>
      <StorageWarningLevel>2936012800</StorageWarningLevel>
    </QuotaTemplate>
    <QuotaTemplate Name="4GB">
      <StorageMaximumLevel>4194304000</StorageMaximumLevel>
      <StorageWarningLevel>3670016000</StorageWarningLevel>
    </QuotaTemplate>
    <QuotaTemplate Name="5GB">
      <StorageMaximumLevel>5242880000</StorageMaximumLevel>
      <StorageWarningLevel>4718592000</StorageWarningLevel>
    </QuotaTemplate>
    <QuotaTemplate Name="10GB">
      <StorageMaximumLevel>10485760000</StorageMaximumLevel>
      <StorageWarningLevel>9961472000</StorageWarningLevel>
    </QuotaTemplate>
    <QuotaTemplate Name="100GB">
      <StorageMaximumLevel>104857600000</StorageMaximumLevel>
      <StorageWarningLevel>99614720000</StorageWarningLevel>
    </QuotaTemplate>
  </QuotaTemplates>
</Configuration>

Standard
Uncategorized

Alternate Access Mappings and Windows Powershell

I had a request this week on how to leverage Windows Powershell to add Alternate Access Mappings to the collection for a specific Web application.  Surprisingly after some searching I was unable to find anything existing on the topic, so for those curious see the attached script and corresponding source Xml.

Instructions

  1. Copy the source below into somefile.ps1.
  2. Copy the Xml source in AAM.xml.
  3. In the Windows Powershell console call ./somefile.ps1.

Source

#——————————————————————————-
# 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 .AAM.xml

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

  $cfg.Configuration.WebApplication | ForEach-Object {
    new-SPAlternateUrl( $_ )
  }
}

#——————————————————————————-
# Function:     New-SPAlternateURL
#
# Description:     This script adds the specified URLs to the collection of
#        alternate request URLs for the Web application.
#
# Parameters:    None
#——————————————————————————-

function New-SPAlternateURL( [object] $cfg )
{
  [Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Sharepoint")

  $webApp = $nul;
  $webApp = [Microsoft.SharePoint.Administration.SPWebApplication]::Lookup($cfg.Url)
  trap [Exception] { 
      Write-Host
    Write-Error $("Exception: " + $_.Exception.Message);
    continue;
  }

  $cfg.AlternateUrl | ForEach-Object {
  $map=New-Object Microsoft.SharePoint.Administration.SPAlternateUrl($_.IncomingUrl, $_.UrlZone)
  $webApp.AlternateUrls.Add($map)
  return $map
  }

main

Source Xml

<?xml version="1.0" encoding="utf-8"?>
<Configuration>
  <WebApplication Url="http://contoso">
    <AlternateUrl IncomingUrl="http://www.contoso.com" UrlZone="Internet" />
    <AlternateUrl IncomingUrl="http://contoso:1234" UrlZone="Custom" />
    <AlternateUrl IncomingUrl="http://contoso:4321" UrlZone="Custom" />
  </WebApplication>
</Configuration>

Standard
Uncategorized

Scripting SharePoint with Powershell

C# has long been the developers preference when working with Microsoft SharePoint Products and Technologies; however, since the release of Microsoft Office SharePoint Server 2007/Windows SharePoint Server 3.0 Powershell has become more widely used to support the automation of common administrative tasks – though C# is and will long be integral in application development, Powershell provides both simplicity and flexibility for the automation of routine tasks.  For example, let’s assume an administrator would like to programmatically provision a Web application outside of the SharePoint administration tool and/or the SharePoint 3.0 Central Administration user interface.


In this example (C#) you can reference the Microsoft.SharePoint.Administration Namespace and call the SPWebApplicationBuilder class to create a new SPWebApplication object.


static void Main(string[] args)
{
    SPWebApplicationBuilder pWebApp = new SPWebApplicationBuilder(SPFarm.Local);
    int iPort = 80;
    pWebApp.Port = iPort;
    SPWebApplication WebApplication = pWebApp.Create();

    WebApplication.Provision();

    SPSite SiteCollection = WebApplication.Sites.Add(“/”, “contoso\wbaer”, “wbaer@contoso.com”);

    SiteCollection.Close();
}

Using Powershell, the underlying concept remains the same with reduction in overall code:


[system.reflection.assembly]::LoadWithPartialName(“Microsoft.Sharepoint”)
$webAppBuilder=new-object Microsoft.SharePoint.Administration.SPWebApplicationBuilder( [Microsoft.SharePoint.Administration.SPFarm]::Local)
$webAppBuilder.Port=80
$webApp=$webAppBuilder.Create()
$webApp.Provision()
$webApp.Sites.Add(“/”,”contosowbaer”,
wbaer@contoso.com)


This code snippet is provided under the Microsoft Permissive License.


While Powershell does not provide specific out-of-the-box functionality for Microsoft SharePoint Products and Technologies as you can see from the example code above, the scripting possibilities are open through native access to .NET objects.  If you read an article, for example, Powerful Command Line Administration for SharePoint in the January 2007 issue of TechNet Magazine you will immediately find areas where common administrative functions can be scripted with Powershell reducing the operational overhead associated with managing a Microsoft SharePoint Products and Technologies deployment.


So what exactly is Powershell?


Powershell is a command line shell and scripting language and as such provides the ability to accelerate automation through system administration utilities, consistent syntax, naming conventions, .NET and COM integration, etc.


Where can I learn more about Powershell?


Windows Powershell Home


Windows Powershell: Frequently Asked Questions


Windows Powershell Newsgroup


Windows Powershell Team Blog


Windows Powershell Quick Reference

Standard