Wrox Real World Sharepoint 2010 – Chapter 5

[Chapter 5: Using PowerShell with SharePoint 2010]

PowerShell shortcut property:

C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe  -NoExit  ” & ‘ C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\CONFIG\POWERSHELL\Registration\\sharepoint.ps1

sharepoint.ps1:

$ver = $host | select version

if ($ver.Version.Major -gt 1) {$Host.Runspace.ThreadOptions = “ReuseThread”}

Add-PsSnapin Microsoft.SharePoint.PowerShell

Set-location $home

To explicit declare a type for a cmdlet variable, such as:

$farm=Get-SPFarm,

you can do this:

[Microsoft.Sharepoint.Administration.SPFarm]$farm=Get-SPFarm

** Object pipeline

e.g., C:> Get-SPWeb http://portal/* | where {$_MasterUrl -like “*/v4.master”}}

$_ means the current object in the script block. How do you kow what parameters can be piped into a cmdlet? use “help Get-SPWeb -full”.

You will see Accept Pipeline Input and Accept Pipeline input is True/False.

– Filter:
Get-SPSite – Limit All -Filter {$_Owner – eq “wrox/spadmin”}

Loop:

C:\> 1..10 | ForEach-Object {
if ($- -eq 5) {continue}
Write-Host $_
}

you get:
1
2
3
4

foreach ($i in 1..10) {
if ($i -eq 5) {continue}
Write-Host $i
}

you get:
1
2
.
.
9
10

Functions and Scripts
function New-ManagedAccount {
$cred=Get-Credential “wrox\spfarm”
return New-SPManagedAccount -Credential #cred
}

To load a script: (*.ps1)
C:\> . .\Functions.ps1

To accept parameters:
function New-ManagedAccount ([string]$accountname) {
$cred=Get-Credential $accountname
return New-SPManagedAccount -Credential #cred
}

Check if a variable if NULL:

if ($account -eq $null)

Get-Command:
Get-Command -pssnapin Microsoft.Sharepoint.Powershell
returns all sharepoint powershell commands

Get-Command -noun SPService* | sort noun | ft name
returns all commands with “SPService” in name, sorted by name, and return name only.

To see members (properties) of an object:
Get-SPSite http://* | Get-Member

Start- and Stop-SPAssignment – used to dispose of objects after using them.

E.g.,: (Simple)
Start-SPAssignment -Global
blah
blah
Stop-SPAssignment

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: