An Advanced Introduction to PowerShell for SharePoint Developers and Administrators Michael Blumenthal & Jack Fruh
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
3 Who is Jack Fruh? SharePoint Administrator Fortune 500 Company Big on community SPS Chicago Suburbs Co- Leader SharePointJack.com SPYam CloudSaturday.com
4 This is about you Version of SharePoint? Admin, Developer, Both, Other? PowerShell experience? SharePoint experience?
5 Why?
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
7 Chapter 1
8 Getting Started with PowerShell 2003 2008, R2, 2012, R27, 8, 8.1, 10…
9
10
11
12
13 PowerShell v3 & v4 ISE
14 POSH vs the SharePoint Mgmt Shell
15 Managing On Prem vs Online http://bit.ly/POSH4SPO
16 Chapter 2
Learn to use PowerShell with SharePoint! Fundamentals Using the SharePoint API
18 Symbols, Keywords, and Syntax! Oh My! • Variables1 • Commands2 • Piping3 • Comparisons4 • Flow Control5 • Filtering6 • Scripts7 • Debugging8
19 Reading Symbols in Code (tal Guidance Moe, Larry, and } The universe started with the Big ! !Important A # of Bacon and # Browns
20 Variables begin with a $ • Case Insensitive, Dynamic typing $something $true, $false, $profile, $null $MyMessage = “Hello, World” 1
21
22 Commands are called cmdlets. Verb-Noun Built-in, Extensible Get-Help & Help Get-Member Get-Command 2
23 Get-Help
24 Discoverability
25 Discover Commands: Get-Command
26 Intellisense!
27 Find custom commands this way Refresh the command list Actions you can take once you fill in parameters
28 The Power of Piping! Output Of Command 1 Input of Command 2 3
Example
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)
31 Example
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
Example
34 Where-Object6 •Where {<Test>}Syntax • V1&2:Dir | Where {$_.Name –like “B*”} • V3:Dir | where Name –like B* Example
36 Executing Scripts .filename.ps1 Set-ExecutionPolicy Unrestricted 7
38 Creating a function Function global:Do-Something ($param, …){ # put cmdlets here… }
39 ISE Debugging8
Screenshot ISE Debugging
41 In Review • Variables1 • Commands2 • Piping3 • Comparisons4 • Flow Control5 • Filtering6 • Scripts7 • Debugging8
42 Using the SharePoint API • Understand the OM1 • Load Cmdlets2 • Use ’em!3
43 Highlights from the SharePoint Object Model SPField SPListItem SPList SPWeb SPSite SPWebApplication SPFarm Word Page Book Classroom Department School District
44 Loading SharePoint Cmdlets C:...14 or 15CONFIGPOWERSHELL RegistrationSharePoint.ps1: Add-PsSnapin Microsoft.SharePoint.PowerShell
45 Loading SharePoint DLLs Works in MOSS 2007 too! [void][System.Reflection.Assembly]:: LoadWithPartialName("Microsoft.SharePoint")
46 Get a Site and Explore it! $site = get-spsite http://server/path THEN $site
47
48
49 Chapter 3
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
51 Verb-Noun = Action-Thing Add Backup Clear Connect Convert Copy Disable Disconnect Dismount Enable Etc… SPAppDeniedEndpoint SPClaimTypeMapping SPDiagnosticsPerformanceCo unter SPDistributedCacheServiceIns tance SPEduClassMember SPEduUser SPInfoPathUserAgent SPPluggableSecurityTrimmer SPProfileLeader SPProfileSyncConnection Etc… 42 verbs combined with 347 nouns to give us 799 cmdlets
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 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. #>
53
54 Find custom commands this way Refresh the command list
55
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
57 A Word About Memory Management SPWeb SPSite Inline In Script
58
59 Chapter 4
60 Practical Uses • Bulk Create Sites1 • List Item CRUD2 •Create data for test cases3 • Associate Workflows with a List4
61 More Practical Uses • Work across site collections5 • Deployment Scripting6 • Identify files that won’t upload7 • Sync Wep App Properties8
62 More Practical Uses • Install SharePoint9 • Repeatably Manage Content10 • Update Field Definitions11 • Check the Farm Version12
63 More Practical Uses • Custom Reports on Permissions13 • Document Versioning Settings14 • Managing Azure and SPO15 • SSL Cert Expiry Alerts16
64 What’s your SharePoint Version? PS C:UsersAdministrator> $(get- SPFarm).BuildVersion Major Minor Build Revision ----- ----- ----- -------- 14 0 6109 5002
65 Audio Alerts Stick this at the end of your long running script: $Voice = new-object -com SAPI.SpVoice $Voice.Speak(“Deployment is done!")
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>]
67 Review • It’s on your machine!1 • Know the PowerShell Syntax2 • Use Best Practices3 • Practical Uses4 • Do it now!5
68 Chapter 5
71
Book Raffle Put your card in the box
73 Tonight only! CSPUG’s SPFest Speaker Panel Discussion 6-7:30pm http://bit.ly/201412CSPUGReg
74 Our Other Sessions
75 Questions & Thank You • Michael Blumenthal SharePoint Architect PSC Group, LLC • MBlumenthal@psclistens.com • www.psclistens.com • MichaelBlumenthal.me • @MichaelBL • LinkedIn • SPYam • Jack Fruh SharePoint Admin • Jack@SharepointJack.com • SPSChicagoSuburbs.com • SharePointJack.com • @SharePointJack • LinkedIn • SPYam Thank you for your time today.

Introduction to PowerShell for SharePoint - SharePointFest 2014 workshop

Editor's Notes

  • #3 PSC is a title sponsor
  • #5 Anyone here not done SharePoint development? What kind of development have you done? Who has worked with PowerShell already? Used for what? Your name? Who has worked with other scripting languages?
  • #6 Command line control of SharePoint at your fingertips? Every SharePoint Developer & admin needs this! Fast and Flexible Great for LastMile Problems. New MSFT std for command line & remote admin. No Visual Studio.
  • #7  Who would like another tool in their toolbox, one that will help them with SharePoint, but also with many other things? How about the swiss army knife of tools? It will make you a better developer!
  • #9 Who here already has installed and PowerShell? Win 2003: http://www.microsoft.com/windowsserver2003/technologies/management/powershell/download.mspx Win 2008 – It’s an OS Feature – “servermanagercmd –install PowerShell” Win2008R2 – it’s already installed
  • #15 One is SharePoint aware, the other isn’t!
  • #16 Download SPO Cmdlets here: http://www.microsoft.com/en-us/download/details.aspx?id=35588
  • #21 Name that Language… Similar to @variables in SQL, apparently also Ruby, PHP. Arrays start at 0
  • #23 Mention Aliases
  • #24 Like a UNIX Manpage!
  • #25 Get-Member examples
  • #26 Get Command Examples
  • #27 Intellisense!
  • #30 Ask if anyone has unix scripting experience, and what they think of this. Can you imagine trying to do this in unix or vbscript?
  • #33 Get-Help about_flow_control Mention that collections start at 0
  • #34 Ask if anyone has unix scripting experience, and what they think of this. Can you imagine trying to do this in unix or vbscript?
  • #44 If you are familiar with these objects, raise your hand.
  • #48 Now that you know all the fundamentals, Jack will show you how to apply them
  • #51 http://technet.microsoft.com/en-us/magazine/ff458353.aspx
  • #52 Try to use an existing verb. You can make your own Noun Prefix if you want.
  • #54 about_comment_based_help
  • #58 Your mother doesn’t work here! You must clean up after yourself.
  • #59 http://sharepoint.microsoft.com/blog/Pages/BlogPost.aspx?PageType=4&ListId={72C1C85B-1D2D-4A4A-90DE-CA74A7808184}&pID=524 http://msdn.microsoft.com/en-us/library/ee557362.aspx See Assignment collections - http://technet.microsoft.com/en-us/library/ff607664.aspx
  • #68  Who would like another tool in their toolbox, one that will help them with SharePoint, but also with many other things? How about the swiss army knife of tools? It will make you a better developer!
  • #70 Michael
  • #71 Once used a choice column’s values instead of a CSV
  • #72 Thanks for the VM, Cloudshare!