SharePoint 2007 and PowerShell

Module #06: PowerShell With SharePoint Lab #02

 

Course:                SharePoint
2007 Operations

Estimated Time to Complete:  60 minutes

Objectives:

·        
Load SharePoint Dll
into PowerShell

·        
Set PowerShell
Execution Policy

·        
Enumerate Webs

·        
Create a site with
PowerShell

·        
Create/Update an
item with PowerShell

·        
Backup SharePoint
with PowerShell

Operating
Notes:
 

·        
Run
this lab on the svr-sp2 image

·        
You
should be logged in as trainingadministrator


Deliverables:

·        
None

 

Overview:         Learn
to write PowerShell scripts for common SharePoint Tasks!

Exercise 1 – Write a PowerShell Script (Load SharePoint Dlls)

Purpose:
        Learn
to load the SharePoint dlls

Result:           
A PowerShell environment with SharePoint .NET
dlls loaded

Task 1 – Load the
SharePoint dlls

  1. Be sure to
    login to svr-sp2 as trainingadministrator
  2. Open a
    command prompt, start powershell:


Powershell

  1. Type the
    following:


[AppDomain]::CurrentDomain.GetAssemblies() | foreach-object {
split-path $_.Location -leaf } | sort

  1. Note how
    SharePoint is not in the list, we need to load the dlls!  Type the following command:


[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

  1. You should
    get an output displaying the GAC, Version and Location information for the
    assembly
  2. Rerun the
    command to display the loaded assemblies, you should now see
    Microsoft.SharePoint loaded!

Exercise 2 – Create a PowerShell Profile

Purpose:
        Create
a PowerShell startup scripts

Result:           
A PowerShell script

Task 1 – Create the
script

  1. Open the C:WINDOWSsystem32windowspowershellv1.0
    directory
  2. Create a
    new file called Profile.ps1
  3. Open the
    new file and type the following into it:


[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

  1. Close any
    open powershell prompts
  2. Open a new
    powershell prompt
  3. At startup
    you will get an error “…scripts is disabled on this systems”, we have to
    enable scripts to be run on the server!

Task 2 – Enable
Scripts

  1. Run the
    following command (you can also review the about_signing_help.txt file in
    the powershell install directory):


get-help about_signing

  1. Run the
    following command to get the execution policy:


get-executionpolicy

  1. Set  the execution policy to allow scripts to
    run:


set-executionpolicy unrestricted

  1. Close and
    reopen PowerShell, you should not get an error this time
  2. Run the
    following command:


[AppDomain]::CurrentDomain.GetAssemblies() | foreach-object {
split-path $_.Location -leaf } | sort

  1. You should
    see the SharePoint dll has been loaded!

Exercise 3 – Write a PowerShell Script (Enumerate Webs)

Purpose:
        Find
all the webs for a site collection

Result:           
A PowerShell script

Task 1 – Create the
script

  1. Open
    powershell, run the following:


$spsite = new-object Microsoft.SharePoint.SPSite("http://servername:100")

  1. NOTE: Be sure to replace servername
    with the name of your server
  2. NOTE: If you get an access denied
    error it likely means that you are logged in as spadmin, you can do two
    things:
    • Login as administrator on the server
      you are running powershell
    • Add spadmin as a site owner to the
      team site on port 100
  3. Run the
    following:


$spsite

  1. You will
    get a listing of all the properties that are available from the SPSite
    object! 
  2. One of the
    properties is a collection of all the webs in the site collection, run the
    following command:


$spsite.allwebs

  1. You will
    get an output all of the properties of the SPWeb object, too much
    information!
  2. Run the
    following command to select and sort specific properties:


$spsite.allwebs | select LastitemModifiedDate, URL, Created |
sort Created

  1. You can
    pipe the properties into other commands, so if you wanted to backup
    individual sites, you could do that!

Exercise 4 – Write a PowerShell Script (Create a Web)

Purpose:
        Create
A Web

Result:           
A PowerShell script

Task 1 – Create the
script

  1. Open
    powershell, run the following:


$spsite.allwebs.Add("sales", "Sales Site",
"Marketing And Sales", 1033, "STS#0", $true, $false)

  1. In a
    browser, open http://servername:100/sales
  2. You will
    see your new site!

Exercise 5 – Write a PowerShell Script (Create/Update an Item)

Purpose:
        Create/Update
an item

Result:           
A PowerShell script

Task 1 – Create the
script

  1. Open
    powershell, run the following commands:


$splist = $spsite.rootweb.lists["announcements"]
$splistitem = $splist.items[0]
$splistitem["Title"] = "a new title"
$splistitem.update()

  1. Open the http://servername:100 site, notice how
    the announcements list item has been updated!

Exercise 6 – Write a PowerShell Script (BackUp SharePoint)

Purpose:
        Learn
to use the SharePoint object model from PowerShell to backup SharePoint

Result:           
A PowerShell script to backup SharePoint

Task 1 – Create the
script

  1. Create a
    new directory called c:SharePointBackup
  2. Create a
    new script called BackUpSharePoint.ps1 in the c:scripts directory (create
    it if it doesn’t exist)
  3. Add C:scripts
    to your path
    • Click
      Start
    • Right
      click “My Computer”
    • Click
      the “Advanced” tab
    • Click
      the “Environment Variables” button
    • Click
      “Edit”
    • Add
      the following:


;c:scripts

    • Click
      Ok
    • Click
      Ok
    • Restart
      your PowerShell prompt
  1. Type the
    following into it:


$settings =
[Microsoft.SharePoint.Administration.Backup.SPBackupRestoreSettings]::GetBackupSettings("c:SharePointBackup",
"Full")

$backupId =
[Microsoft.SharePoint.Administration.Backup.SPBackupRestoreConsole]::CreateBackupRestore($settings)

$obj = 
[Microsoft.SharePoint.Administration.Backup.SPBackupRestoreConsole]::FindItems($backupId,
$settings.IndividualItem)[0]

[Microsoft.SharePoint.Administration.Backup.SPBackupRestoreConsole]::SetActive($backupId)

[Microsoft.SharePoint.Administration.Backup.SPBackupRestoreConsole]::Run($backupId,$obj)

 

  1. Open a PowerShell
    command prompt to c:scripts
    • If
      the script isn’t recognized, then you have to add c:scripts to your path
      environment variable
  2. Type
    backupsharepoint.ps1, type ENTER
  3. Open the
    c:sharepointbackups folder, notice you have some backups!