I have such a hard time expressing how I really feel about online courses. That was precisely what happened as I was taking Gary LaPointe’s Pluralsight Course “Using Windows PowerShell with SharePoint 2010 and SharePoint 2013”.
Seriously, the course filled some gaps in my PowerShell knowledge and I am still reviewing the demos to absorb more of the SharePoint and PowerShell Magic.
The magic which I wish to direct attention to today is the awesomeness of PowerShell passing objects down a pipeline to be acted on by other commands. Also, the much improved user experience of Out-GridView command for selecting rows–made possible by the PassThru option of the command.
In this article I have focused on a few PowerShell one-liners useful in SharePoint 2013 Administration. What I will be discussing involves PowerShell Version 3.0. If you are using a SharePoint 2007 or SharePoint 2010 system, it is not advisable to download PowerShell Version 3.0. The earlier versions of SharePoint won’t play with version 3.0.
To check which version of PowerShell you are using, just open a PowerShell Window (my choice is the 64-bit PowerShell ISE Window) and display the $PSVersionTable variable and look for 3.0.
My objective for this “script du jure” which is displayed below, is simply to find the SharePoint Services that are offline (disabled) and then, to display them for selection in a grid that is displayed alphabetically by “TypeName”. Once the selections have been made, they are piped to the Start-SPServiceInstance command to be started.
The Selections are accomplished by using the PassThru parameter Out-GridView command. With the PassThru option, we can select one, many or no rows to output to the Start-SPServices command.
Finally after a five second sleep, the script displays the Services that are started (online).
The script consists of two meaty commands using the Get-SPServiceInstance command laced by some Write-Hosts and Sleep commands.
The two commands are separated on different lines by the pipe symbol, “|”, for readability.
The grid output of the two commands are as follows:
A Couple of Caveats:
- The “where” clause may look funny—but this is PowerShell Version 3.0. The braces and the “$_.” are not needed in this example–they are assumed by PowerShell.
- The conditions to check for in Status variable are “Online” and “Disabled”—not they appear in Central Admin SharePoint Services, “Started” and “Stopped”.
- Your Service may take longer than 5 Seconds to start in which case it won’t be listed in the second grid of Online Commands.
(Anyone seriously think Search or the User Profile Service will start in five seconds?)
- Your Service may NOT start: this is not an Admin panacea—it just tries to start a Disabled Service. Your system may have more serious issues.
(SharePoint? Serious Issues? Really?)
- On the First Grid-View, “List Stopped/Disabled Services”:
- Don’t use the “X” or Window Close Button it will hang your PowerShell process—use the “Cancel” button if you don’t want to select any. The remainder of the command will execute—even if no service instance is selected.
- The first Item is selected by default – if you don’t want to start it—either pick another item or choose “Cancel” to deselect all of the items.
- Multiple Items may be selected with the “Ctrl” or “Shift” keys.
Want more functionality? Embed this script in a function and add it to your PowerShell Profile:
Then simply use the command: “Start-StoppedService”.
Many thanks go out to Gary LaPointe for your inspiration and for getting me over a few of my many PowerShell hurdles.
PowerShell’s Pipeline is like the Unix Pipeline on steroids. It deals with objects (not text as in Unix) and new objects can even be constructed on the fly. Those object retain methods that can be acted on as needed. I hope to illuminate more examples of this capability in future articles.