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

Preview:

DESCRIPTION

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

Citation preview

Introduction to PowerShell for SharePoint Developers and

Administrators

SIA 101

Michael BlumenthalPSC Group, LLC

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)

3

This is about you

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

4

No Compiling!

No Packaging!

Just Code & Go!

Why PowerShell?

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

6

Chapter 1

IT’S EASY TO GET STARTED!

Getting Started with PowerShell

Windows Server 2003• Download

Windows Server 2008• Install

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

8

9

10

V2

11

PowerShell V3 ISE

12

POSH vs the SharePoint Mgmt Shell

13

Chapter 2

LEARN THE POWERSHELL SYNTAX!

Learn to use PowerShell with SharePoint!

Symbols & Keywords

Using the SharePoint API

Creating and Running Scripts

15

Symbols, Keywords, and Syntax! Oh My!

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

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

17

Variables begin with a $

• Case Insensitive, Dynamic typing

$foo

$true, $false, $profile, $null

$foo = “Hello, World”

1

18

19

Commands are called cmdlets.

Verb-Noun

Built-in, Extensible

Get-Help & Help

Get-Member

2

20

21

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)

24

Example

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

Example

27

Where-Object6

•Where {<Test>}Syntax

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

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

Example

28

Using the SharePoint API

• Getting an SPSite1• Manipulating It2• Cleaning Up3

29

Highlights from the SharePoint Object Model

SPField

SPListItem

SPList

SPWeb

SPWebApplication

SPFarm

SPSite

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

31

A Word About Memory Management

SPWeb SPSite

Inline In Script

Dispose

32

33

Chapter 3

REAL WORLD EXAMPLES

34

Real World Examples

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

35

What’s your SP2010 Version?

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

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

36

Get a Site and Explore it!

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

THEN$site

37

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

39

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

41

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

42

What’s your SharePoint Version?

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

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

43

Create a List Item

44

Audio Alerts

Stick this at the end of your long running script:

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

45

Executing Scripts

.\filename.ps1

Set-ExecutionPolicy Unrestricted

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

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!

48

Chapter 4

MORE RESOURCES

Resources

Books & Blogs

People & Places

51

52

53

54

55

56

JEFF HICKS

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

58

Script something today!

It’s Easy to Get Started!

Learn & Use the PowerShell Syntax

More Resources

In Review…

59

Questions& Raffle

• Michael BlumenthalSharePoint ArchitectPSC Group, LLC

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

Thank you for your time today.

Recommended