Tip of the Week #61 – EVENTS!

PrintVis Tip of the Week #61 – EVENTS!

This week Senior Developer Peter Tijsma brings us another commendable complex of colorful code.

“YAPS”  Yet Another PowerShell Script 🙂

This time a ‘somewhat’ larger script, but always nice-to-have.

You know that since NAV 2016 has been released, there’s a lot to do about ‘Events’.

Huh…. Events? YES Events!

Well, the problem here is that standard NAV has a nice page which shows you all the Event Subscribers (code listening to events being fired) active in your code, but it doesn’t show you the Event Publishers (The actual events being fired).

This script will help you localize the events in an exported NAV Object Text file and on request show them in a nice grid-view. The standard output of the script is a csv text array so you could also simply save it to a csv file and open it in Excel to do further analysis.

The script takes two arguments:

  • InputObjectFilename
    Specify the location of the NAV Object Text file
  • ShowGridView
    Switch to tell the script to present the output in a nice grid-view

Example on how to use the script:

.’E:\TipOfTheWeek\Get-PVEventsFromSource.ps1′

$InputObjectFileName = “E:\TipOfTheWeek\SomeNAVObjects.txt”

$OutputCsvFileName = “E:\TipOfTheWeek\SomeNAVObjects-Events.csv”

$results = Get-PVEventsFromSource -InputObjectFileName $InputObjectFileName -ShowGridView

Set-Content -Path $OutputCsvFileName -Value $results -Force

And…the actual script:

function Get-PVEventsFromSource
{
[
CmdletBinding()]
param (
[
parameter(Mandatory=$true)]
[
string]$InputObjectFileName,
[
parameter(Mandatory=$false)]
[
switch]$ShowGridView
)
PROCESS
{
Write-Progress -Activity “Caching objects file…” -Status “Reading file $InputObjectFileName”

$FileName = [System.IO.Path]::GetFileName($InputObjectFileName)
$DistriText = [System.IO.File]::ReadAllLines($InputObjectFileName)
$TotalLines = $DistriText.Length

$ResultArray = @()
$CurrentObject = 
$i = 0

foreach ($TextLine in $DistriText) {
$i++

switch ($true)
{

{$Textline.Contains(“OBJECT “)}  {$CurrentObject = $TextLine.TrimStart()}
{$Textline.Contains(” [Integration”)} {$CurrentEventType = $TextLine.TrimStart()}
{$Textline.Contains(” [Business”)} {$CurrentEventType = $TextLine.TrimStart()}
{$Textline.Contains(” [TryFunction”)} {$CurrentEventType = $TextLine.TrimStart()}

{$Textline.Contains(” PROCEDURE”) -and !([String]::IsNullOrEmpty($CurrentEventType))} {
$ObjectArray = $CurrentObject.Split(” “)
$ObjectType = $ObjectArray[1]
$ObjectID = $ObjectArray[2]
$ObjectName = $ObjectArray[3..99] -join ” “

$CurrentFunction = $TextLine.TrimStart()
$pos = $CurrentFunction.IndexOf(“PROCEDURE “)
$CurrentFunction = $CurrentFunction.Substring($pos + 10).TrimEnd(“;”)

$CurrentEventType = $CurrentEventType.Trim(“[] “)

$MyObject = New-Object System.Object
$MyObject | Add-Member -MemberType NoteProperty -Name ObjectType -Value $ObjectType
$MyObject | Add-Member -MemberType NoteProperty -Name ObjectID -Value $ObjectID
$MyObject | Add-Member -MemberType NoteProperty -Name ObjectName -Value $ObjectName
$MyObject | Add-Member -MemberType NoteProperty -Name EventType -Value $CurrentEventType
$MyObject | Add-Member -MemberType NoteProperty -Name Function -Value $CurrentFunction
$ResultArray += $MyObject

$CurrentEventType = 
}

Default {}
}
if ($i % 10 -eq 0) {
Write-Progress -Activity “Reading through objects..” -Status $CurrentObject -PercentComplete ([Math]::Round($i / $TotalLines * 100))
}

}

if ($ResultArray.Count -eq 0) {
Write-Warning “No events found in $FileName!”
return
}

Write-Progress -Activity “Compiling Result Array…”
$result = @()

$result += $([string]::Format(‘”{0}”;”{1}”;”{2}”;”{3}”;”{4}”‘, “Object Type”,”Object ID”,”Object Name”,”Event Type”,”Function”))
$i = 0
$TotalLines = $ResultArray.Count
foreach ($object in $ResultArray) {
$i ++
Write-Progress -Activity “Compiling Result Array…” -PercentComplete ([Math]::Round($i / $TotalLines * 100))
$result += $([string]::Format(‘”{0}”;{1};”{2}”;”{3}”;”{4}”‘, $object.ObjectType, $object.ObjectID, $object.ObjectName, $object.EventType, $object.Function))
}

if ($ShowGridView) {
$ResultArray | Out-GridView -Title “All Events in $FileName
}

return $result
}
}

Example output:

Have fun finding all the Events we’ve created in the next version of PrintVis (Codename: PrintVis Madeira) when it’s released!

Thank you Peter!