![Page 1: PowerShell for SharePoint or Administrators Can Use the Object Model, Too 30 th October 2008 Sergey Zelenov Premier Field Engineer Microsoft Corporation](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649d6b5503460f94a4a307/html5/thumbnails/1.jpg)
PowerShell for SharePointor
Administrators Can Use the Object Model, Too
30th October 2008
Sergey ZelenovPremier Field EngineerMicrosoft Corporation
![Page 2: PowerShell for SharePoint or Administrators Can Use the Object Model, Too 30 th October 2008 Sergey Zelenov Premier Field Engineer Microsoft Corporation](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649d6b5503460f94a4a307/html5/thumbnails/2.jpg)
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
![Page 3: PowerShell for SharePoint or Administrators Can Use the Object Model, Too 30 th October 2008 Sergey Zelenov Premier Field Engineer Microsoft Corporation](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649d6b5503460f94a4a307/html5/thumbnails/3.jpg)
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!
![Page 4: PowerShell for SharePoint or Administrators Can Use the Object Model, Too 30 th October 2008 Sergey Zelenov Premier Field Engineer Microsoft Corporation](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649d6b5503460f94a4a307/html5/thumbnails/4.jpg)
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
![Page 5: PowerShell for SharePoint or Administrators Can Use the Object Model, Too 30 th October 2008 Sergey Zelenov Premier Field Engineer Microsoft Corporation](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649d6b5503460f94a4a307/html5/thumbnails/5.jpg)
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
![Page 6: PowerShell for SharePoint or Administrators Can Use the Object Model, Too 30 th October 2008 Sergey Zelenov Premier Field Engineer Microsoft Corporation](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649d6b5503460f94a4a307/html5/thumbnails/6.jpg)
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
![Page 7: PowerShell for SharePoint or Administrators Can Use the Object Model, Too 30 th October 2008 Sergey Zelenov Premier Field Engineer Microsoft Corporation](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649d6b5503460f94a4a307/html5/thumbnails/7.jpg)
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
![Page 8: PowerShell for SharePoint or Administrators Can Use the Object Model, Too 30 th October 2008 Sergey Zelenov Premier Field Engineer Microsoft Corporation](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649d6b5503460f94a4a307/html5/thumbnails/8.jpg)
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!
![Page 9: PowerShell for SharePoint or Administrators Can Use the Object Model, Too 30 th October 2008 Sergey Zelenov Premier Field Engineer Microsoft Corporation](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649d6b5503460f94a4a307/html5/thumbnails/9.jpg)
DEMO: PowerShell First Steps
Sergey ZelenovPremier Field EngineerMicrosoft Corporation
![Page 10: PowerShell for SharePoint or Administrators Can Use the Object Model, Too 30 th October 2008 Sergey Zelenov Premier Field Engineer Microsoft Corporation](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649d6b5503460f94a4a307/html5/thumbnails/10.jpg)
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
![Page 11: PowerShell for SharePoint or Administrators Can Use the Object Model, Too 30 th October 2008 Sergey Zelenov Premier Field Engineer Microsoft Corporation](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649d6b5503460f94a4a307/html5/thumbnails/11.jpg)
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”)
![Page 12: PowerShell for SharePoint or Administrators Can Use the Object Model, Too 30 th October 2008 Sergey Zelenov Premier Field Engineer Microsoft Corporation](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649d6b5503460f94a4a307/html5/thumbnails/12.jpg)
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
![Page 13: PowerShell for SharePoint or Administrators Can Use the Object Model, Too 30 th October 2008 Sergey Zelenov Premier Field Engineer Microsoft Corporation](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649d6b5503460f94a4a307/html5/thumbnails/13.jpg)
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
![Page 14: PowerShell for SharePoint or Administrators Can Use the Object Model, Too 30 th October 2008 Sergey Zelenov Premier Field Engineer Microsoft Corporation](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649d6b5503460f94a4a307/html5/thumbnails/14.jpg)
DEMO: Parsing with PowerShell
Sergey ZelenovPremier Field EngineerMicrosoft Corporation
![Page 15: PowerShell for SharePoint or Administrators Can Use the Object Model, Too 30 th October 2008 Sergey Zelenov Premier Field Engineer Microsoft Corporation](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649d6b5503460f94a4a307/html5/thumbnails/15.jpg)
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
![Page 16: PowerShell for SharePoint or Administrators Can Use the Object Model, Too 30 th October 2008 Sergey Zelenov Premier Field Engineer Microsoft Corporation](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649d6b5503460f94a4a307/html5/thumbnails/16.jpg)
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)
}
![Page 17: PowerShell for SharePoint or Administrators Can Use the Object Model, Too 30 th October 2008 Sergey Zelenov Premier Field Engineer Microsoft Corporation](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649d6b5503460f94a4a307/html5/thumbnails/17.jpg)
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)}
![Page 18: PowerShell for SharePoint or Administrators Can Use the Object Model, Too 30 th October 2008 Sergey Zelenov Premier Field Engineer Microsoft Corporation](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649d6b5503460f94a4a307/html5/thumbnails/18.jpg)
DEMO: PowerShell and SharePoint Object Model
Sergey ZelenovPremier Field EngineerMicrosoft Corporation
![Page 19: PowerShell for SharePoint or Administrators Can Use the Object Model, Too 30 th October 2008 Sergey Zelenov Premier Field Engineer Microsoft Corporation](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649d6b5503460f94a4a307/html5/thumbnails/19.jpg)
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
![Page 20: PowerShell for SharePoint or Administrators Can Use the Object Model, Too 30 th October 2008 Sergey Zelenov Premier Field Engineer Microsoft Corporation](https://reader036.vdocuments.us/reader036/viewer/2022062407/56649d6b5503460f94a4a307/html5/thumbnails/20.jpg)
© 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.