PS > Add-SPShellAdmin -UserName powershell\nigo '
>> -database (Get-SPContentDatabase -Identity WSS_Content)
PS > Get-SPShellAdmin
UserName
--------
POWERSHELL\nigo
PS > Remove-SPShellAdmin -UserName powershell\nigo '
>> -database (Get-SPContentDatabase -Identity WSS_Content)
2. Managing Content Databases in SharePoint 2010
Windows
PowerShell includes eight cmdlets that you can use when working with
the SharePoint 2010 content database. To retrieve a list of all content
database cmdlets available, use Get-Command with the Noun parameter to list all cmdlets where the noun is SPContentDatabase:
PS > Get-Command -Noun SPContentDatabase
CommandType Name Definition
----------- ---- ----------
Cmdlet Dismount-SPContentDatabase Dismount-SPContentDatabase
Cmdlet Get-SPContentDatabase Get-SPContentDatabase [[-Id
Cmdlet Mount-SPContentDatabase Mount-SPContentDatabase [-N
Cmdlet New-SPContentDatabase New-SPContentDatabase [-Nam
Cmdlet Remove-SPContentDatabase Remove-SPContentDatabase [-
Cmdlet Set-SPContentDatabase Set-SPContentDatabase [-Ide
Cmdlet Test-SPContentDatabase Test-SPContentDatabase [-Id
Cmdlet Upgrade-SPContentDatabase Upgrade-SPContentDatabase [
We’ll explore how to use the first six cmdlets here.
Getting a SharePoint 2010 Content Database
The Get-SPContentDatabase
cmdlet is used to return one or more content databases in SharePoint
2010. You can retrieve a specific content database, a content database
based on a site collection, or a content database from a specified web
application.
Let’s start by retrieving all available content databases:
PS > Get-SPContentDatabase
Id : 96dfa348-42df-4e9b-bbc5-1f4e8ee1051e
Name : WSS_Content
WebApplication : SPWebApplication Name=SharePoint - 80
Server : SPServer01
CurrentSiteCount : 2
To retrieve content databases for a specific site collection, use the Site parameter:
PS > Get-SPContentDatabase -Site http://SPServer01
Id : 96dfa348-42df-4e9b-bbc5-1f4e8ee1051e
Name : WSS_Content
WebApplication : SPWebApplication Name=SharePoint - 80
Server : SPServer01
CurrentSiteCount : 2
It
is even possible to specify a web application and retrieve the content
databases attached to it. If the web application is associated with more
than one content database, all are returned.
PS > Get-SPContentDatabase -WebApplication "SharePoint - 80"
Id : 96dfa348-42df-4e9b-bbc5-1f4e8ee1051e
Name : WSS_Content
WebApplication : SPWebApplication Name=SharePoint - 80
Server : SPServer01
CurrentSiteCount : 2
Like SPSite and SPWeb, the SPContentDatabase
class has a large number of properties containing rich information,
which you can retrieve through the appropriate cmdlet. The Format-List cmdlet displays the information as a list. Used with the asterisk wildcard, it retrieves all properties:
PS > Get-SPContentDatabase | Format-List *
Figure 1 shows the output from this command.
Configuring the SharePoint 2010 Content Database
The Set-SPContentDatabase
cmdlet is used to change the properties of a content database. This
cmdlet allows you to change the maximum number of site collections that a
content database can host, change the status of the content database,
and specify the number of sites that can be created before a warning
event is generated.
We’ll start with modifying the maximum number of site collections allowed. First, let’s get the current value of the MaximumSiteCount property:
PS > (Get-SPContentDatabase -Site http://SPServer01).MaximumSiteCount
15000
Here’s how to change the value using the Set-SPContentDatabase cmdlet:
PS > Get-SPContentDatabase -Site http://SPServer01 |
>> Set-SPContentDatabase -MaxSiteCount 20000
PS > (Get-SPContentDatabase -Site http://SPServer01).MaximumSiteCount
20000
This example uses Get-SPContentDatabase to retrieve a content database, and then uses a pipeline to send the object to the Set-SPContentDatabase cmdlet. We then use the MaxSiteCount parameter and change the value to 20000, increasing it by 5,000 sites. When we check the MaximumSiteCount property, we can see that the value has been updated.
Next, let’s change the status of the content database to Offline:
PS > (Get-SPContentDatabase -Site http://SPServer01).Status
Online
PS > Get-SPContentDatabase -Site http://SPServer01 |
>> Set-SPContentDatabase -Status Offline
PS > (Get-SPContentDatabase -Site http://SPServer01).Status
Offline
In this example, we use the -Status parameter to change the status of the content database from Online to Offline. We verify that the change was made using the Get-SPContentDatabase cmdlet. It is just as simple to set the status to Online, as follows:
PS > Get-SPContentDatabase -Site http://SPServer01 |
>> Set-SPContentDatabase -Status Online
Attaching and Detaching a Content Database in SharePoint 2010
The SharePoint 2010
snap-in for Windows PowerShell offers two cmdlets that allow you to
attach and detach content databases to a SharePoint 2010 farm.
To detach a content database, use the Dismount-SPContentDatabase cmdlet:
PS > Get-SPContentDatabase -Site http://SPServer01 | Dismount-SPContentDatabase
Confirm
Are you sure you want to perform this action?
Performing operation "Dismount-SPContentDatabase" on Target "WSS_Content".
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?]
Help(default is "Y"): Y
As you can see,
Windows PowerShell displays a confirmation prompt asking if you are sure
that you want to perform the action. Cmdlets that perform actions that
involve a risk to the system or to user data often require a
confirmation. When you type Y or just press ENTER, the action is performed. If you now try to access your content database with the Get-SPContentDatabase cmdlet, nothing will be returned, since the content database is detached from the farm.
To attach a content database to a farm in SharePoint 2010, use the Mount-SPContentDatabase cmdlet:
PS > Mount-SPContentDatabase "WSS_Content" -DatabaseServer SPServer01 '
>> -WebApplication http://SPServer01
Id : 96dfa348-42df-4e9b-bbc5-1f4e8ee1051e
Name : WSS_Content
WebApplication : SPWebApplication Name=SharePoint - 80
Server : SPServer01
CurrentSiteCount : 2
Creating a New Content Database
Windows PowerShell lets you create new content databases with the New-SPContentDatabase cmdlet. The cmdlet has two required parameters:
The following example creates a new content database called MyContentDB and attaches it to a web application.
PS > New-SPContentDatabase -Name "MyContentDB" '
>> -WebApplication http://SPServer01:5077
Id : 8975393a-cc0a-4d68-ab69-078b1b870904
Name : MyContentDB
WebApplication : SPWebApplication Name=My WebApplication
Server : SPServer01
CurrentSiteCount : 0
Removing a Content Database in SharePoint 2010
To remove a content database with Windows PowerShell, use the Remove-SPContentDatabase cmdlet:
PS > Get-SPContentDatabase -Identity "MyContentDB" | Remove-SPContentDatabase
Confirm
Are you sure you want to perform this action?
Performing operation "Remove-SPContentDatabase" on Target "MyContentDB".
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help
(default is "Y"): Y
Confirm
Removing 'MyContentDB' will permananetly delete the SQL database, permanently deleting all content stored within it.
Use Dismount-SPContentDatabase if you do not want to delete the SQL database.
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help
(default is "Y"): Y
In this example, we use Get-SPContentDatabase to retrieve a content database and a pipeline to send the current object to the Remove-SPContentDatabase cmdlet. As with the Dismount-SPContentDatabase cmdlet, when you use Remove-SPContentDatabase, Windows PowerShell asks for confirmation before performing the operation.