Coffee Break | More piping with Dynamics NAV

 Did you see our first coffee break about piping at Windows PowerShell and Piping? Let’s dig a bit further.

Coffee Break 6 – Return to piping

This time we will use more piping and other ways to look at a PowerShell object and format it in different ways. For the example here we will use Get-NAVServerInstance and the results from that cmdlet. But everything in this post would apply in the same way on other cmdlets, like

Get-NAVServerUser
Get-NAVWebService
Get-Process
Get-Service

 

Change how data is displayed

Import the NAV management module so we can use the NAV cmdlets

import-module ‘C:\Program Files\Microsoft Dynamics NAV\80\Service\Microsoft.Dynamics.Nav.Management.dll’

Run the following commands and check how the result differs:
Get-NAVServerInstance
Get-NAVServerInstance | Format-Table
Get-NAVServerInstance | Format-Table -AutoSize

 

Select parameters:

Select which columns to return

Get-NAVServerInstance | Select-Object ServerInstance, State

But… How do you know which columns are available? Simply pipe the cmdlet into Get-Member:

Get-NavServerInstance | Get-Member

This shows you a list of members, including these properties

Default                                                                                                      
DisplayName                                                                                                 
ServerInstance                                                                                              
ServiceAccount                                                                                            
State                                                                                                    
Version

Formatting Output

The most usual formats are list and table. Confusingly to a Dynamics NAV person, Format-List is like a card display, and Format-Table is just like a list. Run these to see the difference:
Get-NAVServerInstance | Select-Object ServerInstance, State | Format-List
Get-NAVServerInstance | Select-Object ServerInstance, State | Format-Table

Some of the most useful other formats (to replace the last bit of the pipe above):

Group-Object State
Sort-Object State
ConvertTo-Html
Export-Csv -Path c:\x\servers.txt
Out-gridview
Clip

Especially Clip is very useful – it sends the result directly to your clipboard so you can paste it into Notepad or somewhere else.

Note that formatting pipes may destroy the object in order to display it, so always do the formatting as the last part of a pipe. Except if you want to follow it by an Out-cmdlet.

 

Jasminka Thunes, Escalation Engineer Dynamics NAV EMEA

Lars Lohndorf-Larsen, Escalation Engineer Dynamics NAV EMEA

Bas Graaf, Senior Software Engineer Dynamics NAV