Out-Gridview – Powershell


Im not sure if there is a freeware or built-in log-viewer for the AX AOS trace logs, but never the less, I needed one today – and I thought; Why not just make one quick in Powershell and use GridView.

I personally think Out-GridView is way underrated (unused) so ill make a AX AOS log-viewer with GridView. (Plus its fun 🙂 )
It is particular great when you have some data, and you need to play around a bit – sorting, searching and what not. Instead for using programs like Excel – you can actually do this directly.

Lets have a look.

 

The Log-file.
Every “record” is a section of lines, separated by an empty line – like shown below:

Bruger-id: mm
Tidspunkt: 13:32:24 15-07-2014
Version: Microsoft Dynamics 4.0 (Build-nummer 2501.116)
Database: Microsoft SQL Server
Tidsregistrering: 63 ms med EXECUTE+FETCH (execute, first chunk of data)
SQL-sætning: SELECT TOP 1 A.BANK,A.STATEMENTNUM,A.NAME,A..........
Kaldestak:
(C)\Forms\AmcBankReconcileMainAccount\Data Sources\AmcBankMainAccount\Methods\dispLastUpdatedTime

Bruger-id: mm
Tidspunkt: 13:32:50 15-07-2014
Version: Microsoft Dynamics 4.0 (Build-nummer 2501.116)
Database: Microsoft SQL Server
Tidsregistrering: 94 ms med EXECUTE+FETCH (execute, first chunk of data)
SQL-sætning: SELECT SUM(A.AMOUNTRECONCILE) FROM ........
Kaldestak:
(C)\Forms\AmcBankReconcileTrans\Methods\countReconciledBalance

 

So, ill collect every line I find, until I meet an empty one.

Then every line is added to an object, and the object is finally added to an array of objects.
What I came up with – is this (I wont go too much into details about the code it self, its pretty self explaining 🙂 )

######################################################### Input parameters
Param ( 
    [Parameter(Mandatory=$true)] 
 [ValidateNotNullOrEmpty()]
        [String] 
        $LogFile
)
######################################################### Initialize
$Grid = @()
$N = 0
$Record = New-Object System.Object
$LF = get-content $Logfile
$LF | foreach 
 $Line = ($_).trim()
 
 if ($Line -eq "") {    # Empty line ? Ok - log entry has ended. Prepare start of new
  $Grid += $Record
  $Record = New-Object System.Object
  $N = 0
 }
 else {      # Lines with header and value, separated by the "first :"
  $N++
  $FirstColon = $Line.IndexOf(":")
  if ($FirstColon -gt 0) {
   $Logname = $Line.substring(0, $FirstColon)
   $LogVal = $Line.substring($FirstColon +1)
  }
  else {     # Lines without value
   $Logname = "Line $N"
   $LogVal = $Line
  }
  $Record | Add-member -type NoteProperty -Name $LogName -value $LogVal
 }
}
######################################################### Show it
$grid | Out-Gridview

Now to the part I really like to show. The Data-grid, or “GridView”.
The code above reads the log-file, and fill it into the $Grid array. Then we just feed the $Grid to “out-gridview” and magic happens 🙂

AXlogView

Add criterias and sort at will.

AXlogView2

 

If you want to try a simple and fast GridView example – try this:

Get-Service | Out-GridView

Nice right ? 🙂

(you might know this one – but ill through it in as a bonus 🙂  – want output directly into clipboard instead of “mark – paste – cleanup” ? )

Get-Service | clip

Go ahead – paste it to notepad 🙂

 

 

Leave a comment

Your email address will not be published. Required fields are marked *