Tải bản đầy đủ (.pdf) (8 trang)

Thủ thuật Sharepoint 2010 part 41 pptx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (632.92 KB, 8 trang )

Using SharePoint Commands

261
Due to space limitations, this chapter doesn’t cover many other formatting commands and even out-
put commands to pipe content to fi les, including CSV. To learn more, use these two help commands:
Get-Help Format
Get-Help Out
FIGURE 1011
To clear the screen, use CLS, which is an alias for Clear-Host.
USING SHAREPOINT COMMANDS
With more than 500 individual SharePoint 2010commands, one chapter cannot possibly cover them
all. This section will get you started working with some of the more common SharePoint commands
for PowerShell.
Working with the Farm
The Farm is the top-most SharePoint administrator object, containing key properties and collections
associated with the SharePoint farm. The term “farm” can be confusing, especially in a single-sever
environment. A farm is one or more servers that share the same confi guration database. The associa-
tion of farm and confi guration database is carried through as the farm’s name is the confi guration
database name.
In SharePoint 2007, we used the
Farm object quite often when we scripted commands to get to all
the collections and properties. With the many SharePoint 2010 commands, you can now access
many of these properties using a more specifi c command. The farm still contains many key proper-
ties and methods that can be used for administration. For example, you can determine the farm’s
status, display name, and version.
You can access the farm using the
Get-SPFarm command (refer back to Figure 10-9). The output
of this command is nothing special. The default formatting for the
SPFarm object is to display the
Status and DisplayName. However, you learned earlier how to control formatting of the objects, so
feel free to modify the output of the


SPFarm object.
262

CHAPTER 10 admiNisteriNg sharePoiNt 2010 With WiNdoWs PoWershell
To access a specific property on the SPFarm object, use dot notation (.). You could also do this using
a variable, but this has its own complications, which are covered at the end of this chapter when we
cover object disposal. For example, to access the
BuildVersion property of the SPFarm object, use
the following command:
(Get-SPFarm).BuildVersion
The parentheses tell the command that you want the BuildVersion property of the result of the
Get-SPFarm command. The result of the Get-SPFarm command is an SPFarm object. Without the
parentheses, PowerShell would interpret your command as “get the
BuildVersion property of the
Get-SPFarm command,” which does not have a BuildVersion property. Figure 10-12 shows the
result.
FIGURE 1012
There are quite a few methods for the SPFarm object, but we won’t cover any of these because
SharePoint includes specific commands for many of these methods, such as
backup and restore.
There’s no point spending time learning how to work with the object model when you can simply
call a command.
Besides viewing and accessing properties of the farm, you can also back up and restore the farm
using PowerShell and SharePoint commands. The backup command is
Backup-SPFarm; and the
restore command is
Restore-SPFarm. The purpose of these cmdlets is obvious thanks to the nice
verb-noun naming convention.
The backup command requires a
Backup method and Directory parameter. If you don’t include these

parameters, the command will prompt for them. The
BackupMethod can be Full or Differential.
The directory is the location where you want the files to be placed. Just supply the path, not the file-
name, for the
Directory parameter. There are also optional parameters for the Backup-SPFarm
command. Use
Get-Help Backup-SPFarm to see these optional parameters. One particularly interest-
ing parameter is the
ShowTree parameter. Figure 10-13 displays the Backup-SPFarm command and
its output.
Backing up would be useless if we could not restore the data.
Restore-SPFarm requires the Restore
method and a directory where the backup should be located. The
Restore method can be either
overwrite or new. Overwrite will overwrite the original location, whereas new will create a new
database. Figure 10-14 shows the help for
Restore-SPFarm.
Using SharePoint Commands

263
FIGURE 1013
FIGURE 1014
264

CHAPTER 10 admiNisteriNg sharePoiNt 2010 With WiNdoWs PoWershell
Retrieving Farm Configuration Information
We need to touch on the topic of the Get-SPFarmConfig command. This command will return farm-
level configuration information for items that are not on the
SPFarm object. Figure 10-15 displays the
output of the

Get-SPFarmConfig command.
FIGURE 1015
You can use the corresponding Set-SPFarmConfig to modify the values. It is a little bit more than a
single command. You need to get the
FarmConfig into a variable, change the value of the property,
and then pipe the modified
FarmConfig variable to the Set-SPFarmConfig command. Figure 10-16
shows the output.
FIGURE 1016
The commands in Figure 10-16 modify the farm’s WorkFlowBatchSize property. This property
determines how many workflows can be processed at one time by the farm. The default value for
this is 100. The lines above change this setting to 105 and writes the value back to the farm.
Working with Web Applications
Every SharePoint site collection is associated with a single Web application. The SPWebApplication
object represents a Web application. The Web application contains many properties that an adminis-
trator might want to look at, such as those associated with the Recycle Bin, Official File, List throt-
tling, and status. Using the SharePoint commands, you can list all Web applications including the
Central Administration Web application, create new Web applications, remove Web applications,
and modify Web application properties.
Using SharePoint Commands

265
First, let’s see all the Web applications on the farm. The Get-SPWebApplication command will
display all the Web applications on the farm except for the Central Administration Web application.
That is probably a good thing, as you probably don’t want to treat the Central Administration Web
application the same way we treat your content Web applications. To get all Web applications on a
farm, including the Central Administration Web application, use the
Get-SPWebApplication com-
mand with the
IncludeCentralAdministration switch parameter, as shown in Figure 10-17.

FIGURE 1017
Getting all the Web applications might not be exactly what you need. Sometimes you need a single
Web application. To retrieve a single Web application, you use the
Identity parameter. This
parameter is smart enough to accept a name, Url, or Id of the Web application. You rarely see the

Identity parameter actually named. Normally, you can omit the name of the parameter, and
name only the Url or Id, as shown in Figure 10-18.
FIGURE 1018
The SPWebApplication has a lot of properties and methods. To see the properties of this specific
Web application, you can pipe it to
Format-List as shown in Figure 10-19.
As Figure 10-19 demonstrates, the web application object has a lot of properties. To be honest, all
the SharePoint objects have a lot of properties. How are we supposed to discover all these proper-
ties and keep track of them? Fortunately we have a command for that.
Get-Member will return the
members of the object passed into it via the pipeline. To determine what properties and methods
are available on the
SPWebApplication object, use the following command (the output is shown in
Figure 10-20):
Get-SPWebApplication http://sharepoint | Get-Member
266

CHAPTER 10 admiNisteriNg sharePoiNt 2010 With WiNdoWs PoWershell
FIGURE 1019
FIGURE 1020
Using SharePoint Commands

267
If piping the output through Format-List gives you more information than one screen can handle,

you can additionally pipe it through the
More command:
Get-SPWebApplication http://sharepoint | Get-Member | More
This will give you one page of output at a time. Press the spacebar to see the next screen. You can
also scroll one line at a time by pressing the Enter key.
To access a specifi c property on the
SPWebApplication object, use the dot notation described ear-
lier. To access the Id of the http://sharepoint Web application, use the following command:
(Get-SPWebApplication http://sharepoint).Id
While it is nice to be able to access the Web applications, you might just want to create or remove
a Web application. It’s clear why you might want to remove a Web application, but why would you
want to create a new one using PowerShell? Creating a new Web application is a great way to repeat-
edly build out your demo environments. The
New-SPWebApplication cmdlet, shown below and in
Figure 10-21, creates a new Web application, which contains many parameters — some required and
some optional. The following simple example demonstrates how to create a new Web application.
To see all the parameters, use
Get-Help New-SPWebApplication.
New-SPWebApplication –Name “Portal” –Port 80 –HostHeader portal.contoso.com –Url
–ApplicationPool DemoAppPool –ApplicationPoolAccount (Get
-SPManagedAccount contoso\SP_serviceapps)
FIGURE 1021
When trying the code in Figure 10-21 you may get an error telling you that you
need “machine privileges.” This happens if your SharePoint 2010 Management
Shell was not started with the Run as Administrator option. Without elevated
permissions SharePoint can’t always access what it needs. If you get any errors
like this, make sure your management shell window has “Administrator:” at the
beginning of the title bar.
You can use the Get-SPWebApplication command to verify that the Web application was created,
but astute readers will notice that the newly created

SPWebApplication object was formatted for
the screen already. You could capture the
SPWebApplication in a variable or pass the object onto
another command via the pipeline. If you do capture the object in a variable, make sure you read
268

CHAPTER 10 admiNisteriNg sharePoiNt 2010 With WiNdoWs PoWershell
the section “Disposing of SharePoint Variables” at the end of this chapter. Notice that we are
calling the
Get-SPManagedAccount command to retrieve an SPManagedAccount object, which is
required for the AppPool account. You can see what managed accounts you have by using the
Get-SPManagedAccount command with no parameter, as shown in Figure 10-22.
FIGURE 1022
Now that you know how to create a web application using the SharePoint commands, it would be
useful to learn how to remove an
SPWeb application. It probably comes as no surprise that to remove
a web application, you use the
Remove-SPWebApplication command. This command requires you
to select a specific web application, which prevents the deletion of multiple applications at once.
Figure 10-23 shows how to remove the web application you just created.
FIGURE 1023
Notice how PowerShell is smart enough to prompt you before it destroys your precious information.
Yes, there is a way to “override” this helpful prompt, but we will leave that as an exercise for read-
ers who want to learn enough about PowerShell to change this setting and take responsibility for
the consequences. We don’t need any midnight phone calls when you accidentally delete your web
application.
Working with Site Collections
The site collection level is where life gets interesting, for a number of reasons. First, since the 2003
version of SharePoint there has been a disconnect between the way the site collections are referred to
in the Administration UI and object model. Next, the objects that you will be working with in this

section require the proper disposal; otherwise your application might just start to hiccup, or worse.

×