Fall 2008 Connections Conference Template

Download Report

Transcript Fall 2008 Connections Conference Template

Automating Common SharePoint
Tasks with PowerShell
Neil Iversen
Inetium
http://justaddcode.com
What is PowerShell?
• Different things to different people
• It’s a
●
●
●
●
●
●
Shell
Cool .bat file
VBScript replacement
Admin’s Best Friend
Developer’s Best Friend
…
An ugly but powerful shell
Very Scientific Language Usefulness
Continuum (Patent Pending)
SysAdmin
Printer Drivers
C++
.NET (C#/VB.NET)
Scripting Languages (Ruby, Python)
BASH, SH

PowerShell
How can it help with Development?
• Traditional SharePoint Development
●
Time wasted during Testing
Code
Compile
Deploy
Test
Code
Compile
Deploy
• PowerShell / .NET Hybrid Development
●
●
●
‘Risky’ development done in PoSH
Code converted to .NET (C#/VB)
Shorter Deploy/Test Cycle
Prototype
(PoSH)
Code
Compile
Deploy
Test
How can it help with Administration?
Object
Model
stsadm
PowerShell
RPC/Web
Services
So why isn’t everyone using it?
• Perception of PowerShell as a Shell
• No first party Snapins
• Slow SharePoint community adoption
●
●
Most admins aren’t comfortable with .NET
Most devs aren’t comfortable in the shell
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
Getting Around
•
•
•
•
•
dir
cd
del
Mkdir
help
 ls
 cd
 rm
 mkdir
 man
Conditions and Flow Control
• Variables
●
●
$foo = “bar”
$ary = 4,2,5,2
#implicitly typed as string
#typed as object[]
• Some Operators
●
●
●
●
●
-eq
-lt / -gt
-le / -ge
-like / -notlike
-match / -imatch
• Control
●
●
If
switch
• help about_comparison_operators
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
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
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()
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
Dealing with Output
Formatting
Output
•
•
•
•
•
•
•
•
•
Format-Custom
Format-List
Format-Table
Format-Wide
Out-Default
Out-Null
Out-Host
Out-Printer
Out-String
DEMO: Getting Around
Scripting in PowerShell
• Loosely typed variables
●
●
$foo = “bar” (implicit System.String)
$ary = 1,2,3,4 (object array)
• Strongly typed variables
●
[string]$foo = “bar”
• Enhanced Types
●
●
[xml]$d = “<a><b><c>c stuff 1</c><c>c stuff
2</c></b></a>”
$d.a.b.c (array of strings)
DEMO: Building Solutions
Getting started with SharePoint
• [System.Reflection.Assembly]::LoadWithP
artialName(“Microsoft.SharePoint”)
●
●
Use .NET to load it into our shell
Assumes its in the GAC and there is only one
version
Next Up, lets get a (SP)Site
• $site = new-object
Microsoft.SharePoint.SPSite(“http://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)
And how about a (SP)Web?
• $web = $site.OpenUrl()
●
$web stores the SPWeb
DEMO: Working with SharePoint
Is there an easier way?
• That requires some API knowledge that I’d
rather not have
• More Developer focused than
Administrative
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)
Making yourself at ~ (home)
 Microsoft.PowerShell_profile.ps1



.bashrc
.tcshrc
.whatEverKSHuses
 Be lazy, $profile tells you where to go

PS> notepad $profile
 Common Uses



Custom prompt()
Load custom variables and scripts
For Us? Store all those great SharePoint
Commands
DEMO: Packaging get-SPWeb
Working with SPLists
• Display all the Lists
●
$web.Lists
• Getting a specific List
●
$list = $web.Lists[“Tasks”]
• Getting all the Items
●
$list.Items
• Display Properties Sorted by Name
●
$item.Properties | sort –prop Name
• Set an item’s properties
●
●
$item.Properties[“SomeKey”] = “value”
$item.Properties.Update()
DEMO: Working with a Task List
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
DEMO: Exploring the Object Model
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
DEMO: Using Type Extensions
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
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
DEMO: IDisposable
Working with SharePoint Remotely
• Use the Web Services and RPC
●
●
Both are hard to use without some help
Web Services are easy with newwebserviceproxy.ps1
DEMO: Remoting SharePoint
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
Tools to make your life easier
•
•
•
•
•
PowerTab
PowerShell Community Extensions
PowerShell Analyzer
PoshConsole
PowerShell Plus
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!
References
•
•
•
•
PowerShell Building Blocks for SharePoint
PowerShell Community Extensions
PoshCode.org
New-webserviceproxy.ps1
Questions?
Thanks!
Neil Iversen
Inetium
http://justaddcode.com