104
Introduction to PowerShell for SharePoint Developers and Administrators Michael Blumenthal & Jack Fruh

Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

Embed Size (px)

DESCRIPTION

The slides from Michael Blumenthal's and Jack Fruh's "Be a Hero with PowerShell" workshop at SharePoint fest, a comprehensive introduction to using PowerShell with SharePoint.

Citation preview

Page 1: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

Introduction to PowerShell for SharePoint Developers and

Administrators

Michael Blumenthal & Jack Fruh

Page 2: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

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 - Be a PowerShell Hero - SPFest workshop

3

Who is Jack Fruh?

SharePoint AdministratorFortune 500 Company

Big on communitySPS Chicago Suburbs Co-LeaderSharePointJack.comSPYamSharePoint-Community.org

Page 4: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

4

This is about you

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

Page 5: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

5

No Compiling!

No Packaging!

Just Code & Go!

Why PowerShell?

Page 6: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

6

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!5

Page 7: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

7

Chapter 1

IT’S EASY TO GET STARTED!

Page 8: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

Getting Started with PowerShell

Windows Server 2003• Download

Windows Server 2008• Install

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

Page 9: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

9

Page 10: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

10

Page 11: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

11

V2

Page 12: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

12

PowerShell V3 ISE

Page 13: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

13

POSH vs the SharePoint Mgmt Shell

Page 14: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

14

Chapter 2

LEARN THE POWERSHELL SYNTAX!

Page 15: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

Learn to use PowerShell with SharePoint!

Symbols & Keywords

Using the SharePoint API

Creating and Running Scripts

Page 16: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

16

Symbols, Keywords, and Syntax! Oh My!

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

Page 17: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

17

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 18: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

18

Variables begin with a $

$foo

• Case Insensitive, Dynamic typing

$true, $false, $profile, $null

$foo = “Hello, World”

1

Page 19: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

19

Page 20: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

20

Commands are called cmdlets.

Verb-Noun

Built-in, Extensible

Get-Help & Help

Get-Member

2

Page 21: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

21

Page 22: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

22

The Power of Piping!

Output Of Command

1

Input of Command

2|

3

Page 23: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

Example

Page 24: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

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 25: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

25

Example

Page 26: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

26

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 27: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

Example

Page 28: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

28

Where-Object6

•Where {<Test>}Syntax

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

• V3:Dir | where Name –like B*

Example

Page 29: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

DEMO

Directory Sorting

Page 30: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

30

Using the SharePoint API

•Getting an SPSite1•Manipulating It2•Cleaning Up3

Page 31: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

31

Highlights from the SharePoint Object Model

SPSite

Page 32: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

32

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 33: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

33

DEMO:ISE needs to load snapin

Page 34: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

34

Get a Site and Explore it!

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

THEN$site

Page 35: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

35

Page 36: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

DEMO

Web, Site, Lists

Page 37: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

37

A Word About Memory ManagementSPWeb

SPSite

Inline

In Script

Dispose

Page 38: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

38

Page 39: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

39

BREAK

Page 40: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

40

Chapter 3

REAL WORLD EXAMPLES

Page 41: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

41

Practical Uses• Bulk Create Sites1• List Item CRUD2 •Create data for test cases3

• Associate Workflows with a List4 •Work across site collections

5•Deployment Scripting6•Identify files that won’t upload7

Page 42: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

42

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

Page 43: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

43

Real World Examples

Check the Farm VersionCreate List ItemsBulk Site CreationPost Deployment Build Scripts with Audio AlertsDocument Versioning Settings (later)

Page 44: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

44

What’s your SharePoint Version?

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

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

Page 45: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

DEMO

Farm & Build Ver

Page 46: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

46

Create a List Item

Page 47: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

DEMO

Create New Item

Page 48: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

48

Audio Alerts

Stick this at the end of your long running script:

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

Page 49: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

49

Email Alerts

Send-MailMessage [-To] <string[]> [-Subject] <string> [[-Body] <string>] [[-SmtpServer] <string>] -From <string> [-Attachments <string[]>] [-Bcc <string[]>] [-BodyAsHtml] [-Encoding <Encoding>] [-Cc <string[]>] [-DeliveryNotificationOption <DeliveryNotificationOptions>] [-Priority <MailPriority>] [-Credential <pscredential>] [-UseSsl] [-Port <int>] [<CommonParameters>]

Page 50: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

50

Executing Scripts

.\filename.ps1

Set-ExecutionPolicy Unrestricted

Page 51: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

DEMO

Run a Script – See Exec Pol

Page 52: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

52

Chapter 4

TOOLS & BEST PRACTICES

Page 53: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

53

Tools and Best Practices

•Tools 1•Best Practices2•Resources3

Page 54: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

Tools

ISE• Add Windows Feature

PowerShell Plus - Idera• SP2010 focused

PowerGui• Pro version Too

PowerShell Studio - Sapien• Not Free

Visual Studio 2012

Page 55: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

55

ISE, ISE Baby

Microsoft’s

Available Since POSH V2• SP2010 -> POSH 2; SP2013 -> POSH 3

OS Feature

Page 56: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

56

You might have to add the ISE

Page 57: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

57

V2Use With SP2010

Page 58: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

58

PowerShell V3 ISE - for SP2013

Page 59: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

59

POSH vs the SharePoint Mgmt Shell

POSH + SnapIn = SharePoint Management Shell

Page 60: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

60

Where is it?

Page 61: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

61

Page 62: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

62

Demo – ISE Debugging

Page 63: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

63

Visual Studio 2012 Update 3Integration with TFSFor SharePoint developers, tool of choiceAdd-ons available

Page 64: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

64

Page 65: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

65

Page 66: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

66

Page 67: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

67

PowerGUI

Created for POSH v1 originally

Free & large community support

Lots of Add-ons• SCCS in Pro

Page 68: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

68

Page 69: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

69

PowerGUI screen shot

Page 70: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

70

Idera’s PowerShell Plus

Free with ads for Idera’s other productsIncludes support for SP2010, but not 2013

Framework Dependecy….Lots of features…. Bloated Overkill?

Page 71: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

71

Page 72: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

72

Sapien’s PowerShell Studio

Trial requires special trial license in a VMDoes create a POSH Library

Page 73: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

73

Page 74: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

74

Sapien Bonus

Page 75: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

75

Conclusion on POSH IDEs

Page 76: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

76

BREAK

Page 77: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

77

Best Practices

• Follow the Verb-Noun pattern1• Comment Your Functions2• Write your scripts as functions that

announce themselves• Make accidentals runs harmless

3• Use Source Control4

Page 78: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

78

Verb-Noun = Action-Thing

Add Backup Clear Connect Convert Copy Disable Disconnect Dismount Enable

Etc…

SPAppDeniedEndpoint SPClaimTypeMapping SPDiagnosticsPerformanceCounter SPDistributedCacheServiceInstance SPEduClassMember SPEduUser SPInfoPathUserAgent SPPluggableSecurityTrimmer SPProfileLeader SPProfileSyncConnection

Etc…

42 verbs combined with 347 nouns to give us 799 cmdlets

Page 79: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

79

Comment your functions

<#.SYNOPSIS –a brief explanation of what the script or function does..DESCRIPTION – a more detailed explanation of what the script or function does..PARAMETER name – an explanation of a specific parameter. Replace name with the parameter name. You can have one of these sections for each parameter the script or function uses..EXAMPLE – an example of how to use the script or function. You can have multiple .EXAMPLE sections if you want to provide more than one example..NOTES – any miscellaneous notes on using the script or function..LINK – a cross-reference to another help topic; you can have more than one of these. If you include a URL beginning with http:// or https://, the shell will open that URL when the Help command’s –online parameter is used.

#>

Page 80: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

80

Page 81: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

81

Find custom commands this way

Refresh the command list

Actions you can take once you fill in parameters

Page 82: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

82

More Good Ideas

Always read scripts before running themMake it safe for others to not read them firstWrite scripts as functions most of the timeCheck for valid parameter values Do error handling

Page 83: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

83

Demo

Writing FunctionsUsing Comments for Documentation

Page 84: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

TFS DEMOCheck In, Out, Diff

Page 85: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

85

MORE EXAMPLES

Page 86: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

86

Now for More Examples

Jack’s take on Doc Lib Versioning – reporting & settingBulk Site CreationEmailLoggingSSL Cert Expiration WarningDeploy WSPs

Page 87: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

DEMO

Doc Lib Versioning

Page 88: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

88

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 89: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

89

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 90: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

DEMO

Bulk Site Creation

Page 91: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

Jack’s Favorite Scripts

Logging, Deployment, & more

Page 92: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

92

Chapter 5

MORE RESOURCES

Page 93: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

Resources

Microsoft Resources

3rd Party Resources

Page 95: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

95

Page 96: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

96

Page 97: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

97

Page 98: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

98

Page 99: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

99

Page 100: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

100

Page 101: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

101

JEFF HICKS

Page 102: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

102

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 103: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

103

Script something today!

It’s Easy to Get Started!

Learn & Use the PowerShell Syntax

More Resources

In Review…

Page 104: Introduction to PowerShell - Be a PowerShell Hero - SPFest workshop

104

Questions & Thank You

• Michael BlumenthalSharePoint ArchitectPSC Group, LLC

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

@MichaelBL• SPYam

• Jack FruhSharePoint Admin

[email protected]

• SPSChicagoSuburbs.com• SharePointJack.com• Twitter:

@SharePointJack• SPYam

Thank you for your time today.