NZ Code Camp 2011 PowerShell + SharePoint

30
Code Camp NZ 2011 #CCNZ www.mscommunities.co.nz

description

Nick Hadlee's session on PowerShell and SharePoint 2010 at Code Camp 2011.

Transcript of NZ Code Camp 2011 PowerShell + SharePoint

Page 1: NZ Code Camp 2011 PowerShell + SharePoint

Code Camp NZ 2011

#CCNZ

www.mscommunities.co.nz

Page 2: NZ Code Camp 2011 PowerShell + SharePoint

Introduction

PowerShell and SharePoint extensively covered on TechNet2010 Products administration by using Windows PowerShellhttp://technet.microsoft.com/en-us/library/ee806878.aspx

Session Goals◦ Demystify PowerShell + SharePoint◦ Examples of PowerShell in action

Page 3: NZ Code Camp 2011 PowerShell + SharePoint

Session Agenda Quick review of the basics and some useful

Cmdlets

Walkthrough building a portal using PowerShell◦Farm Administration◦ Site Administration◦ Deployment of Customisations◦ Diagnostics and Troubleshooting

Tips and Resources

Page 4: NZ Code Camp 2011 PowerShell + SharePoint

basics

PowerShell Basics

Page 5: NZ Code Camp 2011 PowerShell + SharePoint

How Do I?Work with PowerShell + SharePoint SharePoint 2010 Management Shell

If you’re not using the SP2010 Management

Shell make sure $host.Runspace.ThreadOptions = "ReuseThread"

The PowerShell SharePoint Snapin provides the SP CmdletsIt’s always helpful include a reference to the Snapin

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

Page 6: NZ Code Camp 2011 PowerShell + SharePoint

What Do I Get?With PowerShell + SharePoint

SharePoint Cmdlets◦ 245 listed on TechNet for SharePoint Foundation◦ 527 listed on TechNet for SharePoint Server

(there’s more according to Get-Command)

Page 7: NZ Code Camp 2011 PowerShell + SharePoint

Cmdlets“…a lightweight command [for PowerShell]…”

Cmdlets return or operate on objects or collections

Use the pipeline | to pass and process objects$myObject | ForEach-Object { DoStuff $_ } ForEach %$myObject | Where-Object { $_ -gt 1 } Where ?

I need some help? Some helpful Cmdlets:Get-Help Get-Command

$myObject | Get-Member

Page 8: NZ Code Camp 2011 PowerShell + SharePoint

Assemblies and ClassesPowerShell is not just limited to Cmdlets

Create objects with New-Object$site = New-Object Microsoft.SharePoint.SPSite("http://tech.ed")

Load other SharePoint or .NET assemblies[System.Reflection.Assembly]::LoadWithPartialName

("Microsoft.Office.Server.Search")

[System.Reflection.Assembly]::Load("Microsoft.Office.Server.Search,

Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")

Deprecated

Page 9: NZ Code Camp 2011 PowerShell + SharePoint

Farm Administration

Page 10: NZ Code Camp 2011 PowerShell + SharePoint

Farm AdministrationAdding content to the farm

Adding Web Applications New-SPWebApplication Update the Web Application Get-SPWebApplication

Adding Site Collections New-SPSite

Content Databases◦ Adding new databases New-SPContentDatabase◦ Attaching existing databases Mount-SPContentDatabase

◦ Test them using Test-SPContentDatabase

Page 11: NZ Code Camp 2011 PowerShell + SharePoint

Farm AdministrationProtect and share the content

Backup and Restore◦ See the backup history Get-SPBackupHistory◦ Farm and more granular Backup-SPFarm and Restore-SPFarm

◦ Site Collection Backup-SPSite and Restore-SPSite

Search ◦ The SA Get-SPEnterpriseSearchServiceApplication◦ Content Sources Get-SPEnterpriseSearchCrawlContentSource

Page 12: NZ Code Camp 2011 PowerShell + SharePoint

demo

Farm Administration

Page 13: NZ Code Camp 2011 PowerShell + SharePoint

Site Administration

Page 14: NZ Code Camp 2011 PowerShell + SharePoint

Site Collections and SitesSites…Also known as SPWeb’s Adding sub sites New-SPWeb Change the site collection(s) and site(s)

properties ◦ Get-SPSite and Get-SPWeb

Site Permissions and Users◦ Add users and permissions New-SPUser and Set-SPUser

◦ Site collection administrators Set-SPSiteAdministration

Page 15: NZ Code Camp 2011 PowerShell + SharePoint

Site HierarchySPWeb has a lot of friends

The site’s hierarchy◦ Sub sites .Webs◦ Lists .Lists◦ Content Types .ContentTypes◦ Fields .Fields◦ . . .

Page 16: NZ Code Camp 2011 PowerShell + SharePoint

demo

Site Administration

Page 17: NZ Code Camp 2011 PowerShell + SharePoint

Customisation Deployment

and Diagnostics

Page 18: NZ Code Camp 2011 PowerShell + SharePoint

Customisations and Deployment Solutions

◦ Retrieve solutions in farm Get-SPSolution and download to file system .SolutionFile.SaveAs()

◦ Add/Deploy new solution to farm Add-SPSolution and Install-SPSolution

◦ Remove existing Solution to farm Remove-SPSolution and Uninstall-SPSolution

◦ Update existing farm solution Update-SPSolution

Features◦ List all features in farm Get-SPFeature◦ Install/Remove feature Install-SPFeature and Uninstall-SPFeature

◦ Activate/Deactivate features Enable-SPFeature and Disable-

SPFeature

Page 19: NZ Code Camp 2011 PowerShell + SharePoint

Diagnostics and Troubleshooting Developer Dashboard [Microsoft.SharePoint.Administration.SPWebService]

::ContentService.DeveloperDashboardSettings

Listening to SharePoint’s logging service (ULS) Get-SPLogEvent

Page 20: NZ Code Camp 2011 PowerShell + SharePoint

demo

Customisation Deployment

and Diagnostics

Page 21: NZ Code Camp 2011 PowerShell + SharePoint

tips

Resources and Tips

Page 22: NZ Code Camp 2011 PowerShell + SharePoint

Script Tips

Use and save scripts – they can be reused, maintained, reused, reused…PS C:\TENZ11\PS> .\myscript.ps1

Use variables – they make scripts easier to read and maintain$siteUrl = "http://intranet"

Use functions – pieces of logic in the scriptfunction Update-SiteTitle($site, $title) {

$site.Title = $title$site.Update()

} Use comments

# This explains what the script is doing which is helpful in the future Tab Ahead - PowerShell lets you tab to complete pathname and

input Providing some output is helpful

Write-Host “About to do stuff" vs. “About to do stuff" Build and test your scripts incrementally. Not directly in

production!

Page 23: NZ Code Camp 2011 PowerShell + SharePoint

Disposal TipsObject disposal is important to release memory

Easiest method is to use SPAssignmentStart-SPAssignment -Global# Do stuff ...Stop-SPAssignment –Global

You can use .Dispose() to clean up after yourself$site = Get-SPSite http://intranet; $site.Usage; $site.Dispose()

Closing the PowerShell Console cleans up the session but it’s the lazy option

Page 24: NZ Code Camp 2011 PowerShell + SharePoint

Scripts Won’t Run?Usually it will be permissions

What’s your Execution Policy?Set-ExecutionPolicy Restricted | AllSigned | RemoteSigned | Unrestricted

Is UAC on? You may have to “Run as Administrator”

Does the account have rights on databases?

Add-SPShellAdmin -UserName demo\SPUser -database SP2010_Content_TENZ

Page 25: NZ Code Camp 2011 PowerShell + SharePoint

Session TipsSome More Useful Cmdlets Let the farm document itself using Export-Clixml

http://bit.ly/psfarmdoc

Use Out-GridView to output to a Grid UINeeds PowerShell ISE feature enabled (not on by default)

Use Import-CSV to read from CSV files. Great input to other “creation” Cmdlets like New-SPUser and New-SPWebhttp://bit.ly/psimportcsv

Page 26: NZ Code Camp 2011 PowerShell + SharePoint

ResourcesUseful Blogs

Niklas Goudehttp://www.powershell.nu/

Zach Rosenfield http://sharepoint.microsoft.com/Blogs/zach

Gary Lapointehttp://blog.falchionconsulting.com/

Page 28: NZ Code Camp 2011 PowerShell + SharePoint

Track ResourcesInstallation

AutoSPInstallerhttp://autospinstaller.codeplex.com/

Install SharePoint Server 2010 by using Windows PowerShell (SPModule)http://technet.microsoft.com/en-us/library/cc262839.aspx

The Wizard Likes His GUIDs (Configuring Service App’s without the Wizard)http://todd-carter.com/post/2010/04/26/The-Wizard-Likes-His-GUIDs.aspx

Page 29: NZ Code Camp 2011 PowerShell + SharePoint

Content Slide

◦ 4th Annual Community SharePoint Conference

◦ Business and Technical Tracks, all levels

◦ Superb Internationally Renowned SharePoint Experts

Page 30: NZ Code Camp 2011 PowerShell + SharePoint

PremierPartners

Sponsor

Thanks to our sponsorsand partners!

AssociatedPartners

SupportingPartners