Administering and Automating Sharepoint - .Net

  • Automating Sharepoint with Windows Powershell
  • Administering Sharepoint with STsadm
A. Automating Sharepoint with Windows Powershell

A. 1. Adding Sharepoint Functionality to Windows Powershell

A.1.1. Load Sharepoint .dll files using .Net reflection

1. Log on SP2010-WFE1 as CONTOSO\SP_Admin with the its password.
2. In the Windows Quick Launch, click Windows Powershell.
Windows Powershell opens
3. To identify the assemblies that are currently loaded, type the following command and then press ENTER:


[AppDomain]:CurrentDomain.GetAssemblies() | ForEach-Object { Split-Path $_.Location - Leaf } | Sort

Microsoft.Sharepoint.dll is not in the list. To use the Microsoft Sharepoint object model, you must load the Sharepoint .dll files.

4. Type the following command and then press ENTER.

[System.Reflection.Assembly] : : LoadwithPartialName("Microsoft.Sharepoint")

The output displays global assembly cache (GAC), version, and location information for the assembly.

5. Repeat step 3 to display the loaded assemblies.

Tip: You can press the UP ARROW to scroll through previously executed commands.

The listing includes the Microsoft.Sharepoint.dll.

A.1.2. Add the Sharepoint snap-in using the Add-PSSnapin cmdlet

1. Type the following command and then press ENTER:
Get-PSSnapin

The output lists the snap-ins that have been added to the current session. The Sharepoint snap-in is not listed.

2. Type the following command and then press ENTER:

Get-PSSnapin -Registered.

The output lists the snap-ins that are registered on the system, except for those that are installed with Windows Powershell.

3. Type the following command and then press ENTER.

Add-PSSnapin Microsoft.Sharepoint.Powershell

4. Type the following command and then press ENTER:

Get-PSSnapin

The output lists the snap-ins that have been added to the current session. The Sharepoint snap-in is now added.
5. To identify the assemblies that are currently loaded, type the following command and then press ENTER:

[AppDomain] : : CurrentDomain.Getassemblies() | ForEach-Object { Split-Path $_.Location - Leaf } | Sort

The listing includes numerous Sharepoint assemblies.
Rather than loading each assembly one by one, the Add-PSSnapin cmdlet loads them all at once.


6. Close Windows Powershell.

A.1.3. Open Sharepoint 2010 Management Shell

1. Click Start, click All Programs, click Microsoft Sharepoint 2010 Products, and then click Sharepoint 2010 Management Shell.
2. Type the following command and then press ENTER:
Get-PSSnapin
The output lists the snap-ins that have been added to the current session. The Sharepoint snap-in is already added to the session.
3. To identify the assemblies that are currently loaded, type the following command and then press ENTER:

[AppDomain] : : CurrentDomain.Getassemblies() | ForEach-Object { Split-Path $_.Location - Leaf } | Sort


The listing demonstrates that Sharepoint 2010 Management Shell preloads the Sharepoint .dll files.

A. 2. Delegating the Ability to Use Windows PowerShell to Manage Sharepoint
A. 2.1.  Attempt to use Windows PowerShell to enumerate webs

1. In Sharepoint 2010 Management Shell, type the following command and then press ENTER:
$spsite - Get-SPSite "http://intranet.contoso.com"

2. To enumerate all of the webs in the site collection, type the following command and then press ENTER:

$spsite | Get-SPWeb

An error appears, indicating that login failed. The SP_Admin user account does not have the permissions required to access the information about the intranet site collection with Windows Powershell.

A. 2.2.  Configure least privilege rights to manage Sharepoint with Windows Powershell

1. Click Start, click All programs, click Microsoft Sharepoint 2010 Products hold down the SHIFT key and right-click Sharepoint 2010 Management Shell,  and then click Run as different user.

The Windows Security dialog box appears.

2. In the USer name box, type CONTOSO\Administrator
3. In the Password box, type [Sp_Adminpassword]
4. Click OK.
5. Type the following commands each followed by ENTER:

$spcdb - Get-SPContentDatabase WSS_Content-Intranet;
Add-SPShellAdmin -UserName CONTOSO\SP_Admin -Database $spcdb

6. Close the Administrator Sharepoint Management Shell

A. 3. Reporting Web and Site Collection Properties
A. 3. 1.  Use Windows Powershell to report Web properties
1. Switch to Sharepoint 2010 Management Shell
2. Type the followng command and then press ENTER:

$spsite = Get-SPSite "http://intranet.contoso.com"

3. To enumerate all of the webs in the site collection, type the following command and then press ENTER:

$spsite | get-SPWeb

4. To enumerate all of the webs in the site collection using the AllWebs collection, type the following command and then press ENTER:

$spsite.Allwebs

5. To list specific properties of the webs, type the following command and then press ENTER:

$spsite.Allwebs | Select LastItemModifiedDate, Url, Created | Sort Created


A. 3. 2.  Attempt to use the Grid-View to report site collection properties

1. To enumerate all of the site collection in the farm, except Central Administration, type the following command and then press ENTER:

Get-SPSite

2. Type the following command and then press ENTER:

Get-SPSite | Select URL, @{Name="Storage"; Expression={"{0:N2} MB" -f  ($_.Usage.Storage/1000000)}},  @{Name="Quota"; Expression={"{0:N2} MB"  -f($_.Quota.StorageMaximumlevel/1000000)} }

The listing displays various properties of each site collection.

3. Type the following command and then press ENTER:

Get-SPSite | Select URL, @{Name="Storage"; Expression={"{0:N2} MB" -f  ($_.Usage.Storage/1000000)}},  @{Name="Quota"; Expression={"{0:N2} MB"  -f($_.Quota.StorageMaximumlevel/1000000)} } | Out-GridView -Title "Sites with usage"

An aerror indicates that the Windows Powershell integrated Scripting Environment feature is not installed.

A. 3. 3.  Install the Windows Powershell Integrated Scripting Environment

1. Type the following two commands each followed by ENTER:

Import-Module ServerManager
Add-windowsFeature PowerShell-ISE

An error indicates that you must run the command with elevated rights.

2. Right-click the Windows PowerShell icon in the Windows taskbar, and then click Run as Administrator.
A User Account Control message appears.

3. Click Yes.
4. Type the following two commands each followed by ENTER:



Import-Module ServerManager
Add-windowsFeature PowerShell-ISE

5. Close Administrator Windows PowerShell.

A. 3. 4.  Use the Grid-View to report site collection properties

1. In Sharepoint 2010 Management Shell, press the UP ARROW several times until you see the command you typed in Task 2, and then press ENTER to run the command:


Get-SPSite | Select URL, @{Name="Storage"; Expression={"{0:N2} MB" -f  ($_.Usage.Storage/1000000)}},  @{Name="Quota"; Expression={"{0:N2} MB"  -f($_.Quota.StorageMaximumlevel/1000000)} } | Out-GridView -Title "Sites with usage"
An error indicates that the Windows Powershell integrated Scripting Environment feature is not installed. This occurs because you must close and reopen Sharepoint 2010 Management Shell to load the component.

2. Close Sharepoint 2010 Management Shell.
3. Open Sharepoint 2010 Management Shell.
4. Type the following command and then press ENTER, which is the same as the command you executed in step 1:

Get-SPSite | Select URL, @{Name="Storage"; Expression={"{0:N2} MB" -f  ($_.Usage.Storage/1000000)}},  @{Name="Quota"; Expression={"{0:N2} MB"  -f($_.Quota.StorageMaximumlevel/1000000)} } | Out-GridView -Title "Sites with usage"

A Grid-View window appears displaying the output of the command.
5. Close the Sites With Usage window.

A. 4. Creating Site Collections Using Windows PowerShell

A. 4. 1. Create a single site collection using Windows Powershell

1. In Sharepoint 2010 Management Shell, type the following commands:

New-SPContentDatabase -Name WSS_Content_Intranet_Sales -webApplication http://intranet.contoso.com
$spsite = New-SPSite -URl "http://intranet.contoso.com/sites/Sales" -ContentDatabase WSS-Content_Intranet_Sales -OwnerAlias CONTOSO\SP_Admin -Template "STS#0"

A site collection and top-level web for the Sale Department is created using the Team Site site definition.

2. Open Windows Internet Explorer
3. In the address bar, type http://intranet.contoso.com/sites/Sales, and then press ENTER.
The Sales site opens.
4. Minimize Internet Explorer.

A. 4. 2. Create multiple site collections using Windows Powershell

1. In Sharepoint 2010 Management Shell, type the following script. On the last line, press ENTER to create a blank line. This causes the script to execute.

$i = ("HR", "Marketing")
ForEach($url in $i)
{
New-SPContentDatabase -Name WSS_Content_Intranet_$url -webApplication http://intranet.contoso.com
New-SPSite _Url http://intranet.contoso.com/sites/$url -ContentDatabase WSS_Content-Intranet_$url -OwnerAlias CONTOSO\SP_Admin -Template "STS#0"
}

Two new content databases, site collections, and top-level webs are created.

2. To enumerate all of the site collections in the farm, except Central Administration, type the following command and then press ENTER:

Get-SPSite

The output lists the new site collections.

A. 5. Creating and Updating Items

A. 5. 1. Modify a list item using Windows Powershell

1. Switch to Internet Explorer
2. In the Sales site Quick Launch, click All Site Content, and then click Announcements.
3. Observer the title of the only item in the list.
4. Switch to Sharepoint 2010 Management Shell, and then type the following commands:

$gc = Start-SPAssignment
$spsite = $gc | Get-SPSite "http://intranet.contoso.com/sites/Sales"
$splist = $spsite.rootweb.lists["Announcements']
$splistitem =  $splist.items[0]
$splistitem["Title"] = "Our Sharepoint 2010 Sales site is now live!"
$splistitem.update()

$gc | Stop-SPAssignment

The list item will be updated. Notice that you did not use a cmdlet to update a list item. There are things that will require direct access to the object model and, as such, you need to be careful to dispose of objects you create.

5. Switch to Internet Explorer, and then press F5 to refresh the view of the Announcements list.
6. Observe the updated title of the announcements.
7. Close all Internet Explorer and Windows PowerShell windows.

B. Administering Sharepoint with STsadm
B. 1. Executing Stsadm Commands
B. 1.  1. Display Stsadm help documentation
1. Click Start, click All Programs, click Microsoft Sharepoint 2010 Products, right-click Sharepoint 2010 management shell, and then click Run as administrator.
The User Account Control dialog box appears.
2. Click Yes.
3. Type the following command and then press ENTER;

stsadm

Examine the output of he command, which includes a list of the numerous operations supported by Stsadm. Also notice the examples displayed at the end of the Help documentation.

4. To display Help documentation for the enumsites operation, type the following command and then press ENTER:

stsadm -help enumsites



B. 1. 2.  Enumerate site collections in a Web Application using Stsadm

1. Type the following command, and observer the amount of time it takes for the command to execute.

stsadm -o enumsites -url "http://intranet.contoso.com"

Review the Extensible Markup Language (XML) response that you get from the command, and note that this can be used in a Windows Powershell script to iterate through all your site collections.

2. Type the following command, and observer the amount of time it takes for the command to execute.

Get-SPSite "http://intranet.contoso.com" | Get-SPWeb

3. Repeat steps 1 & 2, and observer the amount of time it takes for each command to execute.


B. 1. 3.  Create an Operations site collection using Stsadm

1. Type the following command:

stsadm -o createsite -url "http://intranet.contoso.com/sites/Operations" -ownerlogin CONTOSO\SP_admin -owneremail sharepoint@contoso.com

A new Operations site collection is created.
2. Open Internet Explorer.
3. In the address bar, type http://intranet.contoso.com/sites/Operations, and then press ENTER.
The Template Selection page appears.
4. Select the Team Site template.
5. Click OK.
The site is created using the Team Site site definition.
6. On the Set Up Groups for this Site page, click OK.

B. 1. 4.  Create an Operations Management Site using Stsadm

1. Switch to Sharepoint Management Shell, and then type the following command:

stsadm -o createweb -url "http://intranet.contoso.com/sites/Operations/Maintenance"

A new web called Maintenance is created in the Operations site collection.
2. Switch to Internet Explorer.
3. In the address bar, type http://intranet.contoso.com/sites/Operations/Maintenance, and the press ENTER.
The Template Selection page appears.
4. Select the Team Site template, and then click OK.

B. 1. 5.  Configure the site collection administrator using Stsadm


1. Click the Sharepoint Administrator menu in the upper-right corner of the page, and then click Sign in as Different User.
The Windows Security dialog box appears.

2. In the User name  box, type CONTOSO\Administrator
3. In the Password box, type [SP_Adminpassword], and then click OK.
An error: Access Denied page appears.
4. Switch to Sharepoint2010 Management Shell, and then type the following command:

stsadm -o siteowner -url "http://intranet.contoso.com/sites/Operations" -ownerlogin CONTOSO\Administrator


5. Switch to Internet Explorer, type http://intranet.contoso.com/sites/Operations/Maintenance in the address bar, and then press ENTER.
You can now access that site collection as CONTOSO\Administrator