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
- Be sure to
login to svr-sp2 as trainingadministrator
- Open a
command prompt, start powershell:
- Type the
following:
[AppDomain]::CurrentDomain.GetAssemblies() | foreach-object {
split-path $_.Location -leaf } | sort
|
- Note how
SharePoint is not in the list, we need to load the dlls! Type the following command:
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
|
- You should
get an output displaying the GAC, Version and Location information for the
assembly
- 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
- Open the C:WINDOWSsystem32windowspowershellv1.0
directory
- Create a
new file called Profile.ps1
- Open the
new file and type the following into it:
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
|
- Close any
open powershell prompts
- Open a new
powershell prompt
- 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
- Run the
following command (you can also review the about_signing_help.txt file in
the powershell install directory):
- Run the
following command to get the execution policy:
- Set the execution policy to allow scripts to
run:
set-executionpolicy unrestricted
|
- Close and
reopen PowerShell, you should not get an error this time
- Run the
following command:
[AppDomain]::CurrentDomain.GetAssemblies() | foreach-object {
split-path $_.Location -leaf } | sort
|
- 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
- Open
powershell, run the following:
$spsite = new-object Microsoft.SharePoint.SPSite("http://servername:100")
|
- NOTE: Be sure to replace servername
with the name of your server
- 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
- Run the
following:
- You will
get a listing of all the properties that are available from the SPSite
object!
- One of the
properties is a collection of all the webs in the site collection, run the
following command:
- You will
get an output all of the properties of the SPWeb object, too much
information!
- Run the
following command to select and sort specific properties:
$spsite.allwebs | select LastitemModifiedDate, URL, Created |
sort Created
|
- 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
- Open
powershell, run the following:
$spsite.allwebs.Add("sales", "Sales Site",
"Marketing And Sales", 1033, "STS#0", $true, $false)
|
- In a
browser, open http://servername:100/sales
- 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
- Open
powershell, run the following commands:
$splist = $spsite.rootweb.lists["announcements"]
$splistitem = $splist.items[0]
$splistitem["Title"] = "a new title"
$splistitem.update()
|
- 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
- Create a
new directory called c:SharePointBackup
- Create a
new script called BackUpSharePoint.ps1 in the c:scripts directory (create
it if it doesn’t exist)
- 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:
- Click
Ok
- Click
Ok
- Restart
your PowerShell prompt
- 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)
|
- 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
- Type
backupsharepoint.ps1, type ENTER
- Open the
c:sharepointbackups folder, notice you have some backups!