Transcript 2010-09-23 - SharePoint Bruger Gruppe (SPBG)
http://spbg.dk
SHAREPOINT BRUGER GRUPPE (SPBG)
SharePoint Bruger Gruppe
DAGENS PROGRAM
Vi skal snakke om Status på SPBG Ny struktur på vores ERFA møder Andre møder Dagens emne: PowerShell i SharePoint 2010
SharePoint Bruger Gruppe
STATUS PÅ SPBG
Praktisk information Lokaler, sodavand og sandwich bliver sponseret af Enabling Danmark Lokaler Vi mangler sponsorer til lokaler til vores ERFA møder
SharePoint Bruger Gruppe
NY STRUKTUR PÅ VORES ERFA MØDER
Mere diskussion!
SharePoint Bruger Gruppe
DAGENS EMNE
PowerShell i SharePoint 2010 af Per Jakobsen
SharePoint Bruger Gruppe
ANDRE MØDER
SharePoint dinner inden jul, følg med på http://spbg.dk
og twitter.
SharePoint Bruger Gruppe
PAUSE
SharePoint Bruger Gruppe
POWERSHELL AGENDA
Introduction to Powershell PowerShell and SharePoint 2010 Custom Cmdlet PowerShell and SharePoint 2007 Actions Per Jakobsen
SharePoint Bruger Gruppe
A SHELL LIKE UNIX
chdir ls cp erase mkdir cat cd dir copy del type Set-Location Get-ChildItem Copy-Item Remove-Item New-Item –type directory Get-Content
SharePoint Bruger Gruppe
A SHELL LIKE UNIX
Aliases History (History, r, F7, F8) Tab completion (incl. wildcard) Piping (|) Redirecting (>, >>, 2>, 2>>, 2>&1) Tee
SharePoint Bruger Gruppe
A SHELL LIKE UNIX
grep (Select-String) Immediate expressions 2+2 “Hello ” + “World” Beware type of left side wins 2 + “2” “2” + 2 2 + “test”
SharePoint Bruger Gruppe
BETTER THAN UNIX SHELL
Virtual drives C:, D:,… ENV: HKLM:, HKCU: Variable: … Get-PSDrive Disk drives Environment variables Registry Variables
SharePoint Bruger Gruppe
BETTER THAN UNIX SHELL
Objects as long as possible Get-Process | Where-Object {$_.Handles –lt 500} | Sort-Object CPU –desc | Select-Object –first 5
SharePoint Bruger Gruppe
BETTER THAN UNIX SHELL
Output formatting Format-Table Format-Table –auto Format-List
SharePoint Bruger Gruppe
VARIABLES
Prefixed by $ Untyped by default, but can be typed $a = 1 $a = “test” [int]$a = 1 $a = “test” Error $a = “2” OK, converted to int
SharePoint Bruger Gruppe
VARIABLES
Default scope when setting: At command line In script In function Global Script Local When reading first defined of Local, script, global Can be specified: $global:a = 42
SharePoint Bruger Gruppe
VARIABLES
One Variable to rule them all, One Variable to find them all, One Variable to bring them all and in darkness bind them $_ 1..10 | Where-Object {$_%2 –eq 1} | ForEach-Object {$_*$_}
SharePoint Bruger Gruppe
LITERALS (STRING)
" text " – expands: $var Content of variable $(expr) Value of expression ` used as escape character (NOT \) ' Text ' – not expanding @ " <
SharePoint Bruger Gruppe
LITERALS (NUMBERS)
1 Int32 / Int64 0x10 Int32/ Int64 (hexadecimal) 1.1 Double 1kb * by 1024 1mb * by 1024*1024 1gb * by 1024*1024*1024
SharePoint Bruger Gruppe
LITERALS (OTHER)
1,2,3 Array 1..10
Array (Range) @(1) Wraps in array if not already array @{a=1;b=2} HashTable [System.Enviroment] => Type Use :: to access static methods/properties {code} ScriptBlock (delegate)
SharePoint Bruger Gruppe
FUNCTION (SIMPLE)
{ Function sqr($x) } Sqr 4 Sqr(5) $x*$x;
SharePoint Bruger Gruppe
FILTER
{ Filter odd if ($_ % 2 –eq 1) { $_ } } 1..10 | odd
SharePoint Bruger Gruppe
FUNCTION (ADVANCED)
{ Function test($x) begin {“B:Param $x”} process {“P:Param $x value $_”} end {“E:Param $x”} } 1..10|test(42)
SharePoint Bruger Gruppe
.NET
Everything in PowerShell is “just” .Net
Tab completion through reflection $d = [System.DateTime]::Now $d.AddDays(42)
SharePoint Bruger Gruppe
XML
Just casting a xml string to [xml] gives you a nice XmlDocument $sols = [xml](stsadm –o enumsolutions) $sols.Solutions.Solution[0] $sols.Solutions.Solution | Where-Object {$_.Name –like “*stsadm*”}
SharePoint Bruger Gruppe
SECURITY
Secure out of the box Scripts (.ps1) are mapped to NotePad Current directory not in path (use .\xxx) By default doesn’t allow scripts Change to allow local scripts: Set-ExecutionPolicy RemoteSigned
SharePoint Bruger Gruppe
REMOTING
Enable-PSRemoting (once + restart) Interactive Enter-PSSession computer –Credential usr Background $s = New-PSSession computer –Credential usr Invoke-Command $s { cmds } … Remove-Session $s
SharePoint Bruger Gruppe
SELF DOCUMENTING
Get-Help Get-Help <
SharePoint Bruger Gruppe
PAUSE
SharePoint Bruger Gruppe
SHAREPOINT 2010
PowerShell knows nothing about SP2010 But “SP2010 Management Shell” does Or Add-PSSnapIn Microsoft.SharePoint.Powershell
Add-SPShellAdmin (once)
SharePoint Bruger Gruppe
SP2010: SITES
New-SPSite url –OwnerAlias usr –Template XXX Get-SPSite url Remove-SPSite url Remove-SPSite url –confirm:$false New-SPWeb url –Template XXX Get-SPWeb url Get-SPSite url | Get-SPWeb Remove-SPWeb url
SharePoint Bruger Gruppe
SP2010: CONTENT
Get-SPWeb url | Select –Expand Lists | Select –Expand Items | Select Name, Url (Get-SPWeb url).RecycleBin
SharePoint Bruger Gruppe
SP2010: LOGS
Get-SPLogEvent –StartTime time –EndTime time | Where-Object {$_.Correlation –eq “guid” } | Select Message
SharePoint Bruger Gruppe
SP2010: OBJECT MODEL
$web = Get-SPWeb url $web.Title
$web.Title = “Title from PS” $web.Update()
SharePoint Bruger Gruppe
SP2010: OBJECT DISPOSAL
Start-SPAssigment -global $web = Get-SPWeb url … Stop-SPAssignment -global $gc = Start-SPAssigment $web = $gc | Get-SPWeb url … Stop-SPAssignment $gc
SharePoint Bruger Gruppe
PAUSE
SharePoint Bruger Gruppe
CUSTOM CMDLET
Add Reference: System.Management.Automation.dll
Microsoft.SharePoint.Powershell.dll
Inherit from SPXXXCmdletBase Add Attributes Cmdlet SPCmdlet Add xml files in SPRoot\Config\PowerShell
SharePoint Bruger Gruppe
SHAREPOINT 2007
PowerShell knows nothing about SP2007 But you can load the dll(s) [System.Reflection.Assembly]::LoadWithPartial Name(“Microsoft.SharePoint”) Then you can just use the object model
SharePoint Bruger Gruppe
SHAREPOINT 2007
$site = [Microsoft.SharePoint.SPSite] (“http://localhost”) $web = $site.OpenWeb() $web $web.Title
$web.Title = “Updated from PS” $web.Update() $web.Dispose() $site.Dispose()
SharePoint Bruger Gruppe
USE PJA’S SP2007 STARTUP SCRIPT
To use PJA’s startup script do this once Set-ExecutionPolicy RemoteSigned If (!(Test-Path $profile)){New-Item –type file –force $profile} Notepad $profile <
SharePoint Bruger Gruppe
PJA’S SP2007 STARTUP SCRIPT
} { function Get-SPSite($url) [Microsoft.SharePoint.SPSite]($url); { # Setting up environment # # Set up path for visual studio tools # function VsVars32() $vs100comntools = (Get-ChildItem env:VS100COMNTOOLS).Value
$batchFile = [System.IO.Path]::Combine($vs100comntools, "vsvars32.bat") $cmd = "`"$batchfile`" & PATH" Set-Content env:\Path (cmd /c $cmd)[1].Split("=")[1] } VsVars32 # Setup path to SharePoint tools # New-Variable 12Hive "C:\Program Files\Common Files\Microsoft Shared\web server extensions\12" -option constant Set-Content Env:\Path ((Get-Content Env:\Path)+";$12Hive\bin;c:\windows\system32\inetsrv") # Load SharePoint Assembly # [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
SharePoint Bruger Gruppe
ACTIONS
Use Get-Help Use Get-Command Use Get-Member
Get Started
SharePoint Bruger Gruppe
ANDRE MØDER
SharePoint dinner inden jul, følg med på http://spbg.dk
og twitter.