Administering Share Point with Windows Power Shell Go

  • Slides: 23
Download presentation
Administering Share. Point with Windows Power. Shell Go Beyond the Management Shell with Share.

Administering Share. Point with Windows Power. Shell Go Beyond the Management Shell with Share. Point and Windows Power. Shell great people, great experience, great passion

Gary Lapointe Aptillon, Inc. – Director and Founding Partner – http: //www. aptillon. com

Gary Lapointe Aptillon, Inc. – Director and Founding Partner – http: //www. aptillon. com Share. Point MVP since January 2008 Blog: http: //blog. falchionconsulting. com Twitter: @glapointe great people, great experience, great passion

Agenda Conditional Logic Iteration Functions and Parameters Supporting the Pipe. Line Scripts Error Handling

Agenda Conditional Logic Iteration Functions and Parameters Supporting the Pipe. Line Scripts Error Handling Remoting great people, great experience, great passion

Load Snap-in For any Editor great people, great experience, great passion

Load Snap-in For any Editor great people, great experience, great passion

Conditional Logic Only perform a given task when an expression evaluates to $true if

Conditional Logic Only perform a given task when an expression evaluates to $true if / else switch if (<expression>) { <code> } else { <code> } switch (<value to evaluate>) { <value 1> { <code> } <value 2> { <code> } <value N> { <code> } default { <code to execute if no condition met> } } great people, great experience, great passion

Iteration While while (<conditional expression>) { <code> } Do While Warning!!! do { <code>

Iteration While while (<conditional expression>) { <code> } Do While Warning!!! do { <code> } while (<conditional expression>) Do Until The statement is notexpression>) the same as the do { foreach <code> } until(<conditional For. Each-Object cmdlet (aliased as foreach and %) For Loop for (<start exp>; <end condition>; <step exp>) { <code> } Foreach foreach (<variable> in <collection>) { <code> } great people, great experience, great passion

Quick Tips Verify your conditional logic before making changes! Test your loops before making

Quick Tips Verify your conditional logic before making changes! Test your loops before making any changes within them! Watch for disposal issues when iterating through SPSite and SPWeb objects! great people, great experience, great passion

Functions function <Name> (<parameter list>) { <code> [return [<variable>]] } Abstract common/complex tasks into

Functions function <Name> (<parameter list>) { <code> [return [<variable>]] } Abstract common/complex tasks into a reusable unit of code Name and call functions just like cmdlets: function Set-SPList. Versioning($list, $enable) {…} Set-SPList. Versioning $list $true Set-SPLIst. Versioning -list $list -enable $true great people, great experience, great passion

Function Parameters Can use either implicit or explicit parameters Implicit Example: function <Name>($param 1,

Function Parameters Can use either implicit or explicit parameters Implicit Example: function <Name>($param 1, $param 2, $param. N) { … } Explicit Example: function <Name>() { param ( $param 1, $param 2 ) } Optionally prefix variable names with type information great people, great experience, great passion

Advanced Function Parameters With explicit parameters you can add cmdlet binding attributes function Set-SPList.

Advanced Function Parameters With explicit parameters you can add cmdlet binding attributes function Set-SPList. Versioning() { [Cmdlet. Binding()] param ( [Parameter(Mandatory=$true, Position=0)], [Validate. Not. Null()] [Microsoft. Share. Point. SPList]$List, [Parameter(Mandatory=$false, Position=1)] [switch]$Enable ) <your code here> } Type help about_functions_advanced_parameters for more examples great people, great experience, great passion

Supporting the Pipe. Line Be Careful!! function <Name> (<parameter list>) { begin { <execute

Supporting the Pipe. Line Be Careful!! function <Name> (<parameter list>) { begin { <execute once before processing> } processenabled { <execute for each item> } handling Pipe. Line functions need extra end { <execute once after processing> } when called like a normal function } Current object is accessible via the $_ automatic variable great people, great experience, great passion

Quick Tips Best Practice: Include a Pipeline parameter and name it $Input. Object Use

Quick Tips Best Practice: Include a Pipeline parameter and name it $Input. Object Use the filter statement if begin {} and end {} are not needed: great people, great experience, great passion

Scripts A script is, effectively, just a function known by a file name A

Scripts A script is, effectively, just a function known by a file name A script can contain many different functions, but doesn’t have to have any A script can have explicit parameters, just like a function Give scripts a. ps 1 file extension Load all script functions into memory using “dot source notation” great people, great experience, great passion

Quick Tips Be careful of scripts that execute when loaded – Use a function

Quick Tips Be careful of scripts that execute when loaded – Use a function to wrap tasks Functions within scripts can be made available without dot sourcing by changing the scope of the function: function global: <name>() { … } great people, great experience, great passion

Error Handling try { <code> } catch [error type][, [error type]] { <code: executed

Error Handling try { <code> } catch [error type][, [error type]] { <code: executed on error> } finally { <code: always executed when finished> } try/catch/finally only works with V 2 – Use trap statements for V 1 (or just don’t use V 1!) In the catch statement block, use the $_ automatic variable to access the error details The finally statement block is optional great people, great experience, great passion

Administering Share. Point remotely… WINDOWS POWERSHELL REMOTING great people, great experience, great passion

Administering Share. Point remotely… WINDOWS POWERSHELL REMOTING great people, great experience, great passion

Remoting Power. Shell Remoting uses Win. RM, Microsoft’s implementation of the WSManagement protocol –

Remoting Power. Shell Remoting uses Win. RM, Microsoft’s implementation of the WSManagement protocol – Win. RM allows you to run scripts against remote servers over HTTP and HTTPS Works with V 2 only Requires that Win. RM be enabled on both the client and the server great people, great experience, great passion

Enabling Remoting Run Enable-Ps. Remoting on the client and server machines Must Enable Cred.

Enabling Remoting Run Enable-Ps. Remoting on the client and server machines Must Enable Cred. SSP – Credential Security Support Provider – Allows cmdlets to talk to SQL using the provided credentials (handles the double-hop issue) Increase the Max. Memory. Per. Shell. MB setting on remote server (default is 150 MB) – Set-Item WSMan: localhostShellMax. Memory. Per. Shell. MB 1000 Decrease Max. Shells. Per. User and Max. Concurrent. Users (default is 5) – Set-Item WSMan: localhostshellMax. Shells. Per. User 2 – Set-Item WSMan: localhostshellMax. Concurrent. Users 2 great people, great experience, great passion

Enabling Cred. SSP On the client machine – Group Policy must be edited to

Enabling Cred. SSP On the client machine – Group Policy must be edited to allow credential delegation to the target computer. • Use gpedit. msc – Computer Configuration -> Administrative Templates -> System -> Credentials Delegation -> Allow Delegating Fresh Credentials – Verify that it is enabled and configured with an SPN appropriate for the target computer (WSMAN/myserver. domain. com or WSMAN/*. domain. com) – May have to similarly enable “Allow Fresh Credentials with NTLM-only Server Authentication” if the above setting does not work – Enable-WSman. Cred. SSP -Role Client Delegate. Computer <remote server name> On the server machine – Enable-WSman. Cred. SSP -Role Server great people, great experience, great passion

Running Remote Commands great people, great experience, great passion

Running Remote Commands great people, great experience, great passion

Session Configurations Use Register-PSSession. Configuration to preload Share. Point Power. Shell Snap-In – Must

Session Configurations Use Register-PSSession. Configuration to preload Share. Point Power. Shell Snap-In – Must also set threading options Use Set-PSSession. Configuration with Show. Security. Descriptor. UI parameter to set security Provide configuration name when calling New. PSSession great people, great experience, great passion

Windows Power. Shell Remoting DEMO… great people, great experience, great passion

Windows Power. Shell Remoting DEMO… great people, great experience, great passion

Summary If you plan on writing more than 2 -3 lines of Power. Shell

Summary If you plan on writing more than 2 -3 lines of Power. Shell then consider using a script editor Test your scripts incrementally – Validate your loops and conditionals before making changes! Consider Power. Shell remoting over integrated login for common admin tasks great people, great experience, great passion