1. Trang chủ
  2. » Công Nghệ Thông Tin

Thủ thuật Sharepoint 2010 part 42 pdf

8 249 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 8
Dung lượng 573,11 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Like other objects in SharePoint 2010, you can list, create, modify, and remove a site collection or SPSite using the SharePoint PowerShell commands.. To get a single site collection, si

Trang 1

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

Trang 2

Let’s start by clarifying that a site collection is represented in the SharePoint object model as an SPSite That should be enough clarifi cation for the purposes of this section We have looked at site collections in various chapters of this book already The site collection belongs to one and only one Web application The site collection is generally defi ned as a boundary for items such as content types and permissions Like other objects in SharePoint 2010, you can list, create, modify, and remove

a site collection or SPSite using the SharePoint PowerShell commands You can also back up and restore a site collection using PowerShell

The SPSiteAdministration commands Get-SPSiteAdministration and Set

-SPSiteAdministration allow administrators who do not have access to the site

collection to manage certain elements of it Use of the SPSite-based commands

assumes some amount of access to the site collection.

Let’s start by listing all site collections on the farm There are two common methods to do this If you do not need to list the Central Administration site collection (see the section “Working with Web Applications”), you can use the Get-SPSite command as described earlier in this chapter As usual, the default formatting for the SPSite object provides only a few items for display Figure 10-24 shows the Get-SPSite command in action

FIguRE 10-24

The Get-SPSite command, as well as other Get commands in SharePoint 2010, will return only

20 items before providing a warning that the list was limited These commands limit the number of objects returned for performance reasons You will greatly appreciate the limit functionality of the

Trang 3

FIguRE 10-25

Of course, sometimes you need to get a single site collection or possibly a smaller subset of site col-lections Using SharePoint 2010, along with some basic PowerShell commands, you have several ways to do just that

To get a specific site collection, you can use the Identity parameter Like the Identity parameter used with the Get-SPWebApplication command, you can provide the command with a few differ-ent values; and like the Get-SPWebApplication command, the actual word Identity is generally not seen In this case, the Identity parameter can use wildcards as well as regular expressions (when used in conjunction with the RegEx switch parameter) To get a single site collection, simply pass in the Url as a parameter to the Get-SPSite command, which is piped to the Format-List

command as demonstrated in Figure 10-26 Feel free to throw an asterisk into the mix to see what you get back

FIguRE 10-26

Trang 4

The Get-SPSite command also has an optional Filter parameter that will perform server-site filtering of site collections, which provides a faster way to limit the SPSites returned The Filter

parameter will limit the results of the Get-SPSite command using a script block A script block is

simply a block of script enclosed by brackets The Filter parameter can be used to filter on Owner,

Secondary Owner, and LockState Figure 10-27 shows the use of the Filter parameter and the script block The $_ represents the current object in the pipeline

FIguRE 10-27

Don’t forget about using the pipeline to filter your site collections The Filter parameter will only filter on Owner, Secondary Owner, or LockState What happens if you need to select on other properties? If you can’t seem to get from here to there using any of the preceding methods, you can always reach for the Where-Object command The Where-Object command is a PowerShell command that uses a script block to filter objects You commonly use the $_ reference to the current pipeline object to check a property and decide whether to keep the object or ignore it Use

Get-Help Where-Object to learn more about the Where-Object command The following example reaches out a little further and retrieves only those site collections that have a single SPWeb object, which is the root web:

Get-SPSite –Limit All |Where-Object {$_.allwebs.Count –eq 1}

Before removing any site collections, let’s look at the backup and restore options It’s a good practice

to back up the site in case you realize you really needed it after removing it

By now you should be able to guess that the command to back up a site is Backup-SPSite The site collection backup requires that you identify the site to back up and the path, including the filename, to save the backup image This command by itself will back up only a single site collection For example,

to back up the site.Contoso.com/teams/IT site collection, you would use the following command:

Trang 5

There’s a lot going on there, but when it’s broken down it’s easy to understand The first two cmd-lets get the list of web applications and site collections in the farm The next part walks through the list of site collections and for each one creates a variable named $FilePath that consists of C:\ backups\ plus the name of the site collection, with the protocol (http://) removed and any slashes

in the URL replaced with dashes Finally, you use your old friend Backup-SPSite to back up the current site collection to the location you just built with $FilePath So simple, yet so powerful You’ve probably already figured out that you would use the Restore-SPSite command to restore the backup The Restore-SPSite command requires the usual standard Identity and Path param-eter To restore the it.bak file, use the following command:

Restore-SPSite http://site.contoso.com/teams/IT -path c:\backups\contoso\it\it.bak

Now that you have a backup and know how to restore the site collection, it is time to finally remove the site collection To do that, you use the Remove-SPSite command Like other destructive commands, you will be prompted for each site collection you want to delete Although the Remove-SPSite will remove only one site collection, you are free to pass the SPSite object into the Remove-SPSite command using the PowerShell pipeline You can now appreciate the fact that PowerShell prompts you to allow the dele-tion of each and every site collecdele-tion

Now is also a good time to talk about PowerShell’s WhatIf parameter Well-behaved cmdlets that are potentially destructive in nature support the optional WhatIf switch parameter, and the Remove

-SPSite cmdlet is indeed one of those cmdlets If you add the WhatIf switch parameter to the

Remove-SPSite command, the command will not actually remove the site but instead indicate what will happen if you remove the WhatIf parameter Nice touch, isn’t it? It might not make sense if you are working with a single site but imagine if you ran this command:

$WebApps = Get-SPWebApplication -IncludeCentralAdministration

Now you have a variable that contains all Web applications Suppose you later decide to remove all site collections That is easy enough: $WebApps | Get-SPSite | Remove-SPSite That is fine until you realize, too late, that you just deleted the Central Administration site collection With the WhatIf

parameter, you are forewarned about the pain you are about to inflict on yourself Figure 10-28 shows you how a smart administrator can leave work on time

Consider using the WhatIf parameter whenever you use a command that might destroy your data

It will help avert those frantic late-night restores and a fair amount of swearing

FIguRE 10-28

Trang 6

Working with Webs

Site collections contain webs or SPWebs if you are speaking about SharePoint PowerShell commands Like the other main objects in the SharePoint hierarchy, you can list, add, modify, and remove webs Administrators tend to spend a lot of time working with webs because they are so numerous and this is where end users actually do their work The SPWeb object contains many items that end users work with, such as lists and libraries Now is probably a good time to let you know that there are

no commands to access objects below the SPWeb object This means that there are no commands for lists, libraries, or files, to name a few objects below the SPWeb object However, that does not mean you cannot access them via PowerShell — just that you will not find cmdlets specific to these objects You are free to access these objects via the object model

Listing all the webs of the farm is slightly different from listing SPWebApplications or SPSites The Get-SPWeb cmdlet requires at least one parameter It will not list all SPWebs on the farm if you omit the parameters On the plus side, you are allowed wildcards, regular expressions (with the use of the –RegEx switch parameter), and filters (with a script block), similar to Get-SPSite The

Identity parameter will also accept a relative path if the Site parameter is used

Let’s look at a few ways to list SPWebs starting with a single web To access a single SPWeb object, use the Identity parameter, passing in the Url as demonstrated in Figure 10-29

FIguRE 10-29

Now use the Filter parameter to filter your SPWebs on the server side, which performs better than returning all SPWebs for further filtering The Filter parameter can be used with the Template and

Title properties of the SPWeb Remember to use a script block with a Filter parameter The follow-ing command returns a list of SPWebs that are based on the Blank Team site:

Get-SPSite | Get-SPWeb –Filter {$_.Template –eq “STS#1”}

Trang 7

FIguRE 10-30

Creating a new web is similar to creating new site collections You use the New-SPWeb command and

a host of parameters to define the new SPWeb One of these parameters, Urlis required; but the rest are optional, such as Name and Template The following command creates a new SPWeb based on the Team Site template:

New-SPSWeb –Url http://site.contoso.com/teams/IT/SP2010,

-Template “STS#1” –Name “SP 2010 Implementation”

Once the web has been created, the SPWeb object is returned and displayed on the screen

You cannot back up or restore an individual web; that is reserved for the site collection and farm What you can do is export and import a web Because we just created a new web, let’s go ahead and export it To export the web, use the Export-SPWeb command, passing in the Identity and Path

parameters The Path parameter indicates where the exported web file will be placed, and it must include both a filename and the path Use the Export-SPWeb command to export your new web:

Export-SPWeb http://corpNet.contoso.com/ops -Path c:\ExportWeb\opsExport.cmp

You can import the web into an existing or new web The Import-Web command requires the iden-tity of the web to import to and the path to the exported web file The command to import your exported site to a new web is as follows:

Import-SPWeb http://corpNet.contoso.com/DemoImport -Path c:\ExportWeb\opsExport.cmp

Finally, you can remove your web by using the Remove-SPWeb command This command is similar

to the Remove-SPSite command It removes one web at a time, prompting users for confirmation along the way You can pipe an unlimited number of SPWebs into the remove command Don’t forget the earlier discussion of the WhatIf parameter As shown in Figure 10-31, use the WhatIf

parameter to see what would happen if you ran this command:

Get-SPSite |Get-SPWeb –Limit All |Remove-SPWeb –WhatIf

Trang 8

FIguRE 10-31

As you can see, the pipeline is very powerful Be careful when you pipe objects into destructive commands The WhatIf parameter and PowerShell’s confirm message will help to keep you out of trouble — that is, if you pay attention!

Working with Objects below the Web Level

As mentioned earlier, SharePoint PowerShell commands generally work from the SPWeb object and above That means you will not find commands such as Get-SPList or New-SPLibrary in the

Ngày đăng: 02/07/2014, 12:20

TỪ KHÓA LIÊN QUAN