+ All Categories
Home > Technology > SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators

SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators

Date post: 24-May-2015
Category:
Upload: michael-blumenthal
View: 250 times
Download: 4 times
Share this document with a friend
Description:
An introduction to the PowerShell scripting language for SharePoint developers and administrators.
Popular Tags:
54
Introduction to PowerShell for SharePoint Developers and Administrators Michael Blumenthal PSC Group, LLC
Transcript
Page 1: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators

Introduction to PowerShell for SharePoint Developers and

Administrators

Michael BlumenthalPSC Group, LLC

Page 2: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators

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

Page 3: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators

No Compiling

!

No Packagin

g!

Just Code & Go!

Why PowerShell?

Page 4: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators

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

Page 5: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators

Chapter 1

IT’S EASY TO GET STARTED!

Page 6: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators

Getting Started with PowerShell

Windows Server 2003• Download

Windows Server 2008• Install

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

Page 9: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators

PowerShell V3&4 ISE

Page 10: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators

POSH vs the SharePoint Mgmt Shell

Page 11: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators

Chapter 2

LEARN THE POWERSHELL SYNTAX!

Page 12: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators

Learn to use PowerShell with SharePoint!

Symbols & Keywords

Using the SharePoint API

Creating and Running Scripts

Page 13: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators

Symbols, Keywords, and Syntax! Oh My!

• Variables1

• Commands2

• Piping3

• Comparisons4

• Flow Control5

• Filtering6

Page 14: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators

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$#|

Page 15: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators

Variables begin with a $

• Case Insensitive, Dynamic typing

$foo

$true, $false, $profile, $null

$foo = “Hello, World”

1

Page 17: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators

Commands are called cmdlets.

Verb-Noun

Built-in, ExtensibleGet-Help &

Help

Get-Member

2

Page 19: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators

The Power of Piping!

Output Of Command

1

Input of Command

2|

3

Page 20: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators

Example

Page 21: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators

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)

Page 22: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators

Example

Page 23: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators

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

Page 24: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators

Example

Page 25: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators

Where-Object6

•Where {<Test>}

Syntax

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

• V3:Dir | where Name –like B*

Example

Page 26: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators

Using the SharePoint API

• Getting an SPSite1

• Manipulating It2

• Cleaning Up3

Page 27: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators

Highlights from the SharePoint Object Model

SPField

SPListItem

SPList

SPWeb

SPWebApplication

SPFarm

SPSite

Page 28: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators

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

Page 29: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators

A Word About Memory Management

SPWeb SPSite

Inline In Script

Dispose

Page 31: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators

Chapter 3

REAL WORLD EXAMPLES

Page 32: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators

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

Page 33: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators

What’s your Farm Version?

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

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

Page 34: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators

Get a Site and Explore it!

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

THEN$site

Page 36: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators

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

Page 38: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators

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

Page 39: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators

More Practical Uses

• Sync Wep App Properties8

• Install SharePoint9

• Repeatably Manage Content10

• Update Field Definitions11

• Edit MP3 Metadata, Make Flashcards12

Page 40: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators

Create a List Item

Page 41: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators

Audio Alerts

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

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

Page 42: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators

Executing Scripts

.\filename.ps1

Set-ExecutionPolicy

Unrestricted

Page 43: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators

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

Page 44: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators

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!

Page 45: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators

Chapter 4

MORE RESOURCES

Page 53: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators

Script something today!

It’s Easy to Get Started!

Learn & Use the PowerShell

Syntax

More Resources

In Review…

Page 54: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators

Questions

• Michael BlumenthalSharePoint ArchitectPSC Group, LLC

[email protected]• psclistens.com• www.cspug.org• Twitter: @MichaelBL• SPYam

Thank you for your time today.


Recommended