PowerShell for SharePoint or Administrators Can Use the Object Model, Too 30 th October 2008 Sergey Zelenov Premier Field Engineer Microsoft Corporation
Agenda What is PowerShell anyway? History Ground principles Availability Future PowerShell and SharePoint Setting the scene... Using PowerShell’s parsing might Harnessing SharePoint Object Model
What is PowerShell anyway? History Idea based on a study commissioned by Microsoft in the early 2000s Inspired by Microsoft’s moving into the server marketplace Originally based on POSIX shell as specified in IEEE Influenced by Perl and UNIX shells Written in.NET providing direct access to the power of the framework Current version is 1.0, 2.0 is coming soon!
Shell or Scripting Language? Best of both worlds!! ShellScripting Language AliasesModules Wildcard matchingDebugging Starting other programsScript-Optimized Command history Command completion What is PowerShell anyway? Ground Principles
What is PowerShell anyway? Ground Principles Cmdlets -Use verb-noun pairs Get-Command Add-Content Copy-Item Read-Host Set-Date -Return objects Implemented by a.NET class that derives from the Cmdlet base class Objects -Everything is an object -Uses and extends.NET type system -Adapts objects through the PSObject layer -Native support for accessing.NET and COM classes
Pipelines -Series of commands separated by the pipe operator “|” get-wmiobject win32_logicaldisk | sort -desc freespace | select -first 3 | format-table -autosize deviceid, freespace -Pass output objects from one command to the next -Support streaming (in-process!) What is PowerShell anyway? Ground Principles
What is PowerShell anyway? Availability Available as a separate download (in fact a Windows Update) for: -Microsoft Windows XP Service Pack 2 (x86 and x64) -Microsoft Windows Vista (x86 and x64) -Windows Server 2003 (all editions, x86 and x64) Included in Windows Server 2008 as a Feature -Not installed by default but can be added at any time
What is PowerShell anyway? Future Exchange Management Shell is already based on Windows PowerShell All Microsoft server products are eventually to become PowerShell- compatible PowerShell a weapon of choice for next version of SharePoint – STSADM included for backward compatibility only!
DEMO: PowerShell First Steps Sergey Zelenov Premier Field Engineer Microsoft Corporation
Setting the Scene Prepare the environment for working with SharePoint Taking care of security -Execution policy is set to Restricted by default -Consider changing policy to RemoteSigned or Unrestricted to allow scripts to run Loading SharePoint assemblies -Use static methods of the System.Reflection.Assembly class LoadWithPartialName is obsolete but great for interactive sessions Load must be used in scripts
Setting the Scene Loading SharePoint Assemblies [System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”) [System.Reflection.Assembly]::Load(“Microsoft.SharePoint, Version= , Culture=Neutral, PublicKeyToken=71e9bce111e9429c”)
Setting the Scene Loading SharePoint Assemblies Windows SharePoint Services (WSS) Object Model -Microsoft.SharePoint -Microsoft.SharePoint.Security Microsoft Office SharePoint Server (MOSS) Object Model -Microsoft.Office.Server -Microsoft.Office.Server.Search -Microsoft.SharePoint.Portal -Microsoft.SharePoint.Publishing
Using PowerShell’s Parsing Might Parsing STSADM output -PowerShell supports XML documents as a primitive data type ([xml]) -Cast the output of an STSADM command to [xml] to use object notation $sites = [xml](Stsadm –o enumsites –url Parsing log files Select-String cmdlet can be used for finding specific strings in ULS and IIS logs Select-String “Timer” $splogs\* *\.log
DEMO: Parsing with PowerShell Sergey Zelenov Premier Field Engineer Microsoft Corporation
Harnessing SharePoint Object Model Working with objects Static classes don’t need to be instantiated SPFarm SPUtility SPEncode “::” operator is used to retrieve static members $farm = [Microsoft.SharePoint.Administration.SPFarm]::Local New-Object cmdlet New-Object [-typeName] [[-argumentList] ] [ ] Dynamic members are retrieved using ‘common’ “.” operator $farm.Servers $farm.Services $farm.Solutions
Harnessing SharePoint Object Model Power of Reflection.NET classes are self-describing Obtain maximum information about an object without explicitly specifying members $bindflag = $([System.Reflection.BindingFlags]::Instance, [System.Reflection.BindingFlags]::Public) $props = $object.GetType().GetProperties($bindflag) foreach($propinfo in $props) { “{0}: {1}” –f $propinfo.Name, $propinfo.GetValue($object, $null) }
Harnessing SharePoint Object Model Is it all this good? SPContentDatabase class is not CLR-compliant Solution? Use reflection! $props = [Microsoft.SharePoint.Administration.SPContentDatabase].GetPr operties($bindflag); $props | % {"{0}: {1}" -f $_.Name, $_.GetValue($site.ContentDatabase,$null)}
DEMO: PowerShell and SharePoint Object Model Sergey Zelenov Premier Field Engineer Microsoft Corporation
Resources PowerShell home page Windows PowerShell Scripts in the TechNet Scripting Center PowerShell Pack for SharePoint PowerGUI CodePlex Zach Rosenfield’s Blog From The Field blog
© 2008 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.