Title: Automating%20Common%20SharePoint%20Tasks%20with%20PowerShell
1Automating Common SharePoint Tasks with PowerShell
- Neil Iversen
- Inetium
- http//justaddcode.com
2What is PowerShell?
- Different things to different people
- Its a
- Shell
- Cool .bat file
- VBScript replacement
- Admins Best Friend
- Developers Best Friend
3An ugly but powerful shell
4Very Scientific Language Usefulness Continuum
(Patent Pending)
SysAdmin
Printer Drivers
.NET (C/VB.NET)
Scripting Languages (Ruby, Python)
BASH, SH
5How 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
6How can it help with Administration?
7So 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
8Core 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
9Getting Around
10Conditions 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
11The 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
12The 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
13The 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()
14Common 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
15Dealing with Output
- Format-Custom
- Format-List
- Format-Table
- Format-Wide
- Out-Default
- Out-Null
- Out-Host
- Out-Printer
- Out-String
16DEMO Getting Around
17Scripting 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)
18DEMO Building Solutions
19Getting 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
20Next 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)
21And how about a (SP)Web?
- web site.OpenUrl()
- web stores the SPWeb
22DEMO Working with SharePoint
23Is there an easier way?
- That requires some API knowledge that Id rather
not have - More Developer focused than Administrative
24PowerShell 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)
25Making 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
26DEMO Packaging get-SPWeb
27Working 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()
28DEMO Working with a Task List
29Exploring the Object Model
- Many objects to choose from
- Search
- Profiles
- Unfamiliar objects require the SDK as reference
- Or a kind person to wrap them
30DEMO Exploring the Object Model
31Extending 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
32DEMO Using Type Extensions
33Implementing 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
34Introducting 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
35DEMO IDisposable
36Working 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
37DEMO Remoting SharePoint
38PowerShell 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
39Tools to make your life easier
- PowerTab
- PowerShell Community Extensions
- PowerShell Analyzer
- PoshConsole
- PowerShell Plus
40Your 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!
41References
- PowerShell Building Blocks for SharePoint
- PowerShell Community Extensions
- PoshCode.org
- New-webserviceproxy.ps1
42Questions?
43Thanks!
- Neil Iversen
- Inetium
- http//justaddcode.com