Thursday, August 11, 2011

SharePoint 2010: PowerShell and Stsadm

[What you have]:
SharePoint 2010 or SharePoint 2007 and desire to start using PowerShell

[What you want]:
You want to understand why you better off  STSADM? And you want small guidelines with PowerShell.

[What you want to know]:

 "STSADM has been officially deprecated by Microsoft and may not appear in the future version of SharePoint (after 2010). So it's a good idea to start reducing or eliminating your dependency on STSADM"
 (Automating SharePoint 2010 with Windows PowerShell 2.0)

The reason I have shifted to PowerShell one year ago while still working with  SharePoint 2007 was an emergency. We systematically abused SharePoint 2007 by changing schema of already existed list. At the some point on Prod we had the biggest list broken. The only way to fix it without restoring backup and keeping the desired schema changes is to use PowerShell! I have fixed the list in an hour on live Prod and reapplied changes directly on the list through SharePoint API. All of this was done in one tiny Powershell window. Feel grateful and felt in love with PowerShell forever...

What is PowerShell anyway? It's a scripting language.  And also it's an environment where the script is executing. It's quite easy to write a powerful command with some additional looping and filtering. You can build the same logic as using C#. In fact, PowerShell's capabilities are fully featured and mirror the capabilities of C# language, with only slight differences in syntax.

The source of well-known and stable scripts for 2007 SharePoint:
PowerShell:
http://sharepointpsscripts.codeplex.com/
STSADM:
http://blog.falchionconsulting.com/index.php/downloads/

Here is a great resource to borrow a PowerShell script for a particular need: http://gallery.technet.microsoft.com/scriptcenter .

Index of SharePoint Server 2010 Windows PowerShell cmdlets

Stsadm to Windows PowerShell mapping (SharePoint Server 2010)

SharePoint 2010 is shipped with PowerShell 2.0.In SharePoint 2010 PSSnapin (Microsoft.SharePoint.PowerShell) there are more than 530 cmdlets ("command-lets"). The great news is you don't need to learn all of them. Start easy.

Here are  few small hints for easy-going start:

  • Get-Help. The best way to learn PowerShell is to use help command.
  • Get-SP<noun> | Get-member . Make a habit to observe available methods and properties first. It help to write more appropriate script
  • -Whatif . Use the risk mitigation parameters. 
Get-Process  | Stop-Process -whatif. The command produces the following example - what would be performed if I executed the command without the -whatif parameter.
  • Set-ExecutionPolicy AllSignedThe AllSigned execution policy is best for production since it forces the requirement for digital signatures on all scripts and configuration files.  (Signing PowerShell Scripts)
  • To prevent memory leak while working with such objects as SPSite, SPWeb, SPSiteAdministration - become familiar with Start-SPAssignment and Stop-SPAssignment. By default PowerShell runs every command in the new thread. Consider for COM objects (such as SPSite, SPweb, SPSiteAdministration) set $Host.Runspace.ThreadOptions ="ReuseThread" (refer for an example to sharepoint.ps1)). Start-SPAssignment and Stop-Assegnment will not work correctly unlesss Reuse Thread is specified.
  • Refer to Stsadm to Windows PowerShell mapping for easy PowerShell adoption
  • In order to run script file (*.ps1) you need to change the default value of Execution Policy OR Sign the script. The best recommendation is not lower the restriction further than "RemoteSigned". But be aware  that ExecutionPolicy is not a security feature. It can be easily overcame.
Best practices:
 On prod create a custom profile $profile.AllUsersAllHosts where specify  :
- $ConfirmPreference="Low"  PowerShell will automatically question all actions to confirm;
Start-Transcript. It will record all Powershell activity 



P.S. SharePoint 2010 Management Shell is a just a PowerShell instance that loads sharepoint.ps1 script. So, you are free to use your own favorite IDE to build scripts, just load sharepoint.ps1 (Simple concept: How to use SharePoint cmdlets in PowerShell ISE)

[What you want to do]:
Are you brand new to PowerShell? To start easy and fast,  go to PowerShell and SharePoint: What, Why and How
Here is series of PowerShell introduction PowerShell Week: Learn It Now Before It's an Emergency