View
218
Download
2
Embed Size (px)
Citation preview
PowerShell for SharePointor
Administrators Can Use the Object Model, Too
30th October 2008
Sergey ZelenovPremier Field EngineerMicrosoft 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 1003.2
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!!
Shell Scripting Language
Aliases Modules
Wildcard matching Debugging
Starting other programs Script-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 ZelenovPremier Field EngineerMicrosoft 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=12.0.0.0 , 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 http://sharepoint)
Parsing log files
- Select-String cmdlet can be used for finding specific strings in ULS and IIS
logs
Select-String “Timer” $splogs\*20080419*\.log
DEMO: Parsing with PowerShell
Sergey ZelenovPremier Field EngineerMicrosoft 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] <string> [[-argumentList] <Object[]>] [<CommonParameters>]
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].GetProperties($bindflag);$props | % {"{0}: {1}" -f $_.Name, $_.GetValue($site.ContentDatabase,$null)}
DEMO: PowerShell and SharePoint Object Model
Sergey ZelenovPremier Field EngineerMicrosoft Corporation
Resources
• PowerShell home page
http://www.microsoft.com/powershell
• Windows PowerShell Scripts in the TechNet Scripting Center
http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx
• PowerShell Pack for SharePoint
http://code.msdn.microsoft.com/psp4sp
• PowerGUI
http://www.powergui.org
• CodePlex
http://www.codeplex.com
• Zach Rosenfield’s Blog
http://sharepoint.microsoft.com/blogs/zach
• From The Field blog
http://sharepoint.microsoft.com/blogs/fromthefield
© 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.