sharepoint powershell for the admin and developer - a venn diagram experience
DESCRIPTION
http://www.sharepointcowbell.comTRANSCRIPT
#DogFoodCon
SharePoint PowerShell for the Admin & DeveloperA Venn Diagram Experience
Ryan Dennis | Ricardo Wilkins
#DogFoodCon
What is PowerShell?PowerShell and SharePoint – the evolutionScripts vs. code – comparisonBusiness case walk throughCode integration with PowerShellArchitectural discussions
What we’re barking about
#DogFoodCon
www.SharePointCowBell.com
@ricardo303
@spcowbell
about.me/ricardowilkins
Ricardo WilkinsConsultant, Blue Chip Consulting Group
Ryan DennisConsultant, Blue Chip Consulting Group
www.SharePointRyan.com
@SharePointRyan
linkedin.com/in/sharepointryan
Meet the Mutts
#DogFoodCon
What’s Your Breed?
#DogFoodCon
…is a task-based command-line shell and scripting language designed especially for Windows system administration
…has a task-based scripting language
…includes powerful object manipulation capabilities
…is built on the .NET Framework
What is PowerShell?
#DogFoodCon
PowerShell deals with Objects, not Strings
• In order to find out what you can and cannot do or see on an object, use the Get-Member cmdlet
• Get-Member will return all Methods and Properties associated with whatever item you pass to it
• PowerShell uses a Verb-Noun syntax for its Cmdlets• Get-Something• Set-Something• New-Something
#DogFoodCon
The Pipeline
• PowerShell passes objects, that is – when you do something like Get-Process, you’re retrieving process object(s) – you can then pipe that output, or that process object to Stop-Process, Select-Object, Where-Object, etc.
• Use the built-in $_ variable to get values from the current object in the pipeline…
• Let’s talk about this metaphorically…
#DogFoodCon
The PipelineGet-DogBreed
Result: 2 Breeds
Get-DogBreed | Where-Object {$_.Group –eq “Herding”}
Get-DogBreed | Where-Object {$_.Group –eq “Herding” –and $_.Name –match “*shepherd*”}
Result: 25 Breeds
Result: 178 Breeds
#DogFoodCon
SharePoint 2010 Cmdlets
• 500+ Cmdlets…•MUCH better than STSADM.exe…• Can automate complete installations and
configurations…• Still doesn’t answer every scenario, leaving
gaps in functionality…• Example: Get, New and Remove SharePoint Groups –
no cmdlet, easy to write a custom function though…
#DogFoodCon
As a Developer, why do I care?• Not always necessary to write code
• Use PowerShell to handle things you could do in C#
• Don’t write console apps, write PowerShell Scripts!
• Some clients don’t allow managed code deployments, but PowerShell is A-OK
#DogFoodCon
Managing Solutions & Features
Farm Solutions• Add-SPSolution• Get-SPSolution• Install-SPSolution• Remove-SPSolution• Remove-
SPSolutionDeploymentLock
• Uninstall-SPSolution• Update-SPSolution
Sandboxed Solutions• Add-SPUserSolution• Get-SPUserSolution• Install-
SPUserSolution• Remove-
SPUserSolution• Uninstall-
SPUserSolution• Update-
SPUserSolution
Features• Disable-SPFeature• Enable-SPFeature• Get-SPFeature• Install-SPFeature• Uninstall-SPFeature
#DogFoodCon
Retrieving SharePoint Objects•Multiple ways to get a Site Object using
PowerShell…• $Site = Get-SPSite• $Site = New-Object Microsoft.SharePoint.SPSite($Url)
•Multiple ways to get a Web Object using PowerShell…• $Web = $Site.RootWeb• $Web = $Site.OpenWeb()• $Web = Get-SPWeb
#DogFoodCon
The Evolution of SP Scripting
• About 200 cmds• No native support
for PowerShell*• STSADM was it
• Over 500 PowerShell cmdlets
• PowerShell Version 2.0
• Over 700 PowerShell cmdlets
• PowerShell Version 3.0
*You could use PowerShell by loading the Microsoft.SharePoint Assembly…
#DogFoodCon
Pros & Cons of Script vs. Code
Script ProsQuicker to writeEasier to edit (open a file, edit it)No need to install a DLL to the serverCan access the hundreds (thousands?) of other PowerShell cmdlets (Processes, Services, etc.)
Code ConsRequires more time to develop& deployEditing requires redeployment to serverRequires a DLL installation
#DogFoodCon
// register controls protected TextBox TextBoxMinutesPerSession; protected LinkButton LinkButtonUpdate; // Create a click event handler LinkButtonUpdate.Click += new EventHandler(LinkButtonUpdate_Click); void LinkButtonUpdate_Click(object sender, EventArgs e) { // Grab the site and web Guid Guid sitedId = SPContext.Current.Site.ID; Guid webID = SPContext.Current.Web.ID; //create and dispose of spweb and spsite objects using (SPSite oSPSite = new SPSite(sitedId)) {
using (SPWeb oWeb = oSPSite.OpenWeb(webID))
{ //Set the custom web property to the
textbox value
oWeb.Properties["CurrentUserSessionDuration"] = TextBoxMinutesPerSession.Text;
oWeb.Properties.Update(); }
} }
C# vs. PowerShell
$web = Get-SPWeb http://url$web.Properties["CurrentUserSessionDuration"] = "60"$web.Properties.Update()$web.Dispose()
Both code snippets add or set the “CurrentUserSessionDuration”
property in a Web.
PowerShell
C#
#DogFoodCon
The Business CaseContoso just purchasedAdventureWorks – Yay!
AdventureWorks Intranet Contains 100’s of site
collections, thousands of sub sites
Renaming them all through
the UI would take (dog) days
#DogFoodCon
The Personas
Developer Admin
Business Analyst /
Project Manager
#DogFoodCon
Get-Process –Name “Demo” | Start-Process
#DogFoodCon
Developer + PS
IIS ServerSharePoint
PS1
ChangeTitles
LogListItems
CallPowerShell
SPList
#DogFoodCon
SharePoint
-Classic Web Part-Visual Web Part-SP2013 App Part
#DogFoodCon
CallPowerShell
#DogFoodConhttp://ilovesharepoint.codeplex.com/wikipage?title=Execute%20PowerShell%20Script%20Action
CallPowerShell
#DogFoodCon
-Files reside in e.g. C:\Scripts-Scripts calling scripts as functions-Storing scripts in source control (TFS)-BA/PM viewing scripts vs code
#DogFoodCon
function Write-SPAppLogItem {[CmdletBinding()]Param([Parameter(Mandatory=$true)][System.String]$WebUrl,[Parameter(Mandatory=$true)][System.String]$ListName)$web = Get-SPWeb $WebUrl$list = $web.Lists[$ListName]$item = $list.Items.Add()$date = Get-Date$item["Title"] = "Operation completed successfully: $date"$item.Update()$web.Dispose()}
LogListItems
SPList
#DogFoodCon
LogListItems
SPList
-BA/PM responsible for this list-Workflow can kickoff on New Item-Other apps, or Search, can pull from this list
#DogFoodCon
Why is this worth chewing on?
Separation of Devs vs OpsDevs maintain the UIOps maintains the Title Change process
Separation of Concerns / Single Responsibility pattern
#DogFoodCon
Other things to chew on?
PS in Office 365PS in AzurePS RemotingPS Workflow
#DogFoodCon
Barks from the Pack
Thoughts? Would this model work for you?Other ideas?
#DogFoodCon
>Get-Questions