sp24s053 introduction to powershell for sharepoint developers and administrators

Post on 24-May-2015

258 Views

Category:

Technology

4 Downloads

Preview:

Click to see full reader

DESCRIPTION

An introduction to the PowerShell scripting language for SharePoint developers and administrators.

TRANSCRIPT

Introduction to PowerShell for SharePoint Developers and

Administrators

Michael BlumenthalPSC Group, LLC

Who is Michael Blumenthal?

• Sr. Solution Architect at PSC Group• CSPUG Co-Leader• INETA Champ 2010-2013• 19 years in IT Consulting• 11 years working with SharePoint

(2003 - 2014)

• 7 years working with PowerShell• Twitter: @MichaelBL

No Compiling

!

No Packagin

g!

Just Code & Go!

Why PowerShell?

PowerShell puts the SharePoint Engine at your fingertips!

• It’s Easy to Get Started!1• Learn the PowerShell

Syntax2

• Real World Examples3

• More Resources4

• Q&A 5

Chapter 1

IT’S EASY TO GET STARTED!

Getting Started with PowerShell

Windows Server 2003• Download

Windows Server 2008• Install

Server2008 R2 +• Run (Add ISE)• Upgrade to V4

PowerShell V3&4 ISE

POSH vs the SharePoint Mgmt Shell

Chapter 2

LEARN THE POWERSHELL SYNTAX!

Learn to use PowerShell with SharePoint!

Symbols & Keywords

Using the SharePoint API

Creating and Running Scripts

Symbols, Keywords, and Syntax! Oh My!

• Variables1

• Commands2

• Piping3

• Comparisons4

• Flow Control5

• Filtering6

Punctuation Pronunciation

Symbol Called Symbol Called

$ Dollar sign, money _ Underscore

# Hash, Pound [ ] Square Brackets

| Pipe, vertical bar . Dot, point, period

{ } Curly braces < > Angle Brackets

“ Double Quote, tick - Dash, hyphen, minus

: Colon % Percent sign

( ) Parentheses ; Semi-colon

+ Plus = Equals, is

! Bang, not /, \ Slash, backslash

1$#|

Variables begin with a $

• Case Insensitive, Dynamic typing

$foo

$true, $false, $profile, $null

$foo = “Hello, World”

1

Commands are called cmdlets.

Verb-Noun

Built-in, ExtensibleGet-Help &

Help

Get-Member

2

The Power of Piping!

Output Of Command

1

Input of Command

2|

3

Example

Making Comparisons4Operator Meaning Operator Meaning

-eq Equals -le Less Than or Equal To

-ne Not Equals -like Wildcard Match

-gt Greater Than

-notlike Not (Wildcard Match)

-ge Greater Than or Equal To

-match Reg. Exp. Match

-lt Less Than -notmatch

Not (Reg. Exp. Match)

Example

Taking Control of the Flow

5

• For (Init;Test;Repeat) {Commands}• for($i=1; $i -le 10; $i++) {Write-Host $i}For

• Foreach (Item in Collection) {Commands}• Foreach ($web in $site.AllWebs) {$web.Title}

ForEach

• If (Test) {Commands} • if ($web.Title –ne “”) {Write-Host $web.Title}

If

• While (Condition){Commands}• while($val -ne 3){$val++; Write-Host $val}While

Example

Where-Object6

•Where {<Test>}

Syntax

• V1&2:Dir | Where {$_.Name –like “B*”}

• V3:Dir | where Name –like B*

Example

Using the SharePoint API

• Getting an SPSite1

• Manipulating It2

• Cleaning Up3

Highlights from the SharePoint Object Model

SPField

SPListItem

SPList

SPWeb

SPWebApplication

SPFarm

SPSite

Loading SharePoint Cmdlets

Even in MOSS 2007:[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

Loading SharePoint DLLs

C:\...\14 or 15\CONFIG\POWERSHELL\Registration\

SharePoint.ps1

A Word About Memory Management

SPWeb SPSite

Inline In Script

Dispose

Chapter 3

REAL WORLD EXAMPLES

Real World Examples

• Check the Farm Version• Check Versioning on all document Libraries• Create List Items• Export Web App Properties to a file• Bulk Site Creation

What’s your Farm Version?

PS C:\Users\Administrator> $(get-SPFarm).BuildVersion

Major Minor Build Revision----- ----- ----- --------14 0 6109 5002

Get a Site and Explore it!

$site = get-spsite http://server/path

THEN$site

Check Doc Lib Versioning Settingsfunction global:show-all-doclibs ($web){$web.Lists | where-object {($_.Hidden -ne $true) -and ($_.BaseType -eq "DocumentLibrary")} }

function global:show-all-doclib-versettings ($web)

{show-all-doclibs ($web) |select-object -property Title, EnableVersioning, MajorVersionLimit, EnableMinorVersions,MajorWithMinorVersionsLimit,forceCheckout}

$site = get-spsite “http://server/path”

show-all-doclib-versettings $site.RootWeb

Practical Uses• Bulk Create Sites1

• List Item CRUD2• Create data for test cases3

• Associate Workflows with a List4

• Work across site collections5

• Deployment Scripting6

• Identify files that won’t upload7

More Practical Uses

• Sync Wep App Properties8

• Install SharePoint9

• Repeatably Manage Content10

• Update Field Definitions11

• Edit MP3 Metadata, Make Flashcards12

Create a List Item

Audio Alerts

• Stick this at the end of your long running script:

$Voice = new-object -com SAPI.SpVoice $Voice.Speak(“Deployment is done!")

Executing Scripts

.\filename.ps1

Set-ExecutionPolicy

Unrestricted

Bulk Site Creation

Site Definitions in V. Studio• Not an answer by

themselves• Define site content• Intended for reuse

Mismatch to one time need• CAML and PITA• Harder: Making it data

driven• Change Site Def ->

Recreate Site

PowerShell & Excel & UI

• Well suited for one time “blow in’s”

• Define the site template in the UI or use standard

• Save as a template Even pub sites - sometimes

• PowerShell has easy loops• Data driven from a CSV• Changes -> Mod Scripts

The PowerShell Solution

• Read the list of sites from CSV• Loop:

Create Site Configure Site

Turn on Features Set Master Pages, Welcome Page Hide Libraries, set versioning Adjust Navigation

Add Lists, Libraries, Pages, Web parts, etc

• Loop again & again if needed – iterative!

Chapter 4

MORE RESOURCES

Script something today!

It’s Easy to Get Started!

Learn & Use the PowerShell

Syntax

More Resources

In Review…

Questions

• Michael BlumenthalSharePoint ArchitectPSC Group, LLC

• MBlumenthal@psclistens.com• psclistens.com• www.cspug.org• Twitter: @MichaelBL• SPYam

Thank you for your time today.

top related