24
PowerShell and SharePoint Talbott Crowell July 28, 2012 SharePoint Saturday NYC #SPSNYC http://www.thirdm.com @ talbott

PowerShell and SharePoint @spsnyc July 2012

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: PowerShell and SharePoint @spsnyc July 2012

PowerShell and SharePointTalbott Crowell

July 28, 2012SharePoint Saturday NYC #SPSNYC

http://www.thirdm.com@talbott

Page 2: PowerShell and SharePoint @spsnyc July 2012

www.sharepointsaturday.org/boston : [email protected] : EMAIL

@SPSBoston / #SPSBos : TWITTER

http://www.thirdm.com : WEBhttp://talbottcrowell.wordpress.com : BLOG

@talbott : TWITTER

Outline & Agenda• What is PowerShell?• PowerShell Basics• Tools• PowerShell and SharePoint• Creating SharePoint 2010 Cmdlets• Iteration Style Scripts

Page 3: PowerShell and SharePoint @spsnyc July 2012

www.sharepointsaturday.org/boston : [email protected] : EMAIL

@SPSBoston / #SPSBos : TWITTER

http://www.thirdm.com : WEBhttp://talbottcrowell.wordpress.com : BLOG

@talbott : TWITTER

What is PowerShell?• Unix-like shell• Object oriented• .NET• Command line• Scripting language

Page 4: PowerShell and SharePoint @spsnyc July 2012

www.sharepointsaturday.org/boston : [email protected] : EMAIL

@SPSBoston / #SPSBos : TWITTER

http://www.thirdm.com : WEBhttp://talbottcrowell.wordpress.com : BLOG

@talbott : TWITTER

Books• Windows PowerShell in Action

– by Bruce Payette (designer and author of PowerShell)

• Windows PowerShell 2.0 Administrator's Pocket Consultant – By William R. Stanek

Page 5: PowerShell and SharePoint @spsnyc July 2012

www.sharepointsaturday.org/boston : [email protected] : EMAIL

@SPSBoston / #SPSBos : TWITTER

http://www.thirdm.com : WEBhttp://talbottcrowell.wordpress.com : BLOG

@talbott : TWITTER

Why PowerShell for SharePoint?• Automated build and deploy• Rapid prototyping• Exploring “What If” scenarios• Developer onboarding• Administration automation

Page 6: PowerShell and SharePoint @spsnyc July 2012

www.sharepointsaturday.org/boston : [email protected] : EMAIL

@SPSBoston / #SPSBos : TWITTER

http://www.thirdm.com : WEBhttp://talbottcrowell.wordpress.com : BLOG

@talbott : TWITTER

When use PowerShell?• When you want to make your team more

agile– Automation, automation, automation

• When developing, your daily build is like the projects heartbeat– PowerShell can be the pacemaker

• Testing– Use the PowerShell scripts to stand up an

environment for running tests

Page 7: PowerShell and SharePoint @spsnyc July 2012

www.sharepointsaturday.org/boston : [email protected] : EMAIL

@SPSBoston / #SPSBos : TWITTER

http://www.thirdm.com : WEBhttp://talbottcrowell.wordpress.com : BLOG

@talbott : TWITTER

PowerShell Basics• What do you know about a command line?– DIR

• How about– $a = DIR

• What is $a?– .NET Object

• use gm or get-member to query properites

– Array• $a[0]

Page 8: PowerShell and SharePoint @spsnyc July 2012

www.sharepointsaturday.org/boston : [email protected] : EMAIL

@SPSBoston / #SPSBos : TWITTER

http://www.thirdm.com : WEBhttp://talbottcrowell.wordpress.com : BLOG

@talbott : TWITTER

Demo - Basics• PowerShell Basics– $a = DIR– $a | gm– Dates

Page 9: PowerShell and SharePoint @spsnyc July 2012

www.sharepointsaturday.org/boston : [email protected] : EMAIL

@SPSBoston / #SPSBos : TWITTER

http://www.thirdm.com : WEBhttp://talbottcrowell.wordpress.com : BLOG

@talbott : TWITTER

Tools• cmd, notepad• PowerShell Command• Windows PowerShell Integrated

Scripting Environment (ISE)– Import-Module ServerManager;– Add-WindowsFeature PowerShell-ISE

• PowerGUI– Download from powergui.org

Page 10: PowerShell and SharePoint @spsnyc July 2012

www.sharepointsaturday.org/boston : [email protected] : EMAIL

@SPSBoston / #SPSBos : TWITTER

http://www.thirdm.com : WEBhttp://talbottcrowell.wordpress.com : BLOG

@talbott : TWITTER

More Basics• # for comment• Verb-Noun convention for

commandlets• Write-Host “Hello World”• Set-ExecutionPolicy Unrestricted• .\scriptname to execute

Page 11: PowerShell and SharePoint @spsnyc July 2012

www.sharepointsaturday.org/boston : [email protected] : EMAIL

@SPSBoston / #SPSBos : TWITTER

http://www.thirdm.com : WEBhttp://talbottcrowell.wordpress.com : BLOG

@talbott : TWITTER

SharePoint 2010• Comes with PowerShell Commandlets– Get-SPSite– New-SPSite– New-SPWeb

• If you are running from standard PowerShellAdd-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue

Page 12: PowerShell and SharePoint @spsnyc July 2012

www.sharepointsaturday.org/boston : [email protected] : EMAIL

@SPSBoston / #SPSBos : TWITTER

http://www.thirdm.com : WEBhttp://talbottcrowell.wordpress.com : BLOG

@talbott : TWITTER

Create Site Collections and Sites

• Get-SPSite– Parameter: url

• New-SPSite– Parameters: url, name, ownerAlias, template

• New-SPWeb– Parameters: url, name, description, template…– Other params:

• -AddToTopNav or -UseParentTopNav• -AddToQuickLaunch

Page 13: PowerShell and SharePoint @spsnyc July 2012

www.sharepointsaturday.org/boston : [email protected] : EMAIL

@SPSBoston / #SPSBos : TWITTER

http://www.thirdm.com : WEBhttp://talbottcrowell.wordpress.com : BLOG

@talbott : TWITTER

What about MOSS 2007 or WSS?• Your friend STSADM is still there• You can call STSADM or any command line tool

from PowerShell• You can write your own command line tools

with .NET• Better yet, you can write your own PowerShell

Commandlets!– Inherit from Cmdlet or PSCmdlet

• Gary Lapointe has WSS and MOSS Cmdlets!– http://stsadm.blogspot.com/2009/02/downloads.html

Page 14: PowerShell and SharePoint @spsnyc July 2012

www.sharepointsaturday.org/boston : [email protected] : EMAIL

@SPSBoston / #SPSBos : TWITTER

http://www.thirdm.com : WEBhttp://talbottcrowell.wordpress.com : BLOG

@talbott : TWITTER

Creating SharePoint 2010 Cmdlets

• When creating non-persistent tasks (i.e. get info) use:– SPCmdlet

• When objects persist between commands, use:– SPRemoveCmdletBase– SPNewCmdletBase– SPSetCmdletBase– SPGetCmdletBase

• For more info, see Gary Lapointe’s blog post:– http://

stsadm.blogspot.com/2009/10/creating-custom-sharepoint-2010-cmdlets.html

Page 15: PowerShell and SharePoint @spsnyc July 2012

www.sharepointsaturday.org/boston : [email protected] : EMAIL

@SPSBoston / #SPSBos : TWITTER

http://www.thirdm.com : WEBhttp://talbottcrowell.wordpress.com : BLOG

@talbott : TWITTER

Creating SharePoint Objects• [void]

[System.Reflection.Assembly]::LoadWithPartialName(”Microsoft.SharePoint”)– Load the assembly

• $SPSite = New-Object Microsoft.SharePoint.SPSite($url)– Reference to the site collection using SharePoint

object model

• Don’t forget to– $SPSite.Dispose()

Page 16: PowerShell and SharePoint @spsnyc July 2012

www.sharepointsaturday.org/boston : [email protected] : EMAIL

@SPSBoston / #SPSBos : TWITTER

http://www.thirdm.com : WEBhttp://talbottcrowell.wordpress.com : BLOG

@talbott : TWITTER

Prototype Strategy• Series of scripts to build your site• Cleanup script to destroy site• Edit script, run cleanup, run script,

view site– Repeat

Page 17: PowerShell and SharePoint @spsnyc July 2012

www.sharepointsaturday.org/boston : [email protected] : EMAIL

@SPSBoston / #SPSBos : TWITTER

http://www.thirdm.com : WEBhttp://talbottcrowell.wordpress.com : BLOG

@talbott : TWITTER

Demo – series of scripts• Build2010.ps1

– Calls other scripts

• Build2010_site_structure.ps1– Sets up the basic site structure and content types

• Build2010_upload_file.ps1– Uploads sample files to the site

• Build2010_set_logo.ps1– Adds site logo

• Build2010_add_users.ps1– Adds users to local machine and/or SharePoint groups

Page 18: PowerShell and SharePoint @spsnyc July 2012

www.sharepointsaturday.org/boston : [email protected] : EMAIL

@SPSBoston / #SPSBos : TWITTER

http://www.thirdm.com : WEBhttp://talbottcrowell.wordpress.com : BLOG

@talbott : TWITTER

Defining functions• function Get-Theme

([Microsoft.SharePoint.SPWeb]$SPWeb, [string]$themeName)

• Strong typed parameters• Returns

Microsoft.SharePoint.Utilities.ThmxTheme

Page 19: PowerShell and SharePoint @spsnyc July 2012

www.sharepointsaturday.org/boston : [email protected] : EMAIL

@SPSBoston / #SPSBos : TWITTER

http://www.thirdm.com : WEBhttp://talbottcrowell.wordpress.com : BLOG

@talbott : TWITTER

Iteration Style Scripts• Upload File

– Takes in 1 or more files

• Has 3 blocks– Begin– Process– End

• Process is executed for each file• gci 'C:\uploadfiles\Samples\Legal' | .\

build2010_upload_file.ps1 -Location "shared/legal" -DocLib "Documents" -ContentType "Document" -MetaDataField "Dept" -MetaDataValue "Legal"

Page 20: PowerShell and SharePoint @spsnyc July 2012

www.sharepointsaturday.org/boston : [email protected] : EMAIL

@SPSBoston / #SPSBos : TWITTER

http://www.thirdm.com : WEBhttp://talbottcrowell.wordpress.com : BLOG

@talbott : TWITTER

Books• Windows PowerShell in Action

– by Bruce Payette (designer and author of PowerShell)

• Windows PowerShell 2.0 Administrator's Pocket Consultant – By William R. Stanek

Page 21: PowerShell and SharePoint @spsnyc July 2012

www.sharepointsaturday.org/boston : [email protected] : EMAIL

@SPSBoston / #SPSBos : TWITTER

http://www.thirdm.com : WEBhttp://talbottcrowell.wordpress.com : BLOG

@talbott : TWITTER

Other References• How to: Build a SharePoint 2010 PowerShell

Cmdlet– http://silverlight.sys-con.com/node/1370916

• Microsoft TechNet– http://technet.microsoft.com/en-us/library/bb978526

Page 22: PowerShell and SharePoint @spsnyc July 2012

22 | SharePoint Saturday New York City 2011

Housekeeping

• Please remember to turn in your filled out bingo cards and event evaluations for prizes.

• SharePint is sponsored by Summit 7 Systems across the way at the Hilton NYC.

• Follow SharePoint Saturday New York City on Twitter @spsnyc and hashtag #spsnyc

Page 23: PowerShell and SharePoint @spsnyc July 2012

Thanks to Our Sponsors!

Page 24: PowerShell and SharePoint @spsnyc July 2012

Thank you. Questions?

PowerShell and SharePointTalbott Crowell

ThirdM.com

http://talbottcrowell.wordpress.com/

Twitter: @talbott