• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Introduction to windows power shell in sharepoint 2010
 

Introduction to windows power shell in sharepoint 2010

on

  • 2,573 views

2nd SPS Vietnam from Gia Mai

2nd SPS Vietnam from Gia Mai

Statistics

Views

Total Views
2,573
Views on SlideShare
2,573
Embed Views
0

Actions

Likes
0
Downloads
90
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • What => Why => Who => Where => When => How
  • What is Shell?Working with a command-line interface, often called a shell, can be a daunting task. If you grew up working with DOS or Unix-based system, you'll feel right at home with PowerShell.If you're used to a computer that always presents you with a Graphical User Interface(GUI), then typing your commands into PowerShell may seem like going backward in time. After you become familiar with a command-line user interface, however, don't be surprised if you get fond of the speed, flexibility, and robustness you find there; you may get a hooked.What is PowerShell?Unix-like shellObject oriented.NETCommand lineScripting languageA single mindset for management of many different systems (e.g.: AD, Exchange, Windows)Extensible framework that can be embedded inside other applications
  • Over the last few years, PowerShell scripting has begun to replace older DOS-style batch files and VBScript files as the preferred way to manage and automate administrative tasksCmdlets are callable functions Pipelining allow one Cmdlet to return an object as input to anotherPowerShell includes formatting features to display output using lists or tablesPowerShell is based on a provider-based model based on Snap-insSharePoint support added through Microsoft.SharePoint.Powershell..Snapin: Group of PowerShell functionality
  • Previous version of WSS relies on STSADM.EXEUsed for command line administration and scriptingPSDrivesFileSystem Provider- access to the file systemMap to key SharePoint directoriesSTSADM via PowerShellExecute operations via the PowerShell hostIntegrate STSADM operations into the pipelineScripting via Object ModelAccess SharePoint objectsCreate reusable SharePoint-aware commandsCustom DevelopmentCmdLetsProvidersHost ApplicationsAutomated build and deploy: => Save time for Deploy/Test Cycle phases
  • This is expensive! The first operation essentially iterates over every site twice—AND it has to start and stop the stsadm logging/process/etc for each feature activation!!The first took 12+ hours and NEVER FINISHED.The latter took roughly 30 minutes.
  • Automation: using Task Scheduler for running Backup and Restore.Development:Testing: usingPowerShell for stress test
  • The first example shows calling a Cmdlet that takes no parametersThe second example adds filtering by adding a Where commandWhere-Object {$_.name –eq "F*"}The syntax $_ refers to the object in question$_.name refers to the object's name property-eq is the operator for equalsThe third example adds in formatting instructionsThe last example redirects output so it is stored in a new text files.
  • Get-Help cmdlet
  • You can use aliases to save yourself the time and effort of typing cmdletsthat you use frequently. If you want to see all the aliases that PowerShell has mapped to specific cmdlets, use the Get-Alias cmdlet.
  •  
  • By default, PowerShell does not allow script to runAdministrator must change execution policy to enable script executionExecution Policy Settingsrestricted (default) – scripts prohibited from executingunrestricted - scripts can execute. Scripts that are signed can run with user interaction. Scripts that are not signed result in prompting user for permission to execute.bypass (developer mode) – scripts can execute and user interaction is suppressed.
  • With the initial release of PowerShell, administrators and developers usually resorted to writing scripts with NotePad. PowerShell 2 provides a nice development environment for writing PowerShell scripts named the PowerShell Integrated Scripting Environment (ISE). When it comes t writing complex scripts with control of flow logic, it is really nice to be able to debug and single step through your code.
  • Thisexample shows a PowerShell script which uses the New-SPSite cmdlet to create a new site. You should observe that the New-SPSite cmdlet returns a SPSite object which is then used to access the SPWeb object for the top-level site.
  • SharePoint Server 2010 adds many Cmdlets for SharePoint administration through a Snap-in provider named Microsoft.SharePoint.PowerShell. The SharePoint cmdlets cannot be called until the SharePoint snap-in has been loaded.There are two common ways to load the SharePoint snap-inCall the Add-PSSnapin cmdlet and pass a parameter with the snap-in nameLaunch the PowerShell console using SharePoint Management Console. This link points to an XML file that loads the SharePoint snap-in in a declarative fashion.

Introduction to windows power shell in sharepoint 2010 Introduction to windows power shell in sharepoint 2010 Presentation Transcript

  • Introduction to Windows PowerShell in SharePoint 2010
    Gia Duy Mai
    Solutions Assistant Lead
    Bamboo Solutions
  • Agenda
    Introduction to PowerShell
    Why PowerShell for SharePoint?
    When use PowerShell?
    PowerShellvs 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 PowerShell
    • Pipelining 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()
  • Why PowerShell for SharePoint 2010?
    Automated build and deploy
    Rapid prototyping
    Remoting interface
    Administration automation
    Easy for controlling, manipulating and modifying our SharePoint environment
    • PowerShell support effectively replaces STSADM
    STSADM.EXE
    We’ll miss you old friend
  • PowerShell vs STSADM
  • PowerShell vs STSADM - Performance
    Performance benefits for batch operations
    STSADM Batch Operation
    PowerShell Cmdlets in Batch
    Start Thread
    New STSADM Execution
    Start Thread
    Load DLLs
    Start logging
    Repeat
    Start Logging
    Execute Command
    Repeat
    Execute Command
    Stop logging
    Stop Logging
    Stop Thread
    Close STSADM & Thread
  • 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;
             }
    }
    12+ Hours
    (5300 SPSites)
    30 Minutes
    (5300 SPSites)
    Get-SPSite –Limit ALL –WebApplication $WebAppNameorUrl |%{
    Enable-SPFeature$FeatureIdOrName –url $_.Url
    }
  • 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
    Traditional SharePoint Development
    Wasted time for testing
    When you use PowerShell
  • Who should learn PowerShell?
    • SharePoint Admins/Devs should learn PowerShell basics
    Why you should learn PowerShell?
    Microsoft says it’s important
    You can’t do everything from the GUI any more
    It can make your life easier
    Most Microsoft products will eventually use it
    Microsoft certification exams contain PowerShell questions
    Improve your job
    It’s not going away anytime soon
  • Getting Started with PowerShell
    (1) Execute a Cmdlet
    (2) Filter results using where clause
    (3) Add formatting instructions
    (4) Redirect output to new text file
  • 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 Types
    Loosely 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”; }
    - ForeachLoop
    $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)
    ForeachLoop with Pipeline
    Get-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
    FunctionListRootDrive($DriveLetter)
    { Get-ChildItem–Path $DriveLetter}
    Function ListRootDrive
    { Get-ChildItem–Path $Args}
    FunctionListRootDrive
    { Get-ChildItem–Path $Args[0] }
    FunctionBlogUsers($SiteCollection) {
    Get-SPWeb-Site $SiteCollection| ForEach-Object { Write-Host “Site Title: “ $_.Title
    if ( $_.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 –gt10}
    • Alias: dir|?{$_.Length –gt10}
    You can find these aliases with the Get-Aliascmdlet
  • Creating your own PowerShell scripts
  • Tools
    Notepad
    PowerShell Command
    Windows PowerShell Integrated Scripting Environment (ISE)
    PowerGUI
    - Download from powergui.org
  • How to Create and Run a PowerShell Script
    The steps to create a script follow:
    Create the script in Notepad and save with a .PS1 file extension (hello.ps1).
    Run the script by entering the full path to the script (c:scriptshello.ps1), or if it’s in the current directory (.hello.ps1).
    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 scriptshello.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.
  • Demo – Series Of Scripts
    Run hello.ps1 script
    How to use PowerShell to create sites.
    How to use PowerShell to populate items in a List for testing, adding attachments
    How to use PowerShell to copy items from one list to another.
    How to use PowerShell to upload documents to a document libraries.
    How to use PowerShell to backup sites.
  • Q&A
  • Resources
    http://technet.microsoft.com/en-us/library/ee890105.aspx
    http://sharepointpsscripts.codeplex.com/
    http://www.sharepointmonitor.com/2010/04/getting-started-powershell-in-sharepoint/
    http://stackoverflow.com/questions/2143460/methods-to-convert-c-code-to-a-powershell-script
    http://www.techrepublic.com/blog/10things/10-reasons-why-you-should-learn-to-use-powershell/1073
  • THANK YOU!