Showing posts with label PowerShell. Show all posts
Showing posts with label PowerShell. Show all posts

Monday, February 17, 2014

PowerShell 3.0 issue with SharePoint 2010

All of sudden my PowerShell stopped working with following error:-

“The local farm is not accessible. Cmdlets with FeatureDependencyId are not registered”



I am currently with SharePoint 2010 on my development machine on Windows 7with SP1. After SharePoint installation for couple days PowerShell was working absolutely fine but now when I need to do something with scripting it has stopped working.

Tried two things very quick but still no luck:-
  1. I have already tried to run PowerShell run as Administrator
  2. I have already given SharePoint_Shell_Access permission to the farm account on SharePoint Config. and Admin Content both databases.

As a next step I investigated the current version of PowerShell, I started PowerShell window and just run the command:-

$psversiontable

I found following:-



As you can see it is PowerShell Version 3.0 which doesn’t support SharePoint 2010 http://blogs.msdn.com/b/ronalg/archive/2013/07/29/10382902.aspx?CommentPosted=true#commentmessage

Now how come it is upgraded to PowerShell 3.0, I found that on my system there was windows update installed as shown below, which has installed Windows Management Framework 3.0. WMF 3.0 also includes PowerShell 3.0, you can see on this link http://www.microsoft.com/en-us/download/details.aspx?id=34595




Solution:
To resolve the issue quick: I changed PowerShell start menu short cut path and added –version 2.0 switch parameter, below is the complete path with –version 2.0 switch.

C:\Windows\SYSTEM32\WINDOWSPOWERSHELL\V1.0\PowerShell.exe -version 2.0 -NoExit  " & ' C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\CONFIG\POWERSHELL\Registration\\sharepoint.ps1' "
Add –version 2.0 swtich

By doing this I could run PowerShell 2.0 which support SharePoint 2010 commands

Reference links:-




Sunday, June 3, 2012

PowerShell – Understanding pipeline and extending pipeline to more than one line

When writing PowerShell script one command which goes in a long line reduces the readability of the command, this normally happens when you are taking output object of one cmdlet and passing it to another cmdlet. 

You would like to extend a long line command in multi-line to improve the readability:- 

First let me clarify what does pipeline means in PowerShell, as you know object returned by a cmdlet can be stored in a variable for later use or piped to a subsequent cmdlet as input for the subsequent cmdlet. A pipeline is a channel through which the output of a cmdlet can be passed to following cmdlet, the pipeline is represented by pipe char ‘|’

Here is an example of pipeline: - Output of Get-Service cmdlet which is SPTimerV4 service is piped to the next cmdlet Stop-Service which stops the SpTimerV4 service.


Get-Service SPTimerV4 | Stop-Service


Below are the ways how you can extend the pipeline to more than one line to improve readability.

Use Tick Mark (‘): You can use tick mark to break the line and continue the command in next line, PowerShell assumes that the subsequent line is the continuation of the current line.

Example:-

Get-Service –DisplayName *SharePoint* | ‘
Stop-Service


Use Pipe Symbol (|) as last character of the line: When Pipe symbol is the last character of a line, it indicates that the command is not complete so Windows PowerShell continues the command with the subsequent line

Example:-

Get-Service –DisplayName *SharePoint* |
Stop-Service


Use a left curly brace ({): Curly braces are normally used to enclose the structure such as expression or a procedure. A left curly brace suggests that a structure follows.