ENTERPRISE

SharePoint Administration with PowerShell (part 2)

2/8/2011 9:02:01 AM

PowerShell Backup and Restore Options

Backups can now be completely controlled via a series of cmdlets that perform backups of the SharePoint farm, site collection, and configuration database.

Every cmdlet comes with a restore command. SharePoint 2010 also introduced commandlets that provide the ability to export and import site collections, web applications, sites, or lists.

Automating Site Collection Backup and Restore

To perform a backup of an individual site collection, type the following:

Backup-SPSite "http://portal.companyABC.com" –path "C:\Backups\portal.dat" -force

The command in the preceding example backs up a site collection at the given url to the designated path. Before running this cmdlet, make sure this path (for example, C:\Backup) exists. In case there is a previous backup file at this location, use the –force parameter to overwrite an existing backup file.

To restore the site collection, type the following:

Restore-SPSite "http://portal.companyABC.com" –path "C:\Backups\portal.dat" –force –confirm:$false

There are two parameters that must be used for silent site collection restore. -force ensures any existing site collection at http://portal.comapanyabc.com will be overwritten and -confirm:$false suppresses the overwrite confirmation dialog. By suppressing the confirmation, you can easily automate a restore operation.

Exporting SharePoint Sites and Content

In SharePoint 2010, Microsoft introduced a new ability to export and import SharePoint objects. These new operations enable administrators to more easily move content between different site collections. During import and export operations, administrators can control which content will be transferred and how versions will be affected by the move.

Here is a sample for exporting a site:

Export-SPWeb "http://portal.companyABC.com/Team-Site" –path "C:\Exports\Team-Site.dat"

The syntax to export a list is similar, but the list path, relative to site path, must be passed as -ItemUrl parameter:
Export-SPWeb "http://portal.companyABC.com/Team-Site" –path "C:\Exports\Doc-Lib.dat" –ItemUrl "/Doc-Lib"

When exporting lists and libraries, associated workflows and alerts will not be exported along with the content. In addition, item-level permissions will not be maintained.

Sometimes a business might ask for a snapshot of SharePoint for archiving or compliance purposes, and now PowerShell comes to the rescue. The cmdlet Export-SPWeb enables administrators to control versions that will be exported to an export file.

To export content of a document library, use the same code as previously given. By using optional parameter –IncludeVersions, you can choose to export: LastMajor, CurrentVersion, LastMajorAndMinor, or All.

Importing Exported Content

Export operations can be performed via central administration but import is only available via PowerShell. Before importing, you need to create an empty site in your site collection. A new site can be created via UI or PowerShell, but make sure you use the same site template as the site that was exported.

The following command lists all installed site templates:

Get-SPWebTemplate

To create a site with a desired template, use the following cmdlet (in this case, a site based on a blank site template will be created):

New-SPWeb -url "http://portal.companyABC.com/New-Site" -Template "STS#1"

To import a document library, type the following:

Get-SPSite "http://portal.companyABC.com/New-Site" | Import-SPWeb –path "C:\Exports\Doc-Lib.dat" –force –UpdateVersions Overwrite


The preceding example imports a document library that was previously exported to a file. –Force parameter ensures that existing data will be overwritten, and –UpdateVersions instructs command to overwrite all existing versions. There is also an append switch (versions will be appended to existing versions).

Monitoring SharePoint Databases and Site Collection Usage

To monitor SharePoint databases, first use the following commandlet to retrieve the list of databases:

Get-SPWebApplication | Get-SPContentDatabase | Select WebApplication, Name, Server, | Format-Table


The preceding command displays all SharePoint web applications available in this farm, along with their respective database names and SQL server name.

To check the current size (in bytes) of each site collection, use the following cmdlet:

Get-SPSite | Select RootWeb, Url,
@{Name="Size"; Expression={"{0:N2} GB" -f ($_.Usage.Storage/1E9)}},
@{Name="Storage Warning"; Expression={"{0:N2} GB" -f ($_.Quota.StorageWarningLevel/1E9)}},
@{Name ="Storage Max"; Expression={"{0:N2} GB" -f ($_.Quota.StorageMaximumLevel/1E9)}} | ConvertTo-HTML | Out-File "C:\Temp\SiteUsage.html"


The sample might look complicated but actually gets all the site collections and formats the output to make it easy to read. Here are a few notes:

  • SPSite.Usage property is a complex object, and to retrieve storage usage we had to construct an expression.

  • A similar expression was used to retrieve quota properties.

  • An expression also demonstrates PowerShell’s capability to convert results to HTML and save it to a file (ConvertTo-HTML | Out – File).

  • Figure 2 shows sample results that could be retrieved by running such a command in CompanyABC’s farm.

    Figure 2. Displaying results of PowerShell cmdlet in a GridView format.

Set-SPSite also gives you the ability to change quotas for a site collection via PowerShell. To change a quota for a site collection, type this ("My Quota Template" uses the name of an existing quota template available in your farm. A new template can be created via central administration):

Set-SPSite "http://portal.companyABC.com" –QuotaTemplate "My Quota Template"


Managing Content Databases and Site Collections

The built-in cmdlets enable you to manage content databases. If a SharePoint farm is not adequately planned, one of your content databases might grow too large. In such scenarios, you can rely on PowerShell to fix the problem. Type the following command to create a new database:

#$webApplication = Get-SPWebApplication "http://portal.companyABC.com"
#New-SPContentDatabase -Name "WSS_Portal_Content_New" -WebApplication $webApplication

The preceding command will create a new content database for the portal web application. All the other database settings (for example, database server, warning site count, and maximum site count) can be changed at a later stage with the Set-SPContentDatabase cmdlet.

The following example demonstrates how warning and maximum site levels can be changed via PowerShell. Type the following to change the warning and maximum site count to 1000 and 2000, respectively:

Set-SPContentDatabases "WSS_Portal_Content_New" –WarningSiteCount 1000 –MaxSite Count 2000

With multiple databases per web application, you can now easily move site collections between databases. Use the following code (make sure you already have "WSS_Portal_Content_New" in place):
Move-SPSite "http://portal.companyABC.com/sites/sub-site-collection" –
DestinationDatabase "WSS_Portal_Content_New" –confirm:$false

The preceding command moves the site collection at the given URL (“http://portal.companyABC.com/sites/sub-site-collection”) to the new database. If you omit the -confirm parameter, you will be prompted to confirm this operation. As noted in the warning, IIS must be restarted to complete the move. This can be accomplished by using the iisreset command.

Analyzing Events in the Event Log

SharePoint 2010 introduced a number of improvements for easier logging and monitoring for SharePoint farms. One of the crucial improvements is the correlation ID displayed to end users in the browser when an error occurs.

PowerShell comes to the rescue when troubleshooting problems with SharePoint. The most important cmdlet for reviewing events is Get-SPLogEvent. Running this cmdlet without additional parameters might take a while, so it is advisable to add some additional parameters before running this cmdlet. Here are some examples:

# Displays all events that occurred between January 1st and 2nd 2010.
Get-SPLogEvent -StartTime "01/01/2010 00:00" -EndTime "01/02/2010 23:59:59"
# Finds event that occurred between January 1st and 2nd 2010 with the CorrelationID "20ce1e39-5027-4db3-87c2-e0fa17154365"
Get-SPLogEvent -StartTime "01/01/2010 00:00" -EndTime "01/02/2010 23:59:59" | Where {$_.Correlation -eq "20ce1e39-5027-4db3-87c2-e0fa17154365"}

Beside the Get cmdlet, there are also Clear, Merge, and New cmdlets that enable additional operations over logs.

With PowerShell, administrators can change logging level for different event categories:

# Displays logging level for each logging category
Get-SPLogLevel
# Displays logging level for each Access Services categories in a GridView
Get-SPLogLevel | Where {$_.Area –like "*Access Service*"} | Out-GridView
# Changes the least error logging level for "Access Services" to "Error" level
Set-SPLogLevel -Identity "Access Services:*" -EventSeverity Error

Other  
  •  Sharepoint 2007: Approve or Reject a File or List Item
  •  Exchange Server 2007 : Configure the Client Access Server - Enable POP3 and IMAP4
  •  Exchange Server 2007 : Configure the Client Access Server - Enable and Configure Outlook Anywhere
  •  Exchange Server 2007 : Configure the Client Access Server - Create and Apply ActiveSync Mailbox Policies
  •  SharePoint 2010 : Understanding Windows PowerShell Concepts (part 3)
  •  SharePoint 2010 : Understanding Windows PowerShell Concepts (part 2)
  •  SharePoint 2010 : Understanding Windows PowerShell Concepts (part 1)
  •  Managing and Administering SharePoint 2010 Infrastructure : Using Additional Administration Tools for SharePoint
  •  Managing Exchange Server 2010 : Archiving and compliancy (part 3) - Discovery
  •  Managing Exchange Server 2010 : Archiving and compliancy (part 2) - Messaging Records Management
  •  
    Most View
    LINQ to Objects : How to Group Elements (part 5) - Projecting Grouped Elements into a New Type
    AMD Radeon HD 7850 2GB vs. Nvidia GeForce GTX 660 2GB vs. AMD Radeon HD 7870 2GB (Part 2)
    Intex AQUA 5.0 - The Gains Of Deception
    Motorola Moto G Android Smartphone
    iOS 6 Maps - What Went Wrong?
    Photo Editors: From Professional RAW Tools To Simple Library Management (Part 3)
    Programming .NET Components : Working with Threads (part 1)
    LG LM9600 47” Smart TV - A New Direction
    Windows Server 2008 and Windows Vista : Working with GPOs - Group Policy Results
    Android 4.0 Ice Cream Sandwich Guided Tour (Part 2)
    Top 10
    Windows Server 2008 R2 : Active Directory lightweight directory services
    Windows Server 2008 R2 : Active Directory federation services (part 4) - Complete ADFS server configuration
    Windows Server 2008 R2 : Active Directory federation services (part 3) - Install Web agent for claims aware Web application, Configure ADFS certificates
    Windows Server 2008 R2 : Active Directory federation services (part 2) - Set up the ADFS role for the internal and external Active Directory forests
    Windows Server 2008 R2 : Active Directory federation services (part 1) - Planning for Active Directory Federation Services
    Windows 8 : Administering Windows Networking - Troubleshooting networking (part 3) - Using the network troubleshooters, Using command-line tools
    Windows 8 : Administering Windows Networking - Troubleshooting networking (part 2) - View ing Windows 8 network settings
    Windows 8 : Administering Windows Networking - Troubleshooting networking (part 1) - Updating the Task Manager view for networking
    Windows Server 2008 and Windows Vista : Troubleshooting GPOs - Group Policy Troubleshooting Essentials
    Windows Server 2008 and Windows Vista : Creating and Using the ADMX Central Store