59
Introduction to PowerShell for SharePoint Developers and Administrators Michael Blumenthal PSC Group, LLC

Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

Embed Size (px)

DESCRIPTION

An introduction to PowerShell v3 for SharePoint Developers and Administrators. Given at SharePoint Fest Chicago 2013, session SIA101.

Citation preview

Page 1: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

Introduction to PowerShell for SharePoint Developers and

Administrators

SIA 101

Michael BlumenthalPSC Group, LLC

Page 2: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

2

Who is Michael Blumenthal?

Sr. Solution Architectat PSC Group

CSPUG Co-LeaderINETA Champ 2010-201318 years in IT Consulting10 years working with

SharePoint (2003,2007,2010, 2013)

Page 3: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

3

This is about you

Version of SharePoint?Admin, Developer, Both, Other?PowerShell experience?SharePoint experience?Unix experience?Scripting experience?

Page 4: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

4

No Compiling!

No Packaging!

Just Code & Go!

Why PowerShell?

Page 5: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

5

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 & Raffle5

Page 6: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

6

Chapter 1

IT’S EASY TO GET STARTED!

Page 7: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

Getting Started with PowerShell

Windows Server 2003• Download

Windows Server 2008• Install

Server2008 R2, 2012, Win8• Run (Add ISE)

Page 8: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

8

Page 9: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

9

Page 10: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

10

V2

Page 11: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

11

PowerShell V3 ISE

Page 12: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

12

POSH vs the SharePoint Mgmt Shell

Page 13: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

13

Chapter 2

LEARN THE POWERSHELL SYNTAX!

Page 14: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

Learn to use PowerShell with SharePoint!

Symbols & Keywords

Using the SharePoint API

Creating and Running Scripts

Page 15: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

15

Symbols, Keywords, and Syntax! Oh My!

• Variables1• Commands2• Piping3• Comparisons4• Flow Control5• Filtering6

Page 16: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

16

Punctuation PronunciationSymbol 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 17: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

17

Variables begin with a $

• Case Insensitive, Dynamic typing

$foo

$true, $false, $profile, $null

$foo = “Hello, World”

1

Page 18: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

18

Page 19: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

19

Commands are called cmdlets.

Verb-Noun

Built-in, Extensible

Get-Help & Help

Get-Member

2

Page 20: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

20

Page 21: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

21

The Power of Piping!

Output Of Command 1

Input of Command 2|

3

Page 22: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

Example

Page 23: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

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 24: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

24

Example

Page 25: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

25

Taking Control of the Flow5

• 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 26: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

Example

Page 27: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

27

Where-Object6

•Where {<Test>}Syntax

• V1&2:Dir | Where {$_.Name –like

“B*”}• V3:Dir | where Name –like B*

Example

Page 28: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

28

Using the SharePoint API

• Getting an SPSite1• Manipulating It2• Cleaning Up3

Page 29: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

29

Highlights from the SharePoint Object Model

SPField

SPListItem

SPList

SPWeb

SPWebApplication

SPFarm

SPSite

Page 30: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

30

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 31: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

31

A Word About Memory Management

SPWeb SPSite

Inline In Script

Dispose

Page 32: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

32

Page 33: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

33

Chapter 3

REAL WORLD EXAMPLES

Page 34: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

34

Real World Examples

Check the Farm VersionCheck Versioning on all document LibrariesCreate List ItemsExport Web App Properties to a fileBulk Site Creation

Page 35: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

35

What’s your SP2010 Version?

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

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

Page 36: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

36

Get a Site and Explore it!

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

THEN$site

Page 37: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

37

Page 38: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

38

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 39: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

39

Page 40: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

40

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 41: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

41

More Practical Uses• Sync Wep App Properties8• Install SharePoint9• Repeatably Manage Content10• Update Field Definitions11• Edit MP3 Metadata, Make Flashcards12

Page 42: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

42

What’s your SharePoint Version?

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

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

Page 43: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

43

Create a List Item

Page 44: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

44

Audio Alerts

Stick this at the end of your long running script:

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

Page 45: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

45

Executing Scripts

.\filename.ps1

Set-ExecutionPolicy Unrestricted

Page 46: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

46

Bulk Site Creation

Site Definitions in V. StudioNot an answer by themselvesDefine site contentIntended for reuse

Mismatch to one time needCAML and PITAHarder: Making it data drivenChange Site Def -> Recreate Site

PowerShell & Excel & UI

Well suited for one time “blow in’s”Define the site template in the UI or use standardSave as a template

Even pub sites - sometimesPowerShell has easy loopsData driven from a CSVChanges -> Mod Scripts

Page 47: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

47

The PowerShell Solution

Read the list of sites from CSVLoop:

Create SiteConfigure Site

Turn on FeaturesSet Master Pages, Welcome PageHide Libraries, set versioningAdjust Navigation

Add Lists, Libraries, Pages, Web parts, etcLoop again & again if needed – iterative!

Page 48: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

48

Chapter 4

MORE RESOURCES

Page 49: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

Resources

Books & Blogs

People & Places

Page 51: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

51

Page 52: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

52

Page 53: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

53

Page 54: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

54

Page 55: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

55

Page 56: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

56

JEFF HICKS

Page 57: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

57

Resources SummaryMSFT

PowerShell Product Team Blog Script Center

CommunityVarious BooksCodePlex: PSBBs (mine), CodePlex:SPInstaller Blog.BlumenthalIT.NetSharePointJack.comJeff Hicks , Gary LaPointe, Raymond Mitchell, Todd Klindt, POSHCODE.ORG, get-spscripts.com SPYam

Page 58: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

58

Script something today!

It’s Easy to Get Started!

Learn & Use the PowerShell Syntax

More Resources

In Review…

Page 59: Introduction to PowerShell for SharePoint Admins and Developers - SharePoint Fest Chicago 2013 SIA101

59

Questions& Raffle

• Michael BlumenthalSharePoint ArchitectPSC Group, LLC

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

Thank you for your time today.