powershell for sharepoint developers

26
Boulos Dib November 16, 2011 http://blog.boulosdib.com @boulosdib

Upload: boulos-dib

Post on 15-Jan-2015

1.215 views

Category:

Education


0 download

DESCRIPTION

PowerShell for Sharepoint developers is a follow-up presentation to the NYC Sharepoint Developers user group. Session presented on 2011-11-16.

TRANSCRIPT

Page 1: PowerShell for SharePoint Developers

Boulos Dib

November 16, 2011

http://blog.boulosdib.com @boulosdib

Page 2: PowerShell for SharePoint Developers

PS> Describe-Presenter

Independent Consultant – Napeague Inc. Professional Software Development since 1983 Few Facts

First Personal Computer 1980 – TRS-80 III First Z80 based product (EPROM based Protocol Adaptor – 1984) First Commercial PC-DOS product (Telex on PCs – 1985) Started 16-bit Windows Development using Win 3.1 Developed on: 8080/Z80, 68xxx, PDP/RSX,VAX-VMS and x86/x64 (C/C++/C#) Worked with PowerShell since Monad (2006) Worked with SharePoint since STS (2003) More facts

Favorite sport – Windsurfing Favorite hobby – Playing my sunburst Fender Stratocaster+ guitar. Favorite guitar players

Wes Montgomery, Larry Carlton and Ritchie Blackmore (Deep Purple, Rainbow)

Page 3: PowerShell for SharePoint Developers

Agenda Quick PowerShell Review

SharePoint 2010 Management Shell

PowerShell (Functions, CmdLet)

SharePoint CmdLets by Group

Best Practices

Sample Scripts

Tools

Resources

Page 4: PowerShell for SharePoint Developers

PowerShell Review Interactive Command Shell

Programmatic Execution Environment

Dynamic Scripting Language

Extensible (CmdLets, .Net etc…)

Hosted (i.e. NuGet/PowerGUI vsix)

Management tool for Servers

Page 5: PowerShell for SharePoint Developers

Review - What is a shell “A shell is the piece of software that lets you access the functionality provided by the operating system. “ Bruce Payette - Co-Designer and Implementer of the PowerShell language. Example

Windows Explorer Command.com Cmd.exe Bash (Unix) PowerShell

Page 6: PowerShell for SharePoint Developers

Review - Consistent Environment

Common Parameters -Verbose -Debug -WarningAction -WarningVariable -ErrorAction -ErrorVariable -OutVariable -OutBuffer

Risk Mitigation Parameters (certainly critical in a production environment) What-If -Confirm

Whildcard support. All names and parameter value can support wildcard.

Pipeline

Page 7: PowerShell for SharePoint Developers

Where do I start Verb-XXNoun

Verbs: Get, Set, New, Write, Read

Nouns: Drive, Variable, Provider, Site, Collection

Most important command

Get-Help

Get-Help *

Get-Help *drive*

Get-Member

Get-Command

Page 8: PowerShell for SharePoint Developers

PowerShell Functions

Scripts

Error Handling

Parameters

Splatting

Remoting

PSSession

Page 9: PowerShell for SharePoint Developers

Functions Functions is a script block containing list of statements function small_files ($size = 100MB) {

Get-ChildItem c:\ | where { $_.length -lt $size -and

!$_.PSIsContainer}

}

To control how a function uses the pipeline, you use Begin, Process and End.

function pipelineFunction {

process {"The value is: $_"}

}

1,2,3 | pipelineFunction

Advanced functions allow you to write CmdLets using scripts instead of compiled code.

Page 10: PowerShell for SharePoint Developers

Scripts A Script file is a text file with .ps1 extension containing

one or more PowerShell command

A Script is a simple mechanism to re-use functionality.

To run a script on a remote computer, use the Invoke-Command and provide remote computer name as a parameter.

Scripts can accept parameters.

To run a script in the current session, we Dot-Source the . .\Script1.ps1

We can Scope Local or Global.

Page 11: PowerShell for SharePoint Developers

Error Handling Terminating vs. Nonterminating Errors

Trap

Throw – Sometime useful for parameters

function hi ($name=$(throw '$name is required')) {"Hi $name"}

Page 12: PowerShell for SharePoint Developers

Errors Handling – as in C# try { $wc = new-object System.Net.WebClient $wc.DownloadFile("http://www.contoso.com/MyDoc.doc") } catch [System.Net.WebException],[System.IO.IOException] { unable to download MyDoc.doc from http://www.contoso.com." } catch { "An error occurred that could not be resolved." }

Page 13: PowerShell for SharePoint Developers

Snapins, Providers and Drives Get-PSProvider | Format-List Name, PSSnapin

Get-PSSnapin

Get-PSProvider

Get-PSDrive

Page 14: PowerShell for SharePoint Developers

PowerShell Modules Get-Module –ListAvailable

Import-Module ActiveDirectory

Page 15: PowerShell for SharePoint Developers

SharePoint Management Shell SharePoint Management Shell differs slightly from

the Standard PowerShell:

Runspace Threading $Host.Runspace.ThreadOptions = "ReuseThread“

SharePoint Specific Snap-In Add-PSSnapin Microsoft.SharePoint.PowerShell

Page 16: PowerShell for SharePoint Developers

Threading & Proper Object Disposal $Host.Runspace.ThreadOptions = "ReuseThread"

Get-SPAssignment –Global

$spWeb = Get-SPWeb -Identity $url

$spWeb.TreeViewEnabled = $True

$spWeb.Update()

Stop-SPAssignment –Global

Page 17: PowerShell for SharePoint Developers

Sharepoint CmdLets Groups Farm

Application

Solution

Site Collection

Site

List and ListItem

Page 18: PowerShell for SharePoint Developers

Farm Backup-SPFarm

Get-SPFarm

Restore-SPFarm

Page 19: PowerShell for SharePoint Developers

Web Application Get-SPWebApplication

New-SPWebApplication

Remove-SPWebApplication

Set-SPWebApplication

Page 20: PowerShell for SharePoint Developers

Solutions Get-SPSolution

Add-SPSolution

Remove-SPSolution

Install-SPSolution

UnInstall-SPSolution

Update-SPSolution

Page 21: PowerShell for SharePoint Developers

Site Collections Backup-SPSite

Get-SPSite

Move-SPSite

New-SPSite

Remove-SPSite

Restore-SPSite

Set-SPSite

Page 22: PowerShell for SharePoint Developers

Sites Export-SPWeb

Get-SPWeb

Import-SPWeb

New-SPWeb

Remove-SPWeb

Set-SPWeb

Page 23: PowerShell for SharePoint Developers

Tools - Productivity Tool Url

PowerGUI http://PowerGUI.org/

PowerTab http://powertab.codeplex.com/

Community Extensions http://pscx.codeplex.com/

Quest ActiveRoles http://www.quest.com/powershell/activeroles-server.aspx/

ShowUI http://showui.codeplex.com/

Windows Automation Snapin for PowerShell

http://wasp.codeplex.com/

Page 24: PowerShell for SharePoint Developers

Visual Studio Hosted NuGet

http://nuget.org/

http://nuget.codeplex.com/

PowerGUI vsix

http://powerguivsx.codeplex.com/

StudioShell

http://studioshell.codeplex.com/

Page 25: PowerShell for SharePoint Developers

Books – PowerShell PowerShell in Action

Bruce Payette

“The book from the authority on PowerShell”

Windows PowerShell 2.0 Bible By Thomas Lee, Karl Mitschke,

Mark E. Schill, and Tome Tanasovski

http://powertoe.wordpress.com/ (Tome’s blog)

Page 26: PowerShell for SharePoint Developers

Books – PowerShell & SharePoint Automating Microsoft SharePoint

2010 Administration with Windows PowerShell 2.0

Gary Lapointe & Shannon Bray

http://blog.falchionconsulting.com/

http://sppowershell.com/

PowerShell for Microsoft Sharepoint 2010 Administrators.

Niklas Goude & Mattias Karlsson http://www.powershell.nu/ http://mysharepointofview.com/