Display Easily Consumed Service Information in PowerShell

Display Easily Consumed Service Information in PowerShell

  • Comments 8
  • Likes

Summary: Learn how to display easily consumed information about system services by using Windows PowerShell.

Hey, Scripting Guy! Question Hey, Scripting Guy! I am still having trouble working with the Out-GridView cmdlet. I sort of understood what you were talking about yesterday, but when I tried it, all I got was errors. What does it mean when it says that it does not accept the input?

—RS

Hey, Scripting Guy! Answer Hello RS,

Microsoft Scripting Guy, Ed Wilson, is here. Today the Scripting Wife and I are heading back from the Mark Minasi Conference. We do not have a lot of time to tarry because tonight the Charlotte PowerShell Users Group has their meeting. We have a really cool presentation by Brian Hitney. We are going to attempt to do a live meeting so we can share the experience with our fellow Windows PowerShell friends.

RS, one thing to keep in mind when you use the Out-GridView cmdlet is that you need to send it usable information. This means staying away from Format* cmdlets such as Format-Table, Format-List, or Format-Wide. The reason for not using the Format* cmdlets is they change the object; therefore, the Out-GridView cmdlet does not know what to do with the incoming properties.

To provide the appropriate filtering information, the Out-GridView cmdlet reads the incoming property types. If those incoming property types change (which is the case when you pipe through a Format* type of cmdlet), Out-GridView does not know how to display the information. The following example illustrates this mutable property type situation. The results of the Get-Service cmdlet returns a ServiceController object. When the ServiceController object pipes to the Format-Table cmdlet, a series of formatting specific objects return instead.

Get-Service | Format-Table name, status | Get-Member

The command and its associated output appear in the image that follows.

Image of command output

The Out-GridView cmdlet does not know what to do with all the different Format* types of objects. If you need to select specific properties to display in the Out-GridView cmdlet, use the Select-Object cmdlet instead of a Format* type of cmdlet (such as Format-Table). In fact, the use of Format-Table and Select-Object are extremely similar. It is common to use Select-Object prior to sending data to Format-Table—especially if you want to limit the number of objects that are returned by a query. For example, the following command chooses the name and status properties from ServiceController objects, but it only returns the first five services.

PS C:\> Get-Service | sort name -Descending | Select-Object -First 5 -Property name,

status | Format-Table

If you leave the Format-Table cmdlet from the end of the command, you have the following command.

PS C:\> Get-Service | sort name -Descending | Select-Object -First 5 -Property name,

Status

If you run both commands and compare the output, you will see the output is amazingly similar. This is shown here.

Image of command output

In general, you can leave the Format-Table cmdlet out of such commands. This makes it easier for you to further process the information, such as sending the output to the Out-GridView cmdlet. For example, RS, to go back to your original problem, sending the first command (with the Format-Table) cmdlet in the command to the Out-GridView cmdlet produces an error. The error, a FormatException, is shown here.

Image of error message

Dropping the Format-Table cmdlet from the end of the command permits the command to work properly. The revised command is shown here.

PS C:\> Get-Service | sort name -Descending | Select-Object -First 5 -Property name,

status | Out-GridView

PS C:\>

The GridView control that is produced by the previous command is shown here.

Image of command output

One thing to keep in mind, is that the Out-GridView cmdlet accepts the format of the input object when creating the GridView control. This means that because the default view from Get-Service only displays three columns (the name, status, and display name), these are the only properties directly available to the GridView control if you pipe the output without any further manipulation.

The easy way to ensure the availability of all properties involves using the Select-Object cmdlet to pick up the additional properties, as shown here.

Get-Service | Select-Object -Property * | Out-GridView

If you do very much preprocessing prior to sending your data to the Out-GridView cmdlet, you may want to create a custom title for the GridView control. The reason for creating a custom title is that by default, the title is the query that creates the control. But if your query is very involved, complex, or confusing, the title will mimic this aspect of your code. A better way is to create a custom title that more directly answers the question, “Why did I create this control?” This technique is shown here.

PS C:\> Get-Service | Select-Object -Property * | Out-GridView -Title "All Service Properties"

The GridView control that displays all the service properties and has a custom title of All Service Properties is shown here.

Image of command output

Although it is certainly possible to use Criteria to filter out running from stopped services when you have produced the GridView control, if you know you only want running services, filter prior to sending the output to the Out-GridView cmdlet. This will make a cleaner way to deal with your data. This technique is shown here.

PS C:\> Get-Service | where { $_.status -eq 'running'} | select * | Out-GridView -Title 'running services'

The custom GridView control that displays all the properties of running services with the title of running services is shown here.

Image of command output

RS, that is all there is to using the Out-GridView cmdlet to display service information. Join me tomorrow when I will spend a bit more time talking about the Out-GridView cmdlet.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Hi Ed,

    I hope that Teresa and you could recreate yourselves after the games!

    It has been fun and I hope that all participators are back to normal now!!

    Even if we don't want to return to the GUI days, controls like the GridView

    are always a good thing to have ... it's close to the Excel view of data.

    I like the easy way to get to this presentation and just want to ask, if

    there could be a button "Export CSV/Excel" available ... or a "Print" button

    to persist the data on disk or paper.

    btw: A PRINT function for the ISE would be great!

    Klaus.

  • @K_Schulte what would you want to print from the ISE?

  • @K_Schulte Here is something that Jeff Hicks put together a while back to add a print menu in the ISE: jdhitsolutions.com/.../friday-fun-add-a-print-menu-to-the-powershell-ise  Not sure if it is quite what you are looking for though...

  • @K_Schulte My first search results (from that other search provider) was literally what I think you wanted!

    jdhitsolutions.com/.../friday-fun-add-a-print-menu-to-the-powershell-ise

    It's a nice site, i've visited there often for some rather clever hacks like this one.

  • @Boe, that's funny you beat me by a minute!

  • I just wrote this one up for my psie library. There has been a few occasions where it would have been nice to print the selected text, but since i've never had that option before i didnt. Not sure how that will look on post, but it's a nice little function

    Function Print-SelectedText

    {

       <#

           .SYNOPSIS

               Print text selected in the ISE

           .DESCRIPTION

               This simple function will send whatever text is currently

               selected in the PowerShell ISE to the printer.

           .PARAMETER InstallMenu

               If this switch is passed a new menu item will appear

               under Add-ons

           .EXAMPLE

               Print-SelectedText

               Description

               -----------

               The default syntax of the command

           .NOTES

               FunctionName : Print-SelectedText

               Created by   : jspatton

               Date Coded   : 05/03/2012 09:55:00

               The idea for this came from

               jdhitsolutions.com/.../friday-fun-add-a-print-menu-to-the-powershell-ise

           .LINK

               code.google.com/.../PSISELibrary

       #>

       [CmdletBinding()]

       Param

            (

            [switch]$InstallMenu

            )

       Begin

       {

           Write-Verbose "Check if we're in ISE"

           if ((Get-Host).Name -ne 'Windows PowerShell ISE Host')

           {

               Write-Verbose "Not in the ISE exiting."

               Return

               }

           }

       Process

       {

           switch ($InstallMenu)

           {

               $true

               {

                   $psISE.CurrentPowerShellTab.AddOnsMenu.submenus.Add("Print Selected",{Print-SelectedText},"CTRL+ALT+S") | Out-Null

                   }

               default

               {

                   $psISE.CurrentFile.Editor.SelectedText |Out-Printer

                   }

               }

           }

       End

       {

           }

       }

  • @Jeffrey, @Boe: Thank you!

    @JDH: Great! Thanks for sharing it!!

    Yes, I sometimes still use paper! Shame on me! :-)

    And printing out a large script is sometimes, at least to me, not the worst idea!

    A very nice script, Jeffery!

    I would just change the shortcut to "CTRL+P" *sss*

    And one issue, if I may say it, is: I can install the menu more than once, which might lead to some confusion ( and a deinstall switch might be as good )

    Sorry jeffery! You have been really nice and I'm the bad guy who wants more :-)))

    But that's the usual way things go: You give somebody your little finger and he takes the whole hand ( a german saying, literally translated )

    Thanks, Jeffrey, Jeff and Boe

    You are "a pal" !!!

    Klaus

  • THink about how much fun it would be to be able to add a custom right context menu to 'Out-Gridview'.