PowerShell for SharePoint Developers and Administrators

Report
PowerShell
for SharePoint Developers and
Administrators
Michael Blumenthal
Magenic Technologies
[email protected]
Who is Michael Blumenthal?
Sr. Consultant @
Magenic (MSFT Gold Partner)
16 years in IT Consulting
8 years working with SharePoint
2
MCITP: SharePoint 2010; MCTS: WSS Dev
MCTS: MOSS Config & MCTS: WSS Config
MCAD, MCSE, MCDBA, CAPM
Why PowerShell?
No
Compiling!
No
Packaging!
Just Code &
Go!
3
PowerShell puts the SharePoint
Engine at your fingertips!
1
• It’s Easy to Get Started!
2
• Learn the PowerShell Syntax
3
• Real World Examples
4
• More Resources
• Demo!
4
Chapter 1
5
Getting Started with PowerShell
Windows Server 2003
• Download
Windows Server 2008
• Install
Windows Server 2008 R2
• Run (V2!)
7
Shell User Experience
Extensible Tab
Completion
Navigable History
>>
8
Line Continuation
9
Chapter 2
10
Learn to use PowerShell with SharePoint!
Symbols &
Keywords
Using the
SharePoint API
Creating and
Running Scripts
Symbols, Keywords, and Syntax! Oh
My!
12
1
• Variables
2
• Commands
3
• Piping
4
• Comparisons
5
• Flow Control
6
• Filtering
Punctuation Pronunciation
Symbol
Called
Symbol
Called
$
Dollar sign, money
_
Underscore
#
Pound, hash
[]
Square Brackets
|
Pipe, vertical bar
.
Dot, point, period
{}
Curly braces
<>
Angle Brackets
“
Double Quote, tick
-
Dash, hyphen, minus
:
Colon
%
Percent sign
()
Parentheses
;
Semi-colon
+
Plus
=
Equals, is
!
Bang, not
/, \
Slash, backslash
Is “$#|” a “one dollar hash pipe”?
13
1
Variables begin with a $
$foo
• Case Insensitive, Dynamic typing
$true, $false, $profile
$foo = “Hello, World”
14
15
2
Commands are called cmdlets.
Verb-Noun
Built-in, Extensible
Get-Help & Help
Get-Member
16
17
3
The Power of Piping!
Output Of
Command 1
18
Input of
Command 2
Example
4
Making Comparisons
Operator
Meaning
Operator
Meaning
-eq
Equals
-le
Less Than or Equal
To
-ne
Not Equals
-like
Wildcard Match
-gt
Greater Than
-notlike
Not (Wildcard
Match)
-ge
Greater Than or
Equal To
-match
Reg. Exp. Match
-lt
Less Than
-notmatch
Not (Reg. Exp.
Match)
5
For
ForEach
If
While
21
Taking Control of the Flow
• For (Init;Test;Repeat) {Commands}
• for($i=1; $i -le 10; $i++) {Write-Host $i}
• Foreach (Item in Collection) {Commands}
• Foreach ($web in $site.AllWebs) {$web.Title}
• If (Test) {Commands}
• if ($web.Title –ne “”) {Write-Host $web.Title}
• While (Condition){Commands}
• while($val -ne 3){$val++; Write-Host $val}
Example
6
Where-Object
Syntax •Where {<Test>}
•Dir | Where {$_.Name –like
Example
“B*”}
23
Using the SharePoint API
24
1
• Getting an SPSite
2
• Manipulating It
3
• Cleaning Up
Highlights from the SharePoint
Object Model
SPFarm
SPWebApplication
SPWeb
SPList
SPListItem
SPField
25
Loading SharePoint DLLs
[void][System.Reflection.Assembly]::
LoadWithPartialName("Microsoft.SharePoint")
26
Get a Site and Explore it!
$site = New-Object
Microsoft.SharePoint.SPSite(“http:/
/server/path”)
OR
$site = GetSPSite(“http://server/path”)
THEN
$site
27
28
Create a List Item
29
Practical Uses
30
1
• Create Sites from the Command Line
2
• Add Lists and List Items to a Web
3
• Create data for test cases
4
• Associate Workflows with a List
5
• Work across site collections
6
• Update User Metadata in AD for better Profile Pages
7
• Identify files that won’t upload
A Word About Memory Management
31
SPWeb
SPSite
Inline
In Script
32
Creating .PS1 Script Files
33
Executing Scripts
.\filename.ps1
Set-ExecutionPolicy
Unrestricted
34
Chapter 3
35
Real World Examples
Check the Version
Check Versioning on all document Libraries
Clear a list
Look at multiple Event Throttling levels at once
for the ULS (diagnostic) log
36
What’s your MOSS Version?
$farm =
[Microsoft.SharePoint.Administratio
n.SPFarm]::Local
$farm.BuildVersion
Major
----12
37
Minor
----0
Build
----0
Revision
-------6318
Check Doc Lib Versioning Settings
function global:show-all-doclibs ($web)
{$web.Lists | where-object {($_.Hidden -ne $true)
-and ($_.BaseType -eq "DocumentLibrary")} }
function global:show-all-doclib-versettings
($web)
{show-all-doclibs ($web) |select-object -property
Title, EnableVersioning, MajorVersionLimit,
EnableMinorVersions,MajorWithMinorVersionsLimit
,forceCheckout}
$site = get-spsite “http://server/path”
show-all-doclib-versettings $site.RootWeb
38
39
Clear a List
Make a mess:
Clean it up!
40
41
Look at the ULS configuration
Why?
See multiple event categories at once!
How?
$ulsid = $farm.Services | select-object
TypeName, Id | where {$_.TypeName -eq "Windows
SharePoint Diagnostics Service"} | SelectObject Id
$ulsvc = $farm.GetObject($ulsid.Id.ToString())
$ulsvc.GetItems() | Select-Object Name,
EventSeverity, TraceSeverity, hidden | where
{$_.hidden -eq 0}
42
43
Chapter 4
44
Resources
SharePoint +
Reflector / Decompiler
Microsoft Resources
3rd Party Resources
Use a Decompiler to see MSFT’s
Code!
OR
ILSpy.net
dotPeek (jetbrains)
justDecompile (Telerik)
Others…
46
47
48
49
50
51
52
Resources Summary
MSFT
Reverse Engineering SharePoint Pages
Bruce Payette’s Book
PowerShell Product Team Blog
Community
CodePlex: PSBBs
My Blog at BlumenthalIT.Net
Jeff Hicks - http://www.jdhitsolutions.com/
Neil Iverson http://www.justaddcode.com/blog/
53
In Review…
It’s Easy to Get
Started!
Learn & Use the
PowerShell Syntax
More Resources
PowerShell puts the SharePoint API
at your fingertips!
54
Chapter 5
See the power of PowerShell + SharePoint!
55
Let’s Eat!
56
Bulk Creating Sites and Site Collections
57
The Challenge
New
SharePoint
website
58
A lot of
structure
Little time
Problem Example 1
BI Solution, Contoso PAC
“Elect more fake people to fake offices nationwide!”
Based on real client though
Solution: Site Collection with 440 subsites
1 per congressional district
Need: Streamline the Creation of 440 nearly
identical subsites
59
Problem Example 2
Global Civil Engineering, Architecture, Design
Co
• New Supplemental Intranet – Communities
• Primary Intranet is Employer 2 Employee Pub
• This one is for Global Communities of Practice
• Deep hierarchy from day 1
• Technical Practice
• Technical Practice Groups
• Specific Doc Libs & lists
• Technical Discipline
60
• Subgroups
• Doc Libs and Lists
Let’s Compare Solutions
PowerShell & Excel & UI
Site Definitions in V. Studio
Not an answer by themselves
Define site content
Intended for reuse
Mismatch to one time need
CAML and PITA
Harder: Making it data driven
Change Site Def -> Recreate Site
61
Well suited for one time “blow
in’s”
Define the site template in the
UI or use standard
Save as a template
Even pub sites
PowerShell has easy loops
Data driven from a CSV
Changes -> Mod Scripts
The PowerShell Solution
Read the list of sites from CSV
Loop:
Create Site
Configure Site
Turn on Features
Set Master Pages, Welcome Page
Hide Libraries, set versioning
Adjust Navigation
Add Lists, Libraries, Pages, Web parts, etc
Loop again & again if needed – iterative!
62
Even DVWP’s become an option
Data View Web Parts of lists & libraries
Edit only in SPD
Pub Pages can’t be edited in SPD, only layouts
But wait!
Traditional limit: tied to a list by GUIDs
Power of POSH: You can edit the DVWP
definitions via the OM!
No longer “Not Deployable”!
63
So How Do I
64
Key Language Constructs
$variable and piping (|)
Import-CSV
Foreach
Function global:MyCustomFunction($foo){
write-host $foo
}
SharePoint Object Model access
65
Start with a
Spreadsheet
66
Save as a CSV
Comma
Separated
Values
“Double Quotes”
Can update CSV
directly
67
68
69
70
Calling SharePoint From POSH
2007
2010
Need to define a set of helper
functions
I use Zach’s
Get these at PSBB.CodePlex.com
500+ POSH cmdlets ship with
SP2010
Register One Snapin
$site = Get-SPSite $SCUrl
$newweb = $site.AllWebs.Add($Url, $Title,
$Description,
1033,"CMSPUBLISHING#0",$InheritParentpermissions,
$DoNotConvert)
71
SP2010 PowerShell SnapIn
Over 500 functions defined
Automatically loaded in the SP Admin Console
Can do the same in ISE and PS Profile
72
Outer Loop & Test Cases
73
Creating a site via POSH
74
DVWP? What’s that?
Only in SPD…
75
76
77
78
79
District DVWP – Runtime Update
Type 1
80
Adding the other DVWP
81
78 created sites in just 7 minutes!
82
The Challenge
You need to stand up a new SharePoint website
It needs a lot of structure
You don’t have a lot of time
No patience for manual GUI site creation
The New Answer
PowerShell!
83
Resources
Blog about SharePoint at http://BlumenthalIT.net
PSBB.CodePlex.com
Twitter: @MichaelBL
LinkedIn: http://www.linkedin.com/pub/michael-blumenthal/0/19a/354
[email protected]
84
Contact Me!
Blog about SharePoint at
http://BlumenthalIT.net
Twitter: @MichaelBL
[email protected] &
http://www.Magenic.com
85
Custom Solutions Guaranteed to fit.
Gold in Portals and Collaboration
SDPS Program Member
86

similar documents