Basic Windows PowerShell usage ➤ ➤ How to use common SharePoint 2010 cmdlets ➤ ➤ The SharePoint object model ➤ ➤ PowerShell is now the command-line tool for administering SharePoint 2010
Trang 1administering sharePoint 2010 with Windows Powershell
WhAt’S IN thIS chAPtER?
Basic Windows PowerShell usage
➤
➤
How to use common SharePoint 2010 cmdlets
➤
➤
The SharePoint object model
➤
➤
PowerShell is now the command-line tool for administering SharePoint 2010 In previous ver-sions of SharePoint, administrators worked with STSADM.exe Starting with SharePoint 2007, we began seeing some integration with PowerShell and SharePoint With no commands provided
we managed to create our own PowerShell commands and scripts for SharePoint 2007 To do
so some of us had to get our hands dirty and either put on our developer hat or beg, borrow, and steal from friends on the Internet Ultimately, we ended up with some PowerShell scripts for SharePoint 2007 even if we really didn’t understand how they worked under the covers With the
2010 version of SharePoint, the use of PowerShell for administration is different, much different Now we have commands and a management shell that installs with SharePoint 2010 — and when we say commands, we mean a lot of commands, well over 500 STSADM only boasted around 185 commands More important, these 500 plus commands can be chained together and scripted, enabling you to create practically an infi nite number of scripts and commands
In this chapter we will get you up to speed on PowerShell in general, and then show you how
to harness the power of this beast to benefi t your SharePoint 2010 farm
10
Trang 2INtROductION tO WINdOWS POWERShELL
There is no requirement that you must use PowerShell to administer SharePoint 2010 Basically every administrative function is exposed through the SharePoint user interface Many administra-tive tasks are easily done via Central Administration or Site Settings While it is conceivable that a SharePoint 2010 administrator could avoid using PowerShell, what fun would that be? PowerShell really starts to shine when you want to perform repeatable processes or multiple tasks
For example, you can create site collections using Central Administration — one site collection
at a time But with a little knowledge of PowerShell and the SharePoint commands you can create
a comma-separated list of many site collection URLs and supporting information, and then use PowerShell’s Import-CSV cmdlet with the New-SPSite cmdlet to create them with a single click of the Enter key
If you are worried that STSADM is no longer available, rest assured that STSADM
is still around, for now The writing is on the wall, however; PowerShell is now the
command-line tool for SharePoint administrators Microsoft says STSADM is
dep-recated, which is a fancy way of saying it will be removed in a future version Start
saying goodbye to STSADM.
mIcROSOFt ShAREPOINt 2010 mANAgEmENt ShELL
ANd OthER hOStS
PowerShell is included in Microsoft Windows Server 2008 and is activated as a feature However, it is version 1.1, and SharePoint 2010 requires version 2 Because PowerShell version 2 is a prerequisite for SharePoint 2010, the prerequisite installer will try to install it You’ll get an error if the PowerShell version 1.1 feature is activated Simply deactivate it and run the prerequisite installer to get the cor-rect version of PowerShell installed Windows Server 2008 R2 comes with PowerShell version 2 already installed and enabled You may get an error when the prerequisite installer runs, but you can ignore it The correct version of PowerShell is installed and patiently waiting for SharePoint to come out and play
microsoft SharePoint 2010 management Shell
The Microsoft SharePoint 2010 Management Shell is displayed in Figure 10-1 This is the default, out-of-the-box SharePoint command-line interface Not much to look at As a matter of fact, it looks a lot like the command console you used to run STSADM commands in SharePoint 2007
It is, however, a cleverly disguised PowerShell console, with the PowerShell commands registered and ready for use Open the SharePoint 2010 Management Shell and PowerShell at the same time
If you squint, you might see that they look pretty similar The only obvious difference is that the
Trang 3SharePoint Management Shell has a black background, while the standard PowerShell background
is blue They may look similar, but only the Management Shell will run the SharePoint commands without further confi guration
You can run STSADM commands inside of PowerShell Add the path to STSADM
to the system’s path variable and call all the STSADM commands you like.
FIguRE 10-1
Run a very basic SharePoint command in both consoles — for example, Get-SPSite The Management Shell will happily return a list of site collections, whereas the PowerShell console won’t have a clue what you want it to do
In other words, the big secret is that the SharePoint 2010 Management Shell is just the PowerShell
.exe with a command-line parameter that points to a PSC1 fi le or a console fi le The PSC1 fi le tells the PowerShell host to register the SharePoint commands One other minor difference is that it also has a title bar that says SharePoint 2010 Management Shell, but otherwise the Management Shell is all PowerShell
using Other Windows PowerShell hosts
While the Management Shell is the only host that is confi gured to run SharePoint commands, it is not the only host that an administrator can use Administrators can choose to work with other PowerShell hosts Two common hosts are available with Windows Server 2008 and Windows Server 2008 R2: Windows PowerShell and Windows PowerShell Integrated Scripting Environment, more commonly known as ISE ISE is displayed in Figure 10-2 Administrators can also use many of the third-party PowerShell hosts available free or for purchase Many of these boast a rich graphical user interface The rest of this chapter sticks to the Management Shell because it is the lowest common denomina-tor, but we realize that many of you will want to know how to confi gure these other hosts to use the SharePoint 2010 PowerShell commands, so we cover that here
Trang 4FIguRE 10-2
Adding commands is similar for all hosts We will be adding commands to the PowerShell ISE host ISE provides a multi-line editor with breakpoints and other help for scripting You might need to consult the documentation for other third-party hosts
ISE is installed with Windows Server 2008 and Windows Server 2008 R2 but it is not activated
To activate ISE, use the Add Feature screen of the Server Manager Figure 10-3 shows the Server Manager’s Add Feature screen with the ISE feature checked
To register the SharePoint PowerShell commands with ISE you must be working locally on a SharePoint 2010 server The SharePoint 2010 PowerShell commands do not remote
SharePoint 2010 commands must be run on a SharePoint 2010 server The
com-mands cannot be run from a client PowerShell 2 does provide a remoting scenario
whereby commands can be executed from a client to run on the server This is a
PowerShell function and not specifi c to SharePoint 2010 To learn more about
confi guring Windows PowerShell remoting with SharePoint 2010, read this blog
post:
http://blogs.msdn.com/opal/archive/2010/03/07/sharepoint-2010-with-windows-powershell-remoting-step-by-step.aspx.
Use the Add-PSSnapin PowerShell command to register the SharePoint PowerShell commands:
Add-PSSnapIn Microsoft.SharePoint.PowerShell
Add-PSSnapIn is a PowerShell command that will register commands The Microsoft.SharePoint PowerShell snap-in contains the registration information for the SharePoint commands
Trang 5FIguRE 10-3
This will get you started To verify that the SharePoint 2010 commands are available in ISE, simply run a SharePoint command such as Get-SPSite ISE should respond with a list of site collections on the farm Note one tiny issue: You are required to add the Microsoft.SharePoint.PowerShell snap-in every time you start the host To avoid this, you can run the command to add the snap-in to your profile, which will run each time you start a host
You can locate your profile by typing $Profile into the command line, which will return the path
to your profile $Profile is a variable containing the current user’s profile location The profile loca-tion may be different for each host The ISE profile is different from the PowerShell profile, so com-mands for the ISE user’s profile will not interfere with other PowerShell hosts If the profile exists already, you can use the following command to open your profile:
Notepad $Profile
If the profile exists, Notepad will open it If the profile does not exist but the directory exists, Notepad will prompt you to create a new file Add the Add-PSSnapin command with the Microsoft.SharePoint
.PowerShell value into your ISE profile and save it You will need to restart ISE to read in the profile changes When you need a command, variable, or function to persist between sessions, you can use the profile
Trang 6PowerShell is powered by commands Commands get things done There are four basic types of commands: cmdlets, functions, scripts, and native commands It is not necessarily important to know the differences among these command types to work with PowerShell and SharePoint, but
it doesn’t hurt either If you are just starting out with PowerShell, you can fi nd a wealth of general information at http://microsoft.com/PowerShell
cmdlets
Cmdlets (pronounced command-lets) are compiled commands that are registered with PowerShell They have a very specifi c naming convention: All cmdlets are named with a verb-noun combina-tion The verb portion of the name should be one of the PowerShell accepted verbs PowerShell defi nes a list of these verbs and what they mean at http://msdn.microsoft.com/en-us/library/ ms714428(VS.85).aspx Because the verbs are “standardized,” you can usually guess the verb portion
of the cmdlet An example of a cmdlet is New-SPWebApplication, which has a corresponding Remove
-SPWebApplication You might have expected to use the verb “delete” to delete a SPWebApplication, but delete is not one of the standardized verbs, so it cannot be used
Cmdlets are installed and registered As you might have already guessed, the SharePoint commands that we will be using are cmdlets You saw that SharePoint installed the cmdlets on the server, and the Management Shell registered them with the host using the PSC1 fi le; and you now know how to manually register them in the ISE You can also create custom cmdlets, but that is a topic beyond the scope of this book You can learn more about writing your own cmdlets in this MSDN article:
http://msdn.microsoft.com/en-us/library/dd878294(VS.85).aspx
Functions
Functions can contain one or more commands to accomplish a task, and can accept parameters that allow the creation of custom reusable code They are defi ned in PowerShell by typing the function into the host PowerShell then compiles the function and verifi es the syntax Functions can be reused for the lifetime of the host session If you close the host, then you lose the function; and it will need
to be entered again when you start another session with the host You can enter functions into your profi le so they will be available whenever you start a session
There’s nothing worse than working on an award-winning function, only to lose
it when you shut down the host Consider yourself warned and save your
func-tion in your profi le or at least to a text fi le so you can copy it later.
Functions have their place and are great for containing code that you will want to call from various other scripts or commands, but with a single chapter for administering SharePoint using PowerShell,
we have no space to cover them in depth here