Chapter 10 – Administering SP 2010 with Windows PowerShell

Wrox – Prof SP 2010 Administration (P.247)

In total, about 500 powershell commands come with SP 2010 installation. Can be chained together and scripted. Powershell starts to shine when you need to do repeatable processes or multiple tasks.

Powershell in Windows 2008 is version 1.1, and Sharepoint 2010 is version 2. Deactivate version 1.1 before oyu install v.2.

Sharepoint’s Powershell is called : Management Shell.

Example: get all site:
PS H:\> GET-SPSite

Other Powershell tools you can use: On Windows 2008 and R2:
(1) Windows Powershell and
(2) Windows Powershell Integrated Scripting Environment (ISE)

Sharepoint 2010 powershell commands must be run on a SP 2010 server. Not from a client.

Use the command to register powershell with Sharepoitn:

Add-SPSnapIn Microsoft.Sharepoint.PowerShell

get current user’s profile location:
PS H:\> $Profile

Four types of basic commands: (1) cmdlets (2) functions (3) scripts and (4) native commands.

Cmdlets —

cmdlets command lets are compiled commands that are registered with Powershell. The command name is like Verb + Noun, such as:
New-SPWebApplication, or Remove-SPWebApplication

Functions —

Functions can contain one or more commands to accomplish a task, and can accept parameters that allow the creation of custom reusable code. Can be re-used throughout the host’s life session. You can enter functions into your profile so they will be available whenever you start a session.

Scripts —

Scripts = Cmdlets + functions + other things. (with *.SP1 extension) Can be saved to a script library and reused. (Note: Powershell is not associated with *.PS1 extension, for security reasons)

native commands – such as STSADM

Basic Powershell —

Use Get-Command to retrieve a list of available commands, such as
Get-Command –Module Microsoft.Sharepoint.PowerShell return all Sharepoint module commands
Get-Command –noun SPWebApplication
reutrn all commands that has the noun part of “SPWebApplication”
Get-Command –verb Backup
Return all commands that has the verb part of “Backup”

To get help on a command:

Get-help New-SPWebApplication
You can also use –detailed and –examples (or –example) to get the detailsed help.

This command saves all web apps in the variable $WebApps
$WebApps = Get-SPWebApplication

Display number of web apps
$WebApps.Count

The Get-SPWebApplication command doesn’t include the Central Administration web app by default; you have to use this to include it:
-IncludeCentralAdministration

Powershell Pipeline —
How you chain commands together.

Get all SPWebs from all SPSites
Get-SPSite -Limit All | Get-SPWeb – Limit All

Controlling output —

Use [Format-List] or [Format-Table] to control output.
Such as
SP-GetFarm | Format-List or SP-GetFarm | Format-Table

Display defalt is [Format-Table]

Display all properties:
Get-SPFarm | Format-Table *

But this will ruin the output because there are too many and they can’t be displayed in one screen; so choose what you want:
Get-SPFarm |Format-Table –Property DisplayName, Status, BuildVersion

To clear screen, use cls, clear or clear-host.

Working with Farms —

A farm is one or more servers that share the same configuration database.

To get the property of a quried object in one step, us this:
(Get-SPFarm).BuildVersion

Others: Backup-SPFarm, Restore-SPFarm, Get-SPFarmConfig

How to update farm config:
$config = Get-SPFarmConfig
config.WorkflowBatchSize=105
$config | Set-SPFarmConfig

Working with Web Applications —

GET-SPWebApplication

To see a list of all properties, pipe it to Format-List (…. | Format-List)

Get-SPWebApplication http://sharepoint | Get-Member find out what property/method the bject has

Use | More to display information page by page.

To get the ID:
(Get-SPWebApplication http://bruce-pc).ID

Working with Site Collections —

A Sharepoint site collection is SPSite. GET/SET-SPSiteCollectionAdministration allows administrators who does not have access to the site collection to manage certain elements.

Get-SPSite (any GET command will only return 20 items at most). If you want ot return all, add this:

-Limit All

$_ means the current object in the pipeline

Filter:

Get-SPSite –Filter {$_.Owner –eq “Contoso\Administrator”}

-Filter is limited to only some of the properties; use Where-Object instead

Get-SPSite –Limit All | Where-Object {$_.allwebs.Count –eq 1}

backup all site collections of all web aopplications –

Get-SPWebApplication | Get-SPSite | ForEach-Object{$FilePath = “c:\backups\” +

$_.Url.Replace(“http://”,””).Replace(“/”,”-“) + “.bak” ; Backup-SPSite –Identity $_.Url -Path $FilePath}

Add a –WhatIf switch in the end to preview what’s going to happen – so add –WhatIf if you’re doing something that destroys data

Working with Webs —

Site collection contains [webs] or SPWebs. There are no commands for lists, libraries.

To access a single Web Object:
Get-SPWeb http://bruce-pc/sites/Judy/1

Get-SPWebApplication | Get-SPSite | Get-SPWeb | Sort-Object Url | Format-Table Title, Url

Create a new site —
New-SPWeb –Url http://site.contoso.com/teams/IT/SP2010 -Template “STS#1” –Name “SP 2010 Implementation”

You cannot backup or restore an individual web; you can export and import a web.

Working with objects below the Web Level —

SPWeb contains a lingle property: Lists, which is a collection of all lists and libraries

Get all lists/libraries of a web:
(Get-SPWeb http://sharepoint/team/blog).lists

Many of the lower objects, such as SPLIst and SPListItem will not save their changes to the content database until the Update method is called.

To get the third item in the collection —
(Get-SPWeb http://sharepoint/team/blog).lists[2]

Add a “Links” list to the web —
(Get-SPWeb http://sharepoint/team/blog).Lists.Add(“Demo Links List”, “This is the

description”, 103)

To delete a list – et the list id, and then delete it
$listId = (Get-SPWeb http://sharepoint/team/blog).lists[“Demo Links List”].Id

(Get-SPWeb http://sharepoint/team/blog).lists.Delete($listId)

How to track and release objects automatically!!!!
Start-SPAssignment –Global
(…. Do something about the objects here)
Stop-SPAssignment –Global

Or, you can track objects individually, such as
$Webs = $SPAssign
| Get-SPSite http://sharepoint | Get-SPWeb
and in the end:
Stop-$SPAssignment $SPAssign

Advertisements
Post a comment or leave a trackback: Trackback URL.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: