Module #06: PowerShell With SharePoint Lab #02
Course: SharePoint Estimated Time to Complete: 60 minutes Objectives: · · · · · · Operating · ·
·
|
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:
|
- Note how
SharePoint is not in the list, we need to load the dlls! Type the following command:
|
- 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:
|
- 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:
|
- Close and
reopen PowerShell, you should not get an error this time - Run the
following command:
|
- 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:
|
- 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:
|
- 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:
|
- 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:
|
- 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:
|
- 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!