Thursday, June 9, 2011

Working with PowerShell Scripts


Working with PowerShell Scripts

In this exercise you will use the PowerShell console to gain experience using PowerShell commands. You will then use the PowerShell add-in for SharePoint 2010 to provision a SharePoint site.

Task 1 – Using the PowerShell Console

In this task you will use the PowerShell console to review the format of a PowerShell script. You will then verify the execute permissions that are configured on the Virtual Machine.

1.    Launch the PowerShell console from Windows Start menu. You’ll find the PowerShell shortcut in the Start menu called Windows PowerShell V2 (CPT3). Select the Windows PowerShell V2 (CPT3) shortcut, not the “ISE” shortcut. Please note that in this step you should be launching the PowerShell console from standard PowerShell menu item and not SharePoint-specific menu item under Administrative Tools.
2.    At the PowerShell command prompt, type the following text:
Set-Location c:\Student\Labs\01_Roadmap\Powershell\
3.    Press [Enter].
The current folder of the PowerShell console reflects the new path. This location has several PowerShell Scripts (*.ps1 files) that will be used in this exercise.
4.    At the PowerShell command prompt, type the following text:
Get-ChildItem
5.    Press [Enter].
PowerShell lists the scripts contained in the current location.

6.    At the PowerShell command prompt, type the following text:
Notepad Hello.ps1
7.    Press [Enter].
Review the contents of the hello.ps1 PowerShell file.
8.    Close Notepad.
9.    At the PowerShell command prompt, type the following text:
.\Hello.ps1
 10.  Press [Enter].
The script runs and a simple message is output to the PowerShell console.
PowerShell Execution Policy
If the PowerShell scripting support on the machine has the execution policy of restricted, the script will not run. If the execution policy has been changed to unrestricted, the console will prompt you whether to run the script or not.
When writing and testing PowerShell scripts, it is easiest to change the execution policy to Bypass so that scripts can freely run without any user prompts. If your machine will not execute the .\Hello.ps1 script use the following instruction at the PowerShell command prompt:
Set-ExecutionPolicyByPass
11.  Leave the PowerShell console open; you will use it in a later task.




Task 2 – Using the PowerShell Integrated Scripting Environment (ISE)

In this task you will use the Integrated Scripting Environment to edit and execute PowerShell scripts.

1.    Click Start | All Programs | Windows PowerShell V2 (CTP3)| Windows PowerShell ISE
The PowerShell Integrated Scripting Environment starts.

Figure 1 - Starting Windows PowerShell ISE
2.    Click the File menu, then click Open.
3.    Browse to the file C:\Student\Labs\01_Roadmap\Powershell\Hello.ps1 and click Open.
The Hello.ps1 file appears at the top of the screen.

Figure 2 - The PowerShell ISE
4.    Click the Debug menu option and review the options, then click Run/Continue.
5.    In the Script pane at the top of the Integrated Scripting Environment edit the script so that it looks as follows:
$HelloMessage = "Sample Message"
Write-Host "-----------------------------------"
Write-Host "Hello World of Powershell Scripting"
Write-Host "Host name: "$(Get-Item env:\computerName).value
Write-Host $HelloMessage
Write-Host "-----------------------------------"



Task 3 – Debugging a PowerShell script in the Integrated Scripting Environment

In this task you will single step through a PowerShell script.

1.    In the Script pane, click in the first line of the Hello.ps1 script.
2.    Click the Debug menu, and click Toggle Breakpoint.
3.    Click the Debug menu, and click Run/Continue.
4.    Click OK if asked to save the script first.
The code execution pauses at the point the breakpoint was set.

Figure 3 - PowerShell Breakpoint
5.    Click the Debug menu, and clickStep Into to step through each of the remaining lines of code. Note that you can also use F11.

Task 4 – Using the PowerShell Consoleto load the SharePoint Snap-in

In this task you will prepare the PowerShell environment to run SharePoint scripts.

1.    In the PowerShell command prompt that you left open in Task 1, type the following:
Notepad LoadSharePointSnapin.ps1
2.    Press [Enter].
Review the code and note the call to theAdd-PSSnapincmdlet. This loads the snap-in for SharePoint 2010 named Microsoft.SharePoint.PowerShell.
3.    Close Notepad.
4.    At the PowerShell command prompt, type the following text:
.\LoadSharePointSnapin.ps1
5.    Press [Enter].
At this point the PowerShell commagnds for SharePoint have been enabled.
6.    At the PowerShell command prompt, type the following text:
Get-Command -PSSnapin Microsoft.SharePoint.PowerShell
7.    Press [Enter].
A list is returned of all the commands supported by SharePoint.
8.    At the PowerShell command prompt, type the following text:
Get-Command -PSSnapin Microsoft.SharePoint.PowerShell -Verb Get
9.    Press [Enter].
A list is returned of all of the Get commands supgeported by SharePoint.
10.  At the PowerShell command prompt, type the following text:
Get-Command -PSSnapin Microsoft.SharePoint.PowerShell >SP2010Cmdlets.txt
11.  Press [Enter].
This time the full list of commands are piped to a new text file.
12.  At the PowerShell command prompt, type the following text:
Notepad SP2010Cmdlets.txt
13.  Press [Enter].
The commands are opened in notepad.
14.  Close Notepad.




Task 5 – Using the PowerShell Console to create a SharePoint site

In this task you will use the PowerShell console to create a SharePoint site.

1.    At the PowerShell command prompt, type the following text:
Get-Help New-SPSite
2.    Press [Enter].
The details for the New-SPSite command are shown. You will now create a SharePoint site using the New-SPSite object.
3.    At the PowerShell command prompt, type the following text:
new-spsite -URL http://intranet.contoso.com/sites/Lab01C -OwnerAlias Contoso\Administrator -Template STS#1 -Name Lab01C
4.    Press [Enter].
When the PowerShell script completes, open Internet Explorer and review the newly created site at http://intranet.contoso.com/sites/Lab01c.

Figure 4 - Creating a SharePoint Site Collection from PowerShell

Task 6 – Using a PowerShell Script to create a SharePoint site

In this task you will use and existing PowerShell script to create a SharePoint site.

1.    At the PowerShell command prompt, type the following text:
Notepad CreateContosoSite.ps1
2.    Press [Enter].
Review the details in the script file.
3.    Close Notepad.
4.    At the PowerShell command prompt, type the following text:
.\CreateContosoSite.ps1 Lab01D
5.    Press [Enter].
When the PowerShell script completes a success message is returned.
6.    Type Exit and press [Enter] to leave PowerShell.
7.    Type Exit and press [Enter] to close the command Prompt.

8.    Use Internet Explorer to open the site http://intranet.contoso.com/sites/Lab01d
Verify the newly created site.

No comments:

Post a Comment