PowerShell for SharePoint Developers and Administrators

86
PowerShell for SharePoint Developers and Administrators Michael Blumenthal Magenic Technologies [email protected]

description

PowerShell for SharePoint Developers and Administrators . Michael Blumenthal Magenic Technologies [email protected]. Who is Michael Blumenthal?. Sr. Consultant @ Magenic (MSFT Gold Partner) 16 years in IT Consulting 8 years working with SharePoint - PowerPoint PPT Presentation

Transcript of PowerShell for SharePoint Developers and Administrators

Page 1: PowerShell  for SharePoint Developers and Administrators

PowerShell for SharePoint Developers and

Administrators

Michael BlumenthalMagenic Technologies

[email protected]

Page 2: PowerShell  for SharePoint Developers and Administrators

2

Who is Michael Blumenthal?

Sr. Consultant @ Magenic (MSFT Gold Partner)

16 years in IT Consulting

8 years working with SharePoint

MCITP: SharePoint 2010; MCTS: WSS DevMCTS: MOSS Config & MCTS: WSS ConfigMCAD, MCSE, MCDBA, CAPM

Page 3: PowerShell  for SharePoint Developers and Administrators

3

No Compiling!

No Packaging!

Just Code & Go!

Why PowerShell?

Page 4: PowerShell  for SharePoint Developers and Administrators

4

PowerShell puts the SharePoint Engine at your fingertips!

• It’s Easy to Get Started!1• Learn the PowerShell Syntax2• Real World Examples3• More Resources4• Demo!

Page 5: PowerShell  for SharePoint Developers and Administrators

5

Chapter 1

IT’S EASY TO GET STARTED!

Page 6: PowerShell  for SharePoint Developers and Administrators

Getting Started with PowerShell

Windows Server 2003• Download

Windows Server 2008• Install

Windows Server 2008 R2• Run (V2!)

Page 7: PowerShell  for SharePoint Developers and Administrators

7

Page 8: PowerShell  for SharePoint Developers and Administrators

8

Shell User Experience

Extensible Tab Completion

Navigable History

Line Continuation>>

Page 9: PowerShell  for SharePoint Developers and Administrators

9

Page 10: PowerShell  for SharePoint Developers and Administrators

10

Chapter 2

LEARN THE POWERSHELL SYNTAX!

Page 11: PowerShell  for SharePoint Developers and Administrators

Learn to use PowerShell with SharePoint!

Symbols & Keywords

Using the SharePoint API

Creating and Running Scripts

Page 12: PowerShell  for SharePoint Developers and Administrators

12

Symbols, Keywords, and Syntax! Oh My!

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

Page 13: PowerShell  for SharePoint Developers and Administrators

13

Punctuation PronunciationSymbol Called Symbol Called

$ Dollar sign, money _ Underscore

# Pound, hash [ ] 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

Is “$#|” a “one dollar hash pipe”?

Page 14: PowerShell  for SharePoint Developers and Administrators

14

Variables begin with a $

• Case Insensitive, Dynamic typing

$foo

$true, $false, $profile

$foo = “Hello, World”

1

Page 15: PowerShell  for SharePoint Developers and Administrators

15

Page 16: PowerShell  for SharePoint Developers and Administrators

16

Commands are called cmdlets.Verb-Noun

Built-in, Extensible

Get-Help & Help

Get-Member

2

Page 17: PowerShell  for SharePoint Developers and Administrators

17

Page 18: PowerShell  for SharePoint Developers and Administrators

18

The Power of Piping!

Output Of Command

1

Input of Command

2|

3

Page 19: PowerShell  for SharePoint Developers and Administrators

Example

Page 20: 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 21: PowerShell  for SharePoint Developers and Administrators

21

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 22: PowerShell  for SharePoint Developers and Administrators

Example

Page 23: PowerShell  for SharePoint Developers and Administrators

23

Where-Object6

• Where {<Test>}Syntax

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

Example

Page 24: PowerShell  for SharePoint Developers and Administrators

24

Using the SharePoint API

• Getting an SPSite1• Manipulating It2• Cleaning Up3

Page 25: PowerShell  for SharePoint Developers and Administrators

25

Highlights from the SharePoint Object Model

SPField

SPListItem

SPList

SPWeb

SPWebApplication

SPFarm

SPSite

Page 26: PowerShell  for SharePoint Developers and Administrators

26

Loading SharePoint DLLs

[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

Page 27: PowerShell  for SharePoint Developers and Administrators

27

Get a Site and Explore it!

$site = New-Object Microsoft.SharePoint.SPSite(“http://server/path”)

OR$site = Get-SPSite(“http://server/path”)

THEN$site

Page 28: PowerShell  for SharePoint Developers and Administrators

28

Page 29: PowerShell  for SharePoint Developers and Administrators

29

Create a List Item

Page 30: PowerShell  for SharePoint Developers and Administrators

30

Practical Uses• Create Sites from the Command Line1• Add Lists and List Items to a Web2• Create data for test cases3• Associate Workflows with a List4• Work across site collections5

• Update User Metadata in AD for better Profile Pages6• Identify files that won’t upload7

Page 31: PowerShell  for SharePoint Developers and Administrators

31

A Word About Memory Management

SPWeb SPSite

Inline In Script

Dispose

Page 32: PowerShell  for SharePoint Developers and Administrators

32

Page 33: PowerShell  for SharePoint Developers and Administrators

33

Creating .PS1 Script Files

Page 34: PowerShell  for SharePoint Developers and Administrators

34

Executing Scripts

.\filename.ps1

Set-ExecutionPolicy Unrestricted

Page 35: PowerShell  for SharePoint Developers and Administrators

35

Chapter 3

REAL WORLD EXAMPLES

Page 36: PowerShell  for SharePoint Developers and Administrators

36

Real World Examples

Check the VersionCheck Versioning on all document LibrariesClear a listLook at multiple Event Throttling levels at once for the ULS (diagnostic) log

Page 37: PowerShell  for SharePoint Developers and Administrators

37

What’s your MOSS Version?

$farm = [Microsoft.SharePoint.Administration.SPFarm]::Local

$farm.BuildVersion Major  Minor  Build  Revision -----  -----  -----  -------- 12     0      0      6318

Page 38: PowerShell  for SharePoint Developers and Administrators

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: PowerShell  for SharePoint Developers and Administrators

39

Page 40: PowerShell  for SharePoint Developers and Administrators

40

Clear a List

Make a mess:

Clean it up!

Page 41: PowerShell  for SharePoint Developers and Administrators

41

Page 42: PowerShell  for SharePoint Developers and Administrators

42

Look at the ULS configurationWhy?

See multiple event categories at once!How?$ulsid = $farm.Services | select-object TypeName, Id | where {$_.TypeName -eq "Windows SharePoint Diagnostics Service"} | Select-Object Id$ulsvc = $farm.GetObject($ulsid.Id.ToString())$ulsvc.GetItems() | Select-Object Name, EventSeverity, TraceSeverity, hidden | where {$_.hidden -eq 0}

Page 43: PowerShell  for SharePoint Developers and Administrators

43

Page 44: PowerShell  for SharePoint Developers and Administrators

44

Chapter 4

MORE RESOURCES

Page 45: PowerShell  for SharePoint Developers and Administrators

Resources

SharePoint + Reflector / Decompiler

Microsoft Resources

3rd Party Resources

Page 46: PowerShell  for SharePoint Developers and Administrators

46

Use a Decompiler to see MSFT’s Code!

OR

ILSpy.netdotPeek (jetbrains)justDecompile (Telerik)Others…

NO

LONGER

FREE

Page 48: PowerShell  for SharePoint Developers and Administrators

48

Page 49: PowerShell  for SharePoint Developers and Administrators

49

Page 50: PowerShell  for SharePoint Developers and Administrators

50

Page 51: PowerShell  for SharePoint Developers and Administrators

51

JEFF HICKS

Page 52: PowerShell  for SharePoint Developers and Administrators

52

NEIL IVERSON

Page 54: PowerShell  for SharePoint Developers and Administrators

54

PowerShell puts the SharePoint API at your fingertips!

It’s Easy to Get Started!

Learn & Use the PowerShell Syntax

More Resources

In Review…

Page 55: PowerShell  for SharePoint Developers and Administrators

55

Chapter 5

See the power of PowerShell + SharePoint!

DEMO!

Page 56: PowerShell  for SharePoint Developers and Administrators

56

Let’s Eat!

BREAK

Page 57: PowerShell  for SharePoint Developers and Administrators

57

Bulk Creating Sites and Site Collections

PART II

Page 58: PowerShell  for SharePoint Developers and Administrators

58

The Challenge

New SharePoint

website

A lot of structure Little time

Page 59: PowerShell  for SharePoint Developers and Administrators

59

Problem Example 1

BI Solution, Contoso PAC“Elect more fake people to fake offices nationwide!”Based on real client though

Solution: Site Collection with 440 subsites1 per congressional district

Need: Streamline the Creation of 440 nearly identical subsites

Page 60: PowerShell  for SharePoint Developers and Administrators

60

Problem Example 2

Global Civil Engineering, Architecture, Design Co

• New Supplemental Intranet – Communities• Primary Intranet is Employer 2 Employee Pub• This one is for Global Communities of Practice

• Deep hierarchy from day 1• Technical Practice• Technical Practice Groups• Specific Doc Libs & lists

• Technical Discipline• Subgroups• Doc Libs and Lists

Page 61: PowerShell  for SharePoint Developers and Administrators

61

Let’s Compare Solutions

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 sitesPowerShell has easy loopsData driven from a CSVChanges -> Mod Scripts

Page 62: PowerShell  for SharePoint Developers and Administrators

62

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 63: PowerShell  for SharePoint Developers and Administrators

63

Even DVWP’s become an option

Data View Web Parts of lists & librariesEdit only in SPDPub Pages can’t be edited in SPD, only layouts

But wait!Traditional limit: tied to a list by GUIDsPower of POSH: You can edit the DVWP definitions via the OM!No longer “Not Deployable”!

Page 64: PowerShell  for SharePoint Developers and Administrators

64

So How Do I

MAKE IT WORK?

Page 65: PowerShell  for SharePoint Developers and Administrators

65

Key Language Constructs

$variable and piping (|)Import-CSVForeachFunction global:MyCustomFunction($foo){

write-host $foo}

SharePoint Object Model access

Page 66: PowerShell  for SharePoint Developers and Administrators

66

Start with a Spreadsheet

Page 67: PowerShell  for SharePoint Developers and Administrators

67

Save as a CSV

CommaSeparatedValues

“Double Quotes”

Can update CSV directly

Page 68: PowerShell  for SharePoint Developers and Administrators

68

Page 69: PowerShell  for SharePoint Developers and Administrators

69

Page 70: PowerShell  for SharePoint Developers and Administrators

70

Page 71: PowerShell  for SharePoint Developers and Administrators

71

Calling SharePoint From POSH2007

Need to define a set of helper functions

I use Zach’sGet these at PSBB.CodePlex.com

2010500+ POSH cmdlets ship with SP2010Register One Snapin

$site = Get-SPSite $SCUrl

$newweb = $site.AllWebs.Add($Url, $Title, $Description, 1033,"CMSPUBLISHING#0",$InheritParentpermissions, $DoNotConvert)

Page 72: PowerShell  for SharePoint Developers and Administrators

72

SP2010 PowerShell SnapIn

Over 500 functions defined

Automatically loaded in the SP Admin Console

Can do the same in ISE and PS Profile

Page 73: PowerShell  for SharePoint Developers and Administrators

73

Outer Loop & Test Cases

Page 74: PowerShell  for SharePoint Developers and Administrators

74

Creating a site via POSH

Page 75: PowerShell  for SharePoint Developers and Administrators

75

DVWP? What’s that?

Only in SPD…

Page 76: PowerShell  for SharePoint Developers and Administrators

76

Page 77: PowerShell  for SharePoint Developers and Administrators

77

Page 78: PowerShell  for SharePoint Developers and Administrators

78

Page 79: PowerShell  for SharePoint Developers and Administrators

79

Page 80: PowerShell  for SharePoint Developers and Administrators

80

District DVWP – Runtime Update Type 1

Page 81: PowerShell  for SharePoint Developers and Administrators

81

Adding the other DVWP

Page 82: PowerShell  for SharePoint Developers and Administrators

82

78 created sites in just 7 minutes!

DEMO!

Page 83: PowerShell  for SharePoint Developers and Administrators

83

The Challenge

You need to stand up a new SharePoint websiteIt needs a lot of structureYou don’t have a lot of time No patience for manual GUI site creation

PowerShell!

The New Answer

Page 84: PowerShell  for SharePoint Developers and Administrators

84

ResourcesBlog about SharePoint at http://BlumenthalIT.net

PSBB.CodePlex.com

Twitter: @MichaelBL

LinkedIn: http://www.linkedin.com/pub/michael-blumenthal/0/19a/354

[email protected]

Page 85: PowerShell  for SharePoint Developers and Administrators

85

Contact Me!

Blog about SharePoint at http://BlumenthalIT.net

Twitter: @MichaelBL

[email protected] & http://www.Magenic.com

Page 86: PowerShell  for SharePoint Developers and Administrators

86

Custom Solutions Guaranteed to fit.Gold in Portals and Collaboration

SDPS Program Member