The Coffee Break Team is back, and this time we will unite the best of the two worlds and show how we can utilize the power of Windows PowerShell, using your favorite application: Dynamics NAV. While we do recognize that scripting complex PowerShell tools and modules is not to everyone’s taste, C/AL is something we all do in Dynamics NAV world. So let’s put them together and see how you can extend your application with PowerShell.
This time, the user scenario can be to automate diagnostics of your Dynamics NAV system and shorten the time and effort it takes to collect all needed details. In this first post we will look at how to use PowerShell to collect some basic diagnostic details. The attached objects consist of a PowerShell runner codeunit, to invoke and manage PowerShell sessions, another codeunit to specify the diagnostics you chose to run, a log table you can export, and two pages to bind them all.
The essence of invoking and running a PowerShell session is described in this blog post, and involves the PowerShell Runner Add-In (deployed with the product). This time we will go a step further and show how to collect the resulting output. Once you run the cmdlet, the result, in fact a collection of them, is read using a PS object ‘wrapper’ (also part of the PowerShell Runner Add-In). In its most basic form, this would read in this result:
ResultsEnumerator := PSRunner. Results.GetEnumerator; ReturnObject := ReturnObject.PSObjectAdapter; ReturnObject.PSObject := ResultsEnumerator.Current;
ReturnObject.GetProperty enables you to then query for specific properties.
The example we’re showing here (objects attached here: NAVDiag Log objects) does require a server user account to have administrative rights on the machine (to read service details), but it can be replaced by other queries/cmdlets that would not require administrative permissions. Reading event logs, for example, would not require elevated permissions.
The screenshot of the tool (below) needs little explanation. The Run Diagnostic button will collect the following details: services on the machine, their versions and running state. Then it will proceed to collect last 10 event logs generated by Dynamics NAV. The output log can be exported.
There is much more you can collect of course, and we intend to show some of it in these posts, but this will hopefully provide a good illustration of how to use PowerShell, in a relatively simple way, even if you are not much experienced in it.
The Coffee Break Team