My SharePoint 2010 Powershell tutorial for SharePoint 200 developers

Here is an extract of the Powershell module of my  SharePoint 2010 for developers course.

 

1.1 lab.introduction to Powershell

In this lab, you will work out a number of exercises that have to do with Powershell.

In Lab1 you will discover the main Powershell concepts; in Lab 2, you will apply these concepts.

1.1.1 Lab 1.discovering Powershell

1. Start the Sharepoint 2010 Management Shell.

clip_image002

2. To get help, type:

Get-help

3. To see all the commands available, type :

Get-Command

4. To see how to use Get-Command, type get-help get-Command; you will notice the Noun option:

clip_image003

5. To get the list of all Sharepoint commands, type:

Get-Command –Noun SP*

You will notice familiar cmdlets like Get-SPUser, Get- SPWeb, Get- SPSite,…

Sharepoint commands are organized as verb-noun like Enable-SPFeature.

You can type Get-Command –Noun SPFeature* or Get-Command –verb Enable.

6. In Powershell, there is a cmdlet that give you the object size :Measure-Object; to get the number of Sharepoint commands, just type :

Get-Command –Noun SP* |Measure-Object –line

There are 560 Sharepoint Cmdlets.

7. If you type:

Get-Command –noun SPSite

clip_image004

8. Instead of using the Sharepoint Powershell, start the default Powershell console (In Accessories) :

clip_image006

9. At the console, type :

Get-Command –noun SP*

clip_image007

You get nothing because the SharePoint module is not loaded.

10. To get the list of Powershell Snapins available (not loaded), type:

Get-PSSNapin –registered

clip_image009

11. To Load the SharePoint Snapin , type:

Add-PSSnapin Microsoft.Sharepoint.Powershell

12. To make sure the Sharepoint Cmdlets are now available, type:

Get-Command –Noun SP*

13. To illustrate auto completion, type:

Get-SP (followed by Tab)

Get-SPW (followed by Tab)

14. To get all site collections in the farm, type:

Get-SPSite

clip_image010

15. To find a way to manipulate or to get more information about objects, type:

Get-Help

16. Notice in the Remarks, the Get-member cmd :

clip_image011

17. Type:

Get-SPSite | get-member

clip_image013

You can notice for instance the Url and the Owner.

18. To display the site collection Url and owner , type :

Get-SPSite | select url,owner

clip_image014

Get-SPSite | select *

19. To display service applications, their id and name, type:

Get-SPServiceApplication |select id, name

clip_image015

20. Parameter auto completion

Type:

Get-SPServiceApplication –id (followed by Tab)

You will get:

clip_image016

21. Now, by copying & pasting an Id from the previous command (or by using an Id from a log file), you can provide the identity:

clip_image017

22. To get a list of all features in the farm, type:

Get-SPFeature

clip_image018

23. To get a list of all features in a site collection, type:

Get-SPfeature –site http://litware

clip_image019

24. Modification of an object’s property

For most Get-SP* cmdlet you have a Set-SP* equivalent;

For instance, if we want to modify a web site description, we can type:

Set-SPWeb –identity http://litware –description blablabla

clip_image021

25. If you type:

Get-SPWeb -identity http://litware | select description

you will get:

clip_image022

26. Create a new Site collection with new-SPSite

Type:

get-help new-SPSite

Type:

New-SpSite –url http://litware/sites/demo1

clip_image023

Powershell will ask you to provide other parameters

For ownerAlias, type:

contoso\Administrator

clip_image024

27. To make sure the site collection has been created, type:

Get-SPSite | select url, owner

clip_image025

28. Create 2 other site collections (and the template used for the root sites will be a “Team site” defined in Caml as STS#0)

New-SPSite –url http://litware/sites/demo2 -owneralias contoso\administrator-name “powershell demo2” –template STS#0

New-SPSite –url http://litware/sites/demo3 -owneralias contoso\administrator -name “powershell demo3” –template STS#0

29. Alias

Some commands have aliases (shortcuts); to get a list of aliases, just type:

alias

clip_image026

30. The | (“pipe”) character can be used to pass the output from one cmdlet to another.

Scripts can be created by using variables and piping output to other cmdlets.

Using the ForEach attribute can quickly get all values or set all properties that match condition set.

It is also possible to filter and to use wildcards *.To illustrate this, we are going to create a blog site in every demo site collection we previously did;

To get the list of site collection, type:

Get-SPSite http://litware/sites/demo*

clip_image027

Now we will try to create 1 blog site (template is BLOG#0) in each demo site collection; type:

Get-SPSite http://litware/sites/demo* -limit all | foreach { new-SPWeb –url ($_.url+”/blog”) –template BLOG#0 }

clip_image028

31. To add a secondary owner to all demo sites, type:

Get-SPSite http://litware/sites/demo* -limit all | set-spsite –secondaryowneralias contoso\brianc

To check this out, type:

Get-SPSite http://litware/sites/demo1 | select url, owner, secondarycontact

32. To discover action related to features like activating, installing, deactivating,…, type

Get-Command -noun SPfeature*

clip_image029

To illustrate Enable-SPFeature, type :

Get-help Enable-SPfeature -examples

clip_image030

1.1.2 Lab 2.Creating a small Powershell application

1. Write a batch file that will create a new site collection and that will provision the root site with the template “team site” (STS#0); the web site title must be “Web Site Powershell”.

2. If the site collection already exists, delete it. Don’t forget to load the Sharepoint snapin if it is not already loaded…

3. When the site is successfully created, display:

clip_image031

Make sure the console stays open.

33. To make sure the console stays open, create a batch file and use the –noexit option

Create an empty file named SetupLab.bat.

34. In this batch file, type :

powershell -Command "& {.\SetupLab.ps1}" -NoExit

pause

35. Create a file named SetupLab.ps1.

36. Define 3 variables:

# define variables for script

$SiteTitle = "Web Site-Powershell"

$SiteUrl = "http://litware/sites/powershell"

$SiteTemplate = "STS#0"

37. Check to make sure the Sharepoint snapin is not loaded:

# check to ensure Microsoft.SharePoint.PowerShell is loaded

$snapin = Get-PSSnapin | Where-Object {$_.Name -eq ‘Microsoft.SharePoint.Powershell’}

if ($snapin -eq $null) {

Write-Host "Loading SharePoint Powershell Snapin"

Add-PSSnapin "Microsoft.SharePoint.Powershell"

}

It is good to know that if the snapin is already loaded and if you try to load it again, you will get errors.

38. Delete the existing site collection if any:

# delete any existing site found at target URL

$targetUrl = Get-SPSite | Where-Object {$_.Url -eq $SiteUrl}

if ($targetUrl -ne $null) {

Write-Host "Deleting existing site at" $SiteUrl

Remove-SPSite -Identity $SiteUrl -Confirm:$false

}

39. Create a Site collection:

# create new site at target URL

Write-Host "Creating new site at" $SiteUrl

$NewSite = New-SPSite -URL $SiteUrl -OwnerAlias Administrator –Template $SiteTemplate -Name $SiteTitle

$RootWeb = $NewSite.RootWeb

40. Display Site info:

# display site info

Write-Host

Write-Host "Site created successfully" -foregroundcolor Green

Write-Host "————————————-" -foregroundcolor Green

Write-Host "URL:" $RootWeb.Url -foregroundcolor Yellow

Write-Host "ID:" $RootWeb.Id.ToString() -foregroundcolor Yellow

Write-Host "Title:" $RootWeb.Title -foregroundcolor Yellow

Write-Host "——-

For your info, it is also possible to create functions with parameters in Powershell and to invoke them; a nice example in Sharepoint is illustrated in the following post : http://stefvanhooijdonk.com/2010/03/02/power-of-powershell-and-the-sharepoint-snap-in

1.1.3 Lab 3.Create a Custom Powershell Cmdlet.

In this lab, you will create a Powershell Cmdlet that will download a Sharepoint Solution package from the Sharepoint Solution store.

1. Start Visual Studio 2010 and create a new Class Library; delete the Class1.cs; name the project DownloadWsp.

2. Make sure the .Net framework 3.5 is selected and that the platform target in the project properties is Any CPU.

3. Add a reference to the Microsoft.Sharepoint assembly.

4. We need to add a reference to the System.Management.Automation assembly which is installed in the Gac, but not available in Visual Studio by default. Add a reference to System.Management (which is showing in the reference list).Save the project. Edit the project file with notepad and change "<Reference Include="System.Management" />" to "<Reference Include="System.Management.Automation" />.

5. Now we will create our custom Cmdlet : add a new public class (it is not public by default) , name it DownloadWspCmdlet and derived it from PSCmdlet; resolve the namespace:

clip_image033

4. Define 2 public properties WspFileName and StoreTo that will become the Cmdlet parameters by decorating them with the Parameter attribute:

clip_image035

5. Override the function ProcessRecord is the code that will run when the Cmdlet will be invoked:

clip_image037

6. Resolve the namespace Microsoft.Sharepoint.Administration.

7. We can also define the verb and the noun to use in order to invoke our Cmdlet :decorate the class with a Cmdlet attribute :

clip_image039

8. Now we need to create a SnapIn which is a class derived from PSSnapin :create a public class named DownloadWspSnapin, derive it from PSSnapIn and decorate it with the attribute RunInstaller:

clip_image041

9. RunInstaller is defined in the assembly System.Configuration.Install, ad a reference to this assembly.

10. PSSnapIn is an abstract class, implement its members:

clip_image043

clip_image045

11. Rebuild the project, add the generated dll into the Gac.

12. Run InstallUtil.exe against your dll; you will find InstallUtil.exe in the following folder :

clip_image047

clip_image049

13. Start a Powershell console and add the new snapin:

clip_image050

14. Test your Cmdlet by invoking Download-SPSolution in the Powershell console:

clip_image052

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s