Archive for the ‘PowerShell’ Category

PowerShell SharePoint Site Collection Backup Script March 14th, 2010

Mike

I have been meaning to write a script in PowerShell to do SharePoint Site Collection backups for a while but not got round to it. I did do a quick and functional one at the tail end of last year, but it wasn't particularly elegant. This script I am much happier with: it will backup all Site Collections at the specified Web Application and place them at the given location in a folder constructed from the day's date. It also writes to a log file and can clean up folders containing backups that are older than a predetermined number of days.

Simply copy the script below and save it to a .ps1 file of your choice. Please retain my comments at the top and, as always, use at your own risk! The deletion portion is quite destructive :) . You may also have to set your Execution Policy to a less strict setting, such as RemoteSigned, e.g.:


Set-ExecutionPolicy RemoteSigned

I have tested the script with Microsoft Office SharePoint Server 2007 SP2 and Windows SharePoint Services 3.0 SP2 running on Windows Server 2003 R2 SP2 (PowerShell 1.0). It should work fine for MOSS and WSS running on different Windows Server operating systems.

Here is the script:

# SharePoint Site Collection Backup Script
# Author: Michael Cox
# Version: 1.0
# Date: March 2010
# Contact: http://www.betteritsolutions.co.uk/
# Use at own risk, please retain these comments

# Create alias for STSADM
Set-Alias -Name exeStsadm -Value ($Env:CommonProgramFiles + "\Microsoft Shared\Web Server Extensions\12\BIN\stsadm.exe") -Option Constant
# Specify maximum age in days for backup files
New-Variable -Name maxFileAge -Value 14 -Option Constant
# Get todays date, formatting as yyyyMMdd e.g. 20100224
New-Variable -Name todaysDate -Value (Get-Date -Format yyyyMMdd) -Option Constant
# Specify backup location and set log file location, folder must exist and be writable by account running script
New-Variable -Name backupLoc  -Value "S:\Backups" -Option Constant
New-Variable -Name logFileLoc -Value "$backupLoc\site_collection_backups.log" -Option Constant
# Specify Web Application
New-Variable -Name webApplication -Value "http://mossintranet/" -Option Constant

# Change to backup location and create folder, suppressing notification
Set-Location $backupLoc
New-Item $todaysDate -type directory | Out-Null
Set-Location $todaysDate

# Record backup start
Out-File -FilePath $logFileLoc -Append -InputObject $("Backup started at: " + (Get-Date -Format HH:mm` dd/MM/yyyy) + "`n")

# Enumerate Site Collections within chosen Web Application and store in a variable
[xml]$sitesXml = exeStsadm -o enumsites -url $webApplication

# Enumerates each site collection and performes an stsadm sitecollection backup against each site
$sitesXml.Sites.Site | ForEach-Object -Process {$backupFile = $_.Url.Replace('http://','').Replace('/','_') + ".bak"; exeStsadm -o backup -url $_.Url -filename $backupFile; Write-Host "Finished writing: $backupFile"; Out-File -FilePath $logFileLoc -Append -InputObject "Finished writing: $backupFile"}

# Remove folders that are older than the maxFileAge value
Set-Location $backupLoc
$tooOld = (Get-Date).addDays(-$maxFileAge)
Get-ChildItem | Where-Object {$_.PSIsContainer -and ($_.lastWriteTime -le $tooOld)} | ForEach-Object -Process {Remove-Item $_ -force -recurse; Write-Host "Folder $_ was deleted."; Out-File -FilePath $logFileLoc -Append -InputObject "Folder $_ was deleted."}

# Record backup end
Out-File -FilePath $logFileLoc -Append -InputObject $("`n" + "Backup ended at: " + (Get-Date -Format HH:mm` dd/MM/yyyy) + "`n")

Here is a screenshot of files created following a backup of a Web Application with two Site Collections and also the contents of the log file:

Screenshot after running script

Screenshot after running script

Please let me know if this is useful and also feel free to make suggestions for improvements or bug fixes, if you find any.

Continue reading...


 

Install PowerShell on Windows Server 2008 R2 Core November 18th, 2009

Mike

Today I installed Windows Server 2008 R2 Standard Core, expecting to be presented with PowerShell. Wrong. I was presented with good old cmd.exe. It took me a while to find this information, so I thought I'd blog it here. To install PowerShell, you need to run the following commands:


DISM /Online /Enable-Feature /FeatureName:NetFx2–ServerCore
DISM /Online /Enable-Feature /FeatureName:MicrosoftWindowsPowerShell

Now you can launch powershell.exe from C:\Windows\System32\WindowsPowerShell\v1.0. Although the path would suggest PowerShell 1.0, a quick Get-Host indicates PowerShell 2.0:

Get-Host output

Get-Host output

A little more about DISM, for those that are interested. DISM is the "Deployment Image Servicing and Management tool" and can be used to perform various configuration tasks on offline or running images. Hence the /Online switch is used to tell DISM it must target the running operating system. The following command will list all features and their status (suggest you pipe it through more):


DISM /Online /Get-Features | more

Note that PowerShell requires .NET Framework 2.0, hence the first DISM command.

Thanks to Vincent Hu for his post on TechNet, which I finally found. There is a slight mistake, though, as there should be no space between /FeatureName: and the feature you want to install.

Continue reading...