introduction to powershell for sharepoint admins and developers

56
Online Conference June 17 th and 18 th 2015 WWW.SPBIZCONF.COM Michael Blumenthal PSC Group Introduction to PowerShell For SharePoint Admins and Developers

Upload: michael-blumenthal

Post on 31-Jul-2015

211 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Introduction to PowerShell for SharePoint Admins and Developers

     

               

 Online Conference

 June 17th and 18th 2015

WWW.SPBIZCONF.COM

Michael BlumenthalPSC Group

Introduction to PowerShell For SharePoint Admins and Developers

Page 2: Introduction to PowerShell for SharePoint Admins and Developers

WWW.SPBIZCONF.COM

Who is Michael Blumenthal?

• Technical Evangelist, PSC Group• 20 years in IT Consulting• 12 years with SharePoint • 8 years with PowerShell• Twitter: @MichaelBL

Page 3: Introduction to PowerShell for SharePoint Admins and Developers

     

               

 Online Conference

 June 17th and 18th 2015

No Compiling!

No Packaging!

Command Line Control!

Why PowerShell?

Page 4: Introduction to PowerShell for SharePoint Admins and Developers

     

               

 Online Conference

 June 17th and 18th 2015

PowerShell  puts the SharePoint Engine at your fingertips!

• It’s Easy to Get Started!1• Learn the PowerShell Syntax2• PowerShell + SharePoint3• Real World Examples4• More Resources5

Page 5: Introduction to PowerShell for SharePoint Admins and Developers

     

               

 Online Conference

 June 17th and 18th 2015

Chapter 1

IT’S EASY TO GET STARTED!

Page 6: Introduction to PowerShell for SharePoint Admins and Developers

     

               

 Online Conference

 June 17th and 18th 2015

Getting Started with PowerShell

20032008,R22012, R2107, 8, 8.1, 10

Page 7: Introduction to PowerShell for SharePoint Admins and Developers

WWW.SPBIZCONF.COM

The Command Line Window

Page 8: Introduction to PowerShell for SharePoint Admins and Developers

WWW.SPBIZCONF.COM

Page 9: Introduction to PowerShell for SharePoint Admins and Developers

WWW.SPBIZCONF.COM

The Integrated Script Editor

V2

Page 10: Introduction to PowerShell for SharePoint Admins and Developers

WWW.SPBIZCONF.COM

PowerShell V3&4 ISE

Page 11: Introduction to PowerShell for SharePoint Admins and Developers

WWW.SPBIZCONF.COM

POSH vs the SharePoint Mgmt Shells

Page 12: Introduction to PowerShell for SharePoint Admins and Developers

     

               

 Online Conference

 June 17th and 18th 2015

Chapter 2

LEARN THE POWERSHELL SYNTAX!

Page 13: Introduction to PowerShell for SharePoint Admins and Developers

WWW.SPBIZCONF.COM

Symbols, Keywords, and Syntax! Oh My!

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

Page 14: Introduction to PowerShell for SharePoint Admins and Developers

WWW.SPBIZCONF.COM

Reading Symbols in Code

• (tal Guidance• Moe, Larry, and }• The universe started with the Big !• !Important• A # of Bacon and # Browns

Page 15: Introduction to PowerShell for SharePoint Admins and Developers

WWW.SPBIZCONF.COM

1. Variables!

• Case Insensitive, Dynamic typing

$something

$true, $false, $null, $profile

$myMessage= “Hello, World”

1

Page 16: Introduction to PowerShell for SharePoint Admins and Developers

     

               

 Online Conference

 June 17th and 18th 2015

Page 17: Introduction to PowerShell for SharePoint Admins and Developers

WWW.SPBIZCONF.COM

2. Commands are called cmdlets.

Verb-Noun

Built-in, Extensible

Get-Help & Help

Get-Member

Page 18: Introduction to PowerShell for SharePoint Admins and Developers

WWW.SPBIZCONF.COM

Page 19: Introduction to PowerShell for SharePoint Admins and Developers

     

               

 Online Conference

 June 17th and 18th 2015

Discoverability

Page 20: Introduction to PowerShell for SharePoint Admins and Developers

WWW.SPBIZCONF.COM

3. The Power of Piping!

Output Of Command 1

Input of Command 2

|

Page 21: Introduction to PowerShell for SharePoint Admins and Developers

WWW.SPBIZCONF.COM

Example

Page 22: Introduction to PowerShell for SharePoint Admins and Developers

WWW.SPBIZCONF.COM

4. Dial zero for an…4

Operator

-eq -le-ne -like-gt -notlike-ge -match-lt -notmatch

Page 23: Introduction to PowerShell for SharePoint Admins and Developers

WWW.SPBIZCONF.COM

Example

Page 24: Introduction to PowerShell for SharePoint Admins and Developers

WWW.SPBIZCONF.COM

5. Taking Control of the Flow

• 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 25: Introduction to PowerShell for SharePoint Admins and Developers

     

               

 Online Conference

 June 17th and 18th 2015

Example

Page 26: Introduction to PowerShell for SharePoint Admins and Developers

WWW.SPBIZCONF.COM

6. Where-Object

•Where {<Test>}Syntax

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

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

Example

Page 27: Introduction to PowerShell for SharePoint Admins and Developers

WWW.SPBIZCONF.COM

Executing Scripts

.\filename.ps1

Set-ExecutionPolicy Unrestricted

Page 28: Introduction to PowerShell for SharePoint Admins and Developers

     

               

 Online Conference

 June 17th and 18th 2015

Chapter 3

POWERSHELL + 

SHAREPOINT

Page 29: Introduction to PowerShell for SharePoint Admins and Developers

WWW.SPBIZCONF.COM

Version of SharePoint?

O365

D/L SOMC•Get GLP Scripts

SP2007

Get Scripts

2010/2013

Run SMC

Get PoSh Cmdlets for SharePoint

Page 30: Introduction to PowerShell for SharePoint Admins and Developers

WWW.SPBIZCONF.COM

Using SharePoint Cmdlets in the ISE

O365: Import-Module microsoft.online.sharepoint.Powershell

On Prem 2010+:C:\...\14 or 15\CONFIG\POWERSHELL\Registration\SharePoint.ps1

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

LoadWithPartialName("Microsoft.SharePoint")

Page 31: Introduction to PowerShell for SharePoint Admins and Developers

WWW.SPBIZCONF.COM

Highlights from the SharePoint Object Model 

SPField

SPListItem 

SPList

SPWeb

SPWebApplication

SPFarm

SPSite

Page 32: Introduction to PowerShell for SharePoint Admins and Developers

     

               

 Online Conference

 June 17th and 18th 2015

Page 33: Introduction to PowerShell for SharePoint Admins and Developers

WWW.SPBIZCONF.COM

On-Prem is simpler

Get-SPSite $url

• Use Farm Account or grant access with Add-SPShellAdmin

Page 34: Introduction to PowerShell for SharePoint Admins and Developers

     

               

 Online Conference

 June 17th and 18th 2015

A Word About Memory Management

SPWeb SPSite

Inline In Script

 Dispose

Page 35: Introduction to PowerShell for SharePoint Admins and Developers

     

               

 Online Conference

 June 17th and 18th 2015

Page 36: Introduction to PowerShell for SharePoint Admins and Developers

     

               

 Online Conference

 June 17th and 18th 2015

Chapter 4

REAL WORLD EXAMPLES

Page 37: Introduction to PowerShell for SharePoint Admins and Developers

WWW.SPBIZCONF.COM

Real World Examples

• Check the Farm Version• Check Versioning on all document

Libraries• Create List Items• Export Web App Properties to a file• Bulk Site Creation

Page 38: Introduction to PowerShell for SharePoint Admins and Developers

WWW.SPBIZCONF.COM

What’s your Farm Version?

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

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

Page 39: Introduction to PowerShell for SharePoint Admins and Developers

WWW.SPBIZCONF.COM

Get a Web and Explore it!

$web = get-spweb http://server/path

THEN

$web

Page 40: Introduction to PowerShell for SharePoint Admins and Developers

     

               

 Online Conference

 June 17th and 18th 2015

Page 41: Introduction to PowerShell for SharePoint Admins and Developers

WWW.SPBIZCONF.COM

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 42: Introduction to PowerShell for SharePoint Admins and Developers

     

               

 Online Conference

 June 17th and 18th 2015

Page 43: Introduction to PowerShell for SharePoint Admins and Developers

WWW.SPBIZCONF.COM

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 44: Introduction to PowerShell for SharePoint Admins and Developers

WWW.SPBIZCONF.COM

More Practical Uses• Sync Wep App Properties8• Install SharePoint9• Repeatably Manage Content10• Update Field Definitions11• Report on Security12

Page 45: Introduction to PowerShell for SharePoint Admins and Developers

WWW.SPBIZCONF.COM

Create a List Item Server-Side

Page 46: Introduction to PowerShell for SharePoint Admins and Developers

WWW.SPBIZCONF.COM

Audio Alerts

• Stick this at the end of your long running script:

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

Page 47: Introduction to PowerShell for SharePoint Admins and Developers

     

               

 Online Conference

 June 17th and 18th 2015

Bulk Site Creation

Site Definitions in V. Studio• Not an answer by themselves• Define site content• Intended for reuse– Mismatch to one time need

• CAML and PITA• Harder: Making it data driven• Change Site Def -> Recreate Site

PowerShell & Excel & UI

• Well suited for one time “blow in’s”

• Define the site template in the UI or use standard

• Save as a template– Even pub sites - sometimes• PowerShell has easy loops• Data driven from a CSV• Changes -> Mod Scripts

Page 48: Introduction to PowerShell for SharePoint Admins and Developers

WWW.SPBIZCONF.COM

The PowerShell Solution

• Read the list of sites from CSV• Loop:– Create Site– Configure Site• Turn on Features• Set Master Pages, Welcome Page• Hide Libraries, set versioning• Adjust Navigation

– Add Lists, Libraries, Pages, Web parts, etc

• Loop again & again if needed – iterative!

Page 49: Introduction to PowerShell for SharePoint Admins and Developers

     

               

 Online Conference

 June 17th and 18th 2015

Chapter 5

MORE RESOURCES

Page 50: Introduction to PowerShell for SharePoint Admins and Developers

     

               

 Online Conference

 June 17th and 18th 2015

Page 51: Introduction to PowerShell for SharePoint Admins and Developers

     

               

 Online Conference

 June 17th and 18th 2015

Page 52: Introduction to PowerShell for SharePoint Admins and Developers

     

               

 Online Conference

 June 17th and 18th 2015

Page 53: Introduction to PowerShell for SharePoint Admins and Developers

     

               

 Online Conference

 June 17th and 18th 2015

Page 55: Introduction to PowerShell for SharePoint Admins and Developers

     

               

 Online Conference

 June 17th and 18th 2015

In Review• It’s Easy to Get Started!1• Learn the PowerShell Syntax2• PowerShell + SharePoint3• Real World Examples4• More Resources5

Start Scripting Today!

Page 56: Introduction to PowerShell for SharePoint Admins and Developers

WWW.SPBIZCONF.COM

Please fill in my session feedback form available from the ‘Session Resources’ tab

on my session window.

Why not join us in October at

Michael BlumenthalTechnical Solution EvangelistPSC Group, LLC

• Contact me at: • [email protected]• MichaelBlumenthal.me• Twitter: @MichaelBL• Yammer.com/SPYam