Automating%20Common%20SharePoint%20Tasks%20with%20PowerShell - PowerPoint PPT Presentation

About This Presentation
Title:

Automating%20Common%20SharePoint%20Tasks%20with%20PowerShell

Description:

Cool .bat file. VBScript replacement. Admin's Best Friend. Developer's Best Friend ... Tools to make your life easier. PowerTab. PowerShell Community Extensions ... – PowerPoint PPT presentation

Number of Views:292
Avg rating:3.0/5.0
Slides: 44
Provided by: erikr155
Category:

less

Transcript and Presenter's Notes

Title: Automating%20Common%20SharePoint%20Tasks%20with%20PowerShell


1
Automating Common SharePoint Tasks with PowerShell
  • Neil Iversen
  • Inetium
  • http//justaddcode.com

2
What is PowerShell?
  • Different things to different people
  • Its a
  • Shell
  • Cool .bat file
  • VBScript replacement
  • Admins Best Friend
  • Developers Best Friend

3
An ugly but powerful shell
4
Very Scientific Language Usefulness Continuum
(Patent Pending)
SysAdmin
Printer Drivers
  • C

.NET (C/VB.NET)
Scripting Languages (Ruby, Python)
BASH, SH
  • PowerShell

5
How can it help with Development?
  • Traditional SharePoint Development
  • Time wasted during Testing
  • PowerShell / .NET Hybrid Development
  • Risky development done in PoSH
  • Code converted to .NET (C/VB)
  • Shorter Deploy/Test Cycle

6
How can it help with Administration?
7
So why isnt everyone using it?
  • Perception of PowerShell as a Shell
  • No first party Snapins
  • Slow SharePoint community adoption
  • Most admins arent comfortable with .NET
  • Most devs arent comfortable in the shell

8
Core Components
  • Alias
  • cd set-location
  • Dir get-childitem
  • Cmdlet
  • Workhorse of PowerShell
  • Function/Filter
  • Block of script
  • Provider
  • Adds alternate directory structure
  • Think /proc only niftier
  • Get-PSdrive
  • Snapin
  • Group of PowerShell functionality

9
Getting Around
  • ls
  • cd
  • rm
  • mkdir
  • man
  • dir
  • cd
  • del
  • Mkdir
  • help

10
Conditions and Flow Control
  • Variables
  • foo bar implicitly typed as string
  • ary 4,2,5,2 typed as object
  • Some Operators
  • -eq
  • -lt / -gt
  • -le / -ge
  • -like / -notlike
  • -match / -imatch
  • Control
  • If
  • switch
  • help about_comparison_operators

11
The Pipeline
  • First some background
  • In DOS/Unix
  • Dir more
  • Run a directory and pipe the output to the More
    command
  • Actually passes the resulting text to the next
    command
  • Unix has advanced text manipulation functions to
    parse
  • In PowerShell
  • Dir more
  • Passes the .NET Objects between commands
  • System.IO.FileInfo in this case

12
The Pipeline A Visual
Traditional Shell
Green Car Red Car Blue Car
PowerShell
Color Green Doors 2 MPG 45
Color Red Doors 2 MPG 36
Color Blue Doors 2 MPG 27
13
The Pipeline
  • The most unique feature of PowerShell
  • Since everything in PowerShell is a .NET type, it
    can be passed as an argument
  • Enables a LINQ-esque experience
  • C
  • foo Class.Method()
  • Bar OtherClass.Method(foo)
  • Baz OtherOtherClass.Method(bar)
  • PowerShell
  • Class.Method() OtherClass.Method()
    OtherOtherClass.Method()

14
Common Pipeline Commands
  • Foreach-object
  • dir foreach-object _.Name
  • Alias dir _.Name
  • Where-object
  • dir where-object _.Length gt 10
  • Alias dir ? _.Length gt 10
  • Select-object
  • dir select-object first 5
  • Alias none
  • Honorable Mentions Sort-Object, Group-Object

15
Dealing with Output
  • Formatting
  • Output
  • Format-Custom
  • Format-List
  • Format-Table
  • Format-Wide
  • Out-Default
  • Out-Null
  • Out-Host
  • Out-Printer
  • Out-String

16
DEMO Getting Around
17
Scripting in PowerShell
  • Loosely typed variables
  • foo bar (implicit System.String)
  • ary 1,2,3,4 (object array)
  • Strongly typed variables
  • stringfoo bar
  • Enhanced Types
  • xmld ltagtltbgtltcgtc stuff 1lt/cgtltcgtc stuff
    2lt/cgtlt/bgtlt/agt
  • d.a.b.c (array of strings)

18
DEMO Building Solutions
19
Getting started with SharePoint
  • System.Reflection.AssemblyLoadWithPartialName(
    Microsoft.SharePoint)
  • Use .NET to load it into our shell
  • Assumes its in the GAC and there is only one
    version

20
Next Up, lets get a (SP)Site
  • site new-object Microsoft.SharePoint.SPSite(ht
    tp//thesite/site)
  • site is a variable to store the SPSite into
  • new-object creates a new instance of an Object
  • The Constructor for SPSite has an overload with
    one argument (the url)

21
And how about a (SP)Web?
  • web site.OpenUrl()
  • web stores the SPWeb

22
DEMO Working with SharePoint
23
Is there an easier way?
  • That requires some API knowledge that Id rather
    not have
  • More Developer focused than Administrative

24
PowerShell Packaging
  • Scripts
  • PowerShell code in a PS1 (similar to .bat files)
  • Snapins
  • Like Solutions in SharePoint, they hold a lot
  • Compiled into dlls
  • CmdLets
  • Like Features in SharePoint, performs a task
  • Can be compiled or in script (v2 only)

25
Making yourself at (home)
  • Microsoft.PowerShell_profile.ps1
  • .bashrc
  • .tcshrc
  • .whatEverKSHuses
  • Be lazy, profile tells you where to go
  • PSgt notepad profile
  • Common Uses
  • Custom prompt()
  • Load custom variables and scripts
  • For Us? Store all those great SharePoint Commands

26
DEMO Packaging get-SPWeb
27
Working with SPLists
  • Display all the Lists
  • web.Lists
  • Getting a specific List
  • list web.ListsTasks
  • Getting all the Items
  • list.Items
  • Display Properties Sorted by Name
  • item.Properties sort prop Name
  • Set an items properties
  • item.PropertiesSomeKey value
  • item.Properties.Update()

28
DEMO Working with a Task List
29
Exploring the Object Model
  • Many objects to choose from
  • Search
  • Profiles
  • Unfamiliar objects require the SDK as reference
  • Or a kind person to wrap them

30
DEMO Exploring the Object Model
31
Extending the Object Model
  • Updating Types
  • Similar to Extension Types
  • Add-Member
  • Add new Methods/Properties to an instance
  • Use ps1xml file to make changes semi-permanent
    and always applied for a specific .NET type

32
DEMO Using Type Extensions
33
Implementing Disposal
  • Some SharePoint Objects are Messy
  • SPWeb, SPSite,
  • Object that implement IDisposable
  • Need to get Dispose()d
  • Free memory and connections
  • Very Difficult in PowerShell

34
Introducting the cleaner
  • Add-DisposableItem
  • PowerShell Filter
  • Adds any IDisposable object to a list
  • Can be used Inline (ex site.AllWebs)
  • Dispose-DisposableItems
  • Calls Dispose() on all objects in the List
  • Still in Testing, Your Mileage My Vary
  • Consult Owners Manual

35
DEMO IDisposable
36
Working with SharePoint Remotely
  • Use the Web Services and RPC
  • Both are hard to use without some help
  • Web Services are easy with new-webserviceproxy.ps1

37
DEMO Remoting SharePoint
38
PowerShell v2 Enhancements
  • V2 Timeline
  • CTP 3 in December
  • RTM in Late 09 Early 2010
  • Remote Command Execution
  • SharePoint might not be remoteable, but
    PowerShell is
  • Easier to Share Code
  • Code based CmdLets and Modules
  • Advanced Language Features
  • Native ability to run multiple processes in the
    background

39
Tools to make your life easier
  • PowerTab
  • PowerShell Community Extensions
  • PowerShell Analyzer
  • PoshConsole
  • PowerShell Plus

40
Your Feedback is Important
  • Please fill out a session evaluation form and
    either put them in the basket near the exit or
    drop them off at the conference registration
    desk.
  • Thank you!

41
References
  • PowerShell Building Blocks for SharePoint
  • PowerShell Community Extensions
  • PoshCode.org
  • New-webserviceproxy.ps1

42
Questions?
43
Thanks!
  • Neil Iversen
  • Inetium
  • http//justaddcode.com
Write a Comment
User Comments (0)
About PowerShow.com