introduction to powershell for sharepoint - sharepointfest 2014 workshop

73
An Advanced Introduction to PowerShell for SharePoint Developers and Administrators Michael Blumenthal & Jack Fruh

Upload: michael-blumenthal

Post on 17-Jul-2015

200 views

Category:

Technology


3 download

TRANSCRIPT

Page 1: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

An Advanced Introduction to PowerShell

for SharePointDevelopers and Administrators

Michael Blumenthal & Jack Fruh

Page 2: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

2

Who is Michael Blumenthal?

Sr. Solution Architect

at PSC Group

CSPUG Co-Leader

19 years in IT Consulting

11 years working with

SharePoint

7 years with PowerShell

Page 3: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

3

Who is Jack Fruh?

SharePoint AdministratorFortune 500 Company

Big on communitySPS Chicago Suburbs Co-Leader

SharePointJack.com

SPYam

CloudSaturday.com

Page 4: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

4

This is about you

Version of SharePoint?

Admin, Developer, Both, Other?

PowerShell experience?

SharePoint experience?

Page 5: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

5

Why?

Page 6: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

6

PowerShell puts the SharePoint Engine at your fingertips!

• It’s Easy to Get Started!1

• Learn the PowerShell Syntax2

• Best Practices3

• Practical Uses4

• Scripting Q&A, Raffle5

Page 7: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

7

Chapter 1

Page 8: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

8

Getting Started with PowerShell

20032008, R2,2012, R27, 8, 8.1,

10…

Page 9: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

9

Page 10: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

10

Page 11: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

11

Page 12: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

12

Page 13: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

13

PowerShell v3 & v4 ISE

Page 14: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

14

POSH vs the SharePoint Mgmt Shell

Page 15: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

15

Managing On Prem vs Online

http://bit.ly/POSH4SPO

Page 16: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

16

Chapter 2

Page 17: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

Learn to use PowerShell with SharePoint!

Fundamentals

Using the SharePoint API

Page 18: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

18

Symbols, Keywords, and Syntax! Oh My!

• Variables1

• Commands2

• Piping3

• Comparisons4

• Flow Control5

• Filtering6

• Scripts7

• Debugging8

Page 19: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

19

Reading Symbols in Code

(tal Guidance

Moe, Larry, and }

The universe started with the Big !

!Important

A # of Bacon and # Browns

Page 20: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

20

Variables begin with a $

• Case Insensitive, Dynamic typing

$something

$true, $false, $profile, $null

$MyMessage = “Hello, World”

1

Page 21: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

21

Page 22: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

22

Commands are called cmdlets.

Verb-Noun

Built-in, Extensible

Get-Help & Help

Get-Member

Get-Command

2

Page 23: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

23

Get-Help

Page 24: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

24

Discoverability

Page 25: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

25

Discover Commands: Get-Command

Page 26: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

26

Intellisense!

Page 27: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

27

Find custom commands this way

Refresh the command list

Actions you can take once you fill in parameters

Page 28: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

28

The Power of Piping!

Output Of Command 1

Input of Command 2

3

Page 29: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

Example

Page 30: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

Making Comparisons4

Operator 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 31: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

31

Example

Page 32: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

32

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 33: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

Example

Page 34: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

34

Where-Object6

•Where {<Test>}Syntax

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

• V3:Dir | where Name –like B*Example

Page 35: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop
Page 36: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

36

Executing Scripts

.\filename.ps1

Set-ExecutionPolicyUnrestricted

7

Page 37: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop
Page 38: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

38

Creating a function

Function global:Do-Something ($param, …){

# put cmdlets here…

}

Page 39: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

39

ISE Debugging8

Page 40: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

Screenshot

ISE Debugging

Page 41: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

41

In Review

• Variables1

• Commands2

• Piping3

• Comparisons4

• Flow Control5

• Filtering6

• Scripts7

• Debugging8

Page 42: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

42

Using the SharePoint API

• Understand the OM1

• Load Cmdlets2

• Use ’em!3

Page 43: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

43

Highlights from the SharePoint Object Model

SPField

SPListItem

SPList

SPWeb

SPSite

SPWebApplication

SPFarm

Word

Page

Book

Classroom

Department

School

District

Page 44: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

44

Loading SharePoint Cmdlets

C:\...\14 or 15\CONFIG\POWERSHELL\

Registration\SharePoint.ps1:

Add-PsSnapin Microsoft.SharePoint.PowerShell

Page 45: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

45

Loading SharePoint DLLs

Works in MOSS 2007 too!

[void][System.Reflection.Assembly]::

LoadWithPartialName("Microsoft.SharePoint")

Page 46: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

46

Get a Site and Explore it!

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

THEN

$site

Page 47: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

47

Page 48: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

48

Page 49: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

49

Chapter 3

Page 50: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

50

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 51: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

51

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 52: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

52

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 namewith 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 53: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

53

Page 54: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

54

Find custom commands this way

Refresh the command list

Page 55: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

55

Page 56: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

56

More Good Ideas

Always read scripts before running them

Make it safe for others to not read them first

Write scripts as functions most of the time

Check for valid parameter values

Do error handling

Page 57: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

57

A Word About Memory Management

SPWeb SPSite

Inline In Script

Page 58: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

58

Page 59: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

59

Chapter 4

Page 60: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

60

Practical Uses

• Bulk Create Sites1

• List Item CRUD2

• Create data for test cases3

• Associate Workflows with a List4

Page 61: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

61

More Practical Uses

• Work across site collections5

• Deployment Scripting6

• Identify files that won’t upload7

• Sync Wep App Properties8

Page 62: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

62

More Practical Uses

• Install SharePoint9

• Repeatably Manage Content10

• Update Field Definitions11

• Check the Farm Version12

Page 63: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

63

More Practical Uses

• Custom Reports on Permissions13

• Document Versioning Settings14

• Managing Azure and SPO15

• SSL Cert Expiry Alerts16

Page 64: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

64

What’s your SharePoint Version?

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

Major Minor Build Revision

----- ----- ----- --------

14 0 6109 5002

Page 65: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

65

Audio Alerts

Stick this at the end of your long running script:

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

Page 66: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

66

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 67: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

67

Review

• It’s on your machine!1

• Know the PowerShell Syntax2

• Use Best Practices3

• Practical Uses4

• Do it now!5

Page 68: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

68

Chapter 5

Page 69: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

71

Page 70: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

Book RafflePut your card in the box

Page 71: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

73

Tonight only!

CSPUG’s SPFest Speaker Panel Discussion

6-7:30pm

http://bit.ly/201412CSPUGReg

Page 72: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

74

Our Other Sessions

Page 73: Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

75

Questions & Thank You

• Michael BlumenthalSharePoint ArchitectPSC Group, LLC

[email protected]

• www.psclistens.com

• MichaelBlumenthal.me

• @MichaelBL

• LinkedIn

• SPYam

• Jack FruhSharePoint Admin

[email protected]

• SPSChicagoSuburbs.com

• SharePointJack.com

• @SharePointJack

• LinkedIn

• SPYam

Thank you for your time today.