Chapter 5 – Using Windows Powershell to Perform and Automate Farm Administrative Tasks

MS Sharepoint 2010 – Administrator’s Companion (Page 195)

Powershell architecture – (1) object oriented, (2) based on .net 3.5 (.Net 3.5 for PowerShell 2.0) and (3) based on C#.

Two ways to use Powershell:
(1) command-line, or Powershell Console (powershell.exe)
(2) Windows Powershell Interactive Scripting Environment (ISE) GUI (powershell_ise.exe)

Using Powershell to install ISE interfasce:
Import-Module Servermanager; Add-WindowsFeature “Pwershell-ISE”

Using Server manager: To add ISE feature (from Server manager Features Add Features)

Powershell has a built-in security feature called Execution Policy. Can set the policy on a per-user basis. (saved in registry)

To see your execution plocy
Get-ExecutionPolicy

If you want to run config files or scripts from remote sources, they need to be signed. You can change the execution policy by using Set-ExecutionPOlicy.

To sign scripts, use the Set-AuthentiCodeSignature.

If you use Windows 7 and want to remotely manage Windows Server 2008 R2, you need the Remote server Admin Tool (RSAT).

Powershell 2.0 introduceds a new capability to manage your system remotely from your desktop by using (1) WinRM (Remote management) or (2) IIS. RM has methods known as Fan-in/out remoting:

Rremote Management —
** Fan-in remoting: Many administrators connect to an instance of Windows Powershell on the same remote servers.
** Fan-out remoting: send a single Windows Powershell command to run multiple instances of Windows Powershell in parellel, and results will return to your desktop. Use this if you need to complete the task on multiple servers. (No more remote desktop and execute locally!!!) Just create Powershell scripts and pass the server names to the command, which then will complete the command sequentially.
** Background jobs:
Windows Powershell 2.0 supports both local and remote background jobs. The command prompt is returned immediately after execute the command.

Configuring WinRM —

The WinRM windows service must be started and configured for remoting on both the local and server for command to run. To see if this service is running, type:
Get-Service WinRM

To check if it’s running on a remote server, type:
Get-Service WinRM –computername $server_name

On your local (windows 7), the service may not be started. Type this to start:
Enable-RSRemoting
(which execute two commands: Set-WSManQuickCOnfig and Start-Service WinRM)

User must eb a member of the local WSS_ADM_WGP; must be a member of the Sharepoint_Shell_Access SQL server role on the config database, must have access to the content database.

Using WinRM Remoting —

$cred = Get-Credential contoso\spfarm — take cares of security
$sess = New-PSSession Web1 –Authentication CredSSP –Credential $cred — establishes a session
Invoke-Command –Session $sess –ScriptBlock ` {ADD-PSSnapin Microsoft.SharePoint.PowerShell;} – run command on remote server

Invoke-Command -Session $sess -ScriptBlock {get-SPServiceInstance} – this will return all the service instances on the remote server.

Concatenate and convert your script block and then execute it
$url = “http://contoso.com” – initial string
$myscript = “get-spsite $url” – keep adding to it
$sb = [scriptblock]::Create($myscript) — create a script block out of the string
Invoke-Command $sess –ScriptBlock $sb
– execute it

To run the job as a background job, add –AsJob
to the
Invoke-Command
line!!!!!!!!!!!

You can enter this and make it as if you’re working on the remote machine:
Enter-PSSession $sess

Working with Sharepoint 2010 Management Shell —

The Sharepoint Powershell command points to a file: Sharepoint.ps1.

Understanding Cmdlets —

In general, cmdlets use a verb-noun
pair. The noun specifies the object you want information about or that you want to manipulate, and the verb states what you want to do with that object.

Sharepoint cmdlet verbs have the prefix of SP. For example, to get all the content databases in the farm:
Get-SPContentDatabase

get-Command
can be shorted as gcm, Get-Help
can be shortened as help.

Each object (such as Site, Web etc.) has its own members – including properties and methods. To get all properties/methods of an object, do this:
Get-SPSite | Get-Member

Get-SPSite http://xxxx/xxxx | select *
Get-SPSite | select url, contentdatabase

Using Verbs —

Help files —

Gte-Help finds the information to display by using XML files that are installed on the server. It’s under….\14\Config\PowerShell\Help\….

ISE Help – The Help file for ISE is only good for the general Windows Powershell. If you want to get help on Sharepoint Powershell, use the Get-Help
command.

Using Parameters —

To find out what parameters are needed, type:
Get-Help <cmdlet> or
Get-Help <cmdlet> -syntax
Get-Help <cmdlet> – sample

Get-SPSite –filter {$_.Owner –eq “contoso\Peter”} – Filter is more efficient
Get-SPSite http:/xxx/xxx | Get-SPWeb – Filter {$_.Template – eq “STS#03”}
Get-SPSite http://xxx/xxx/xxx/* | select *
Get-SPContetnDatabase | where {$_.name –match “oso”} | Get-SPSite –Limit 50 – WHERE is less efficient because of SQL round trip

Wildcard and Regex URL. E.g.,
Get-SPSite http://xxx/xxx/*
Get-SPSite “http:/xxx/xxx/(site1/site2)/HR” –RegEx

Memory considerations —

Default of Powershell – multithread – each line/function/script runs in its own thread:
$host.Runspace.ThreadOptions == “Default”

But this line mitigate the problem.
$host.Runspace.ThreadOptions =”ReuseThread”

But leaks still occur, so need to use Start/Stop-AssignmentCollection.

Using SPAssignment cmdlets, all objects are kept in a global memory store that is released when the Stop-SPAssignment
cmdlet si called.

Start-SPAssignment –Global
$sc = Get-SPSite http://intranet
$sc.Title
Stop-SPAssignment –Global

$o = Start-SPAssignment
$sc = $o | Get-SPSite http://intranet
Get-SPSite –Limit all
$sc.Title
$o | Stop-SPAssignment

or simply dispose the object after using it.
$sc = New-SPSite(“http://Intranet&#8221;); $sc.Title; $sc.Dispose()


Sharepoint Foundation has 240 cmdlets.
Sharepoint Server has 530 cmdlets. (full install)

Performing Basic Administrative Tasks – two different tasks:
(1) Tasks that need to be completed only once and from any server (creating a web application, configuring owner of a site collection or deleting a web site)
(2) Tasks that need to be completed more than one on each server, or more than one server, such as starting a service on a server, adding or removing a server from a farm, or connecting the server to a specific configuration database.

Majority is the first type – do it once from any server. It doesn’t matter which server you login to, as you can do it from any server.

Using the –whatif parameter – try the command before you actually execute it!!!! Add this switch to the end of each pwershell command and then it will not get executed. Always use –whatif first!!!!

Start-Transcript and Stop-Transcript – allows you to record all the commands tha you type at the prompt.
Syntax:
Start-transcript log.txt

Microsoft developed a script to create a farm from scratch using Powershell. It’s tricky so MS suggest that you use PowerShell module called “SPModule”.

Get-EventLog to get event logs
Get-SPDiagnosticConfig – to get how ULC is configured on your farm.
Get-SPLogEvent – get the event log info

Use Correlation ID to help troubleshoot errors by reducing the amount of log information.

Get-SPLogEvent –StartDate $Sdate –EndTime $Edate | where {$_.Correlation eq “sd-34ferf-ef34fe-546ytrt-23ewd-y”}

To quickly delete a specific Web Application:
Get-SPWebApplication http://xxxxxxx | Remove-SPWebApplication –Confirm

To remove the web application, IIS Web Site, and all the associated databases:
Remove-SPWebApplication http://xxxxx – Confirm –DeleteIISSite -RemoveContentDatabases

pre-upgrade
Use Test-SPContentDatabase
to check your Sharepoint 2007 database for any potential issues, such as orphan data, missing site definition, features etc. (pre-upgrade checker)

post-upgrade (revert to Sharepoint 2007 Look & Feel)
Start-SPAssignment –Global
$web=Get-SPWeb http://xxxx;
$web.UIVersion=3;
$web.UIVersionConfigurationEnabled=$true;
$web.Update();
Stop-SPAssignment

reserved key words:
$true is True, $false is False, $null is NULL, $_ is current pipeline object.

To enable visual upgrade option —
$web=Get-SPWeb http://xxxx;
$web.UIVersionConfigurationEnabled=$true;

When updating the properties of the SPSite object, there is no need to issue UPDATE method.

To reset all “team” web site within a site collection back to the Sharepoint 2007 look & feel:
Get SP-Site http://xxxxx | Get-SPWeb –Filter {$_.Template eq “STS#0”} |
ForEach-Object {
$_.Version=3;
$_.UIVersionConfigurationEnabled = $false;
$_.Update();
}


Advertisements
Post a comment or leave a trackback: Trackback URL.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: