introduction to windows power shell in sharepoint 2010

Post on 21-Oct-2014

2.858 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

2nd SPS Vietnam from Gia Mai

TRANSCRIPT

Introduction to Windows PowerShell in SharePoint 2010

Gia Duy MaiSolutions Assistant LeadBamboo Solutions

Agenda

• Introduction to PowerShell• Why PowerShell for SharePoint?• When use PowerShell?• PowerShell vs STSADM• Getting started with PowerShell• Controlling PowerShell with variables, loops,

logic, and functions• Creating your own PowerShell scripts • Scheduling your PowerShell scripts• Demo• Q&A

Introduction to PowerShell

• What is Shell?Working with a command-line interface, often called a shell

• What is PowerShell? A modern replacement for the CMD (command)

shell PowerShell works with objects PowerShell integrates with the .Net Framework PowerShell is based on a provider-based model

A powerful scripting environment for administration

PowerShell Fundamentals

• Scripts– PowerShell code in a PS1 (similar to .bat files)

• CmdLets– A PowerShell command is called CmdLets– Like Features in SharePoint, performs a task– Can be compiled or in script

Ex: Get-Process and Stop-Process

• Snapins– Like Solutions in SharePoint, they hold a lot CmdLet– Compiled into dlls

Ex: Microsoft.SharePoint.Powershell

• Pipelining • Formatting features• Provider-based model for accessing resources

The Pipeline

• What is Pipeline?The most unique feature of PowerShellPipelining allow one Cmdlet to return an object as

input to another

• C#: – X = Class.Method();– Y = OtherClass.Method(X);– Z = OtherOtherClass.Method(Y)

• PowerShell:– Class.Method() | OtherClass.Method() |

OtherOtherClass.Method()

PowerShell vs STSADM

STSADM is SharePoint-specific command interface

PowerShell is a generic scripting interface

Extensible with compiled code only Extensible via script interface into .Net objects

Limited scripting using VBScript/JavaScript Cmd

Is an extensible scripting environment

Output is basic text or Xml Output is rich objects to a pipeline

STSADM does not allow you to access file system, registry and so on

PowerShell gives you access to the file system, registry, data source…

STSADM in MOSS 2007 support 182 Commands

PowerShell in SharePoint 2010 support 652 cmdlets +

PowerShell vs STSADM - Performance

• Performance benefits for batch operations

Start Thread

New STSADM Execution

Start Logging

Execute Command

Load DLLs

Stop Logging

Close STSADM & Thread

Repeat

STSADM Batch Operation

Start Thread

Stop Thread

PowerShell Cmdlets in Batch

Start logging

Execute Command

Stop logging

Repeat

PowerShell vs STSADM - Performance

• PowerShell “Wrapped” STSADM to enable a feature on every SPSite

• Same command in SharePoint 2010 PowerShell:

$rawdata = stsadm.exe -o enumsites -url $WebAppURL $sitexml = [XML]$rawdata$sitesxml.Sites | foreach-object{ $_.Site } | foreach-object{  stsadm -o activatefeature -url $_.url -filename $featureFileName         if( $lastexitcode -ne 0 ){             Write-Host “Failure:$lastexitcode \n" -Fore Red;            $failure = $true;          }}

Get-SPSite –Limit ALL –WebApplication $WebAppNameorUrl |%{ Enable-SPFeature $FeatureIdOrName –url $_.Url }

12+ Hours(5300 SPSites)

30 Minutes(5300 SPSites)

When use PowerShell?

• When you want to make your team more agile- Automation, automation, automation

• Development phase - Using PowerShell to save time for Build/Deploy/Test Cycle

• Testing phase– Using the PowerShell scripts to stand up an environment

for running tests

Code Compile Deploy Test

Code

Compile

Deploy

Test

CodeComp

ileDeplo

y

Traditional SharePoint Development

When you use PowerShellWasted time

for testing

Who should learn PowerShell?

• SharePoint Admins/Devs should learn PowerShell basics

• Why you should learn PowerShell?

1. Microsoft says it’s important2. You can’t do everything from the GUI any more3. It can make your life easier4. Most Microsoft products will eventually use it5. Microsoft certification exams contain PowerShell

questions6. Improve your job7. It’s not going away anytime soon

Getting Started with PowerShell

(4) Redirect output to new text file

(2) Filter results using where clause

(1) Execute a Cmdlet

(3) Add formatting instructions

Getting Help in PowerShell

Controlling PowerShell with variables, loops, logic, and functions

Variables in PowerShell

• A variable is a place for you to store some value so you can revisit it later.

• All variables that you create begin with $Ex: $my_first_variable = “Hello World”

• Variables TypesLoosely typed variables– $f = “bar” #implicitly typed as string– $ary = 4,2,5,2 #typed as object[]

Strongly typed variables– [string]$f = “bar”

Enhanced Types variables– [xml]$d = “<a><b><c>c stuff 1</c><c>c stuff

2</c></b></a>”

Looping in PowerShell

• Classic PowerShell Loop- For Loop

$i = 10 For($j = 1; $j -lt $i; $j++){ Write-Host ”$j”; }

- Foreach Loop$NumArray = (1,2,3,4,5,6,7,8,9,10)Foreach ($Item in $NumArray) {$Item * 3}

- While Loop$i =8 While ($i -le 96) { $i +=8; Write-Host $i; }- Do Until Loop$i = 7; do { $i +=7; Write-Host $i; } until ($i -gt 85)

• Foreach Loop with PipelineGet-SPSite -Limit All | foreach { Remove-SPSite -identity $_.Id }$_ and is a variable that represents the current object

Adding logic in PowerShell

• Logic is what enables you to do something at a specific point in the program.

• Statements– If/ ElseIf/ Else– Switch– While

• Comparison Operators– -eq/-ne– -lt / -gt– -le / -ge– -like / -notlike– -contains/-notcontains– -replace– …

Working with PowerShell functions

• Function ListRootDrive($DriveLetter){ Get-ChildItem –Path $DriveLetter }

• Function ListRootDrive{ Get-ChildItem –Path $Args }

• Function ListRootDrive{ Get-ChildItem –Path $Args[0] }Function BlogUsers($SiteCollection) {Get-SPWeb -Site $SiteCollection | ForEach-

Object { Write-Host “Site Title: “ $_.Titleif ( $_.Title -eq “Blog” ) { Write-Host “Blog

Users:” $_.Users}}}

PowerShell Aliases

PowerShell commands are usually long. However, PowerShell has aliases. You can use aliases to save yourself the time and effort of typing cmdlets

Get-ChildItem ~ dir Foreach-Object ~ %

Get-ChildItem | foreach-object { $_.Name } Alias: dir | % { $_.Name }

Where-Object ~ ? Get-ChildItem | where-object {$_.Length –gt 10} Alias: dir | ? {$_.Length –gt 10}

You can find these aliases with the Get-Alias cmdlet

Creating your own PowerShell scripts

Tools

1. Notepad2. PowerShell Command3. Windows PowerShell Integrated

Scripting Environment (ISE)4. PowerGUI

- Download from powergui.org

How to Create and Run a PowerShell Script

The steps to create a script follow:1. Create the script in Notepad and save

with a .PS1 file extension (hello.ps1).2. Run the script by entering the full path to

the script (c:\scripts\hello.ps1), or if it’s in the current directory (.\hello.ps1).

3. If the path to the script contains a space, enclose the full path in quotation marks and prefix the entire thing with an ampersand ("C:\my scripts\hello.ps1").

Scripts and Execution Policy

• Local execution policy must be configured

Scripts will not execute under default execution policy of restricted

Scripts can execute under execution policy of unrestricted. Scripts that are not signed result in prompting user for permission to execute.

Scripts can execute under execution policy of bypass. This mode suppresses prompting user for permission to execute.

Editing Scripts using PowerShell ISE

• Supports Color-coding, IntelliSense and debugging

Writing scripts here

Run cmdlets or functions here

Results

Examples of SharePoint Scripting

The SharePoint PowerShell Snapin

• Explicitly load SharePoint PowerShell snap-in from console or script

• Implicitly load SharePoint PowerShell snap-in by using SharePoint 2010 Management Shell

Begin using SharePoint Cmdlets

Scheduling Your Scripts

• Sometimes commands need to be scheduled to run automatically

• PowerShell Scripts (.ps1) can be scheduled using Task Scheduler

• Just put the above command into a .bat or .cmd file and schedule it like you would normally schedule a script to be run with Task Scheduler.

PowerShell -command “c:\PowerShellScripts\BackupSPSite.ps1”

Demo – Series Of Scripts

1. Run hello.ps1 script

2. How to use PowerShell to create sites.

3. How to use PowerShell to populate items in a List for testing, adding attachments

4. How to use PowerShell to copy items from one list to another.

5. How to use PowerShell to upload documents to a document libraries.

6. How to use PowerShell to backup sites.

Q&A

THANK YOU!

top related