Coffee Break: Automate it!


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.

NAVDiag Log objects

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

Comments (13)

  1. Tobias Fenster says:

    Hello,

    I would like to use this not only on the server but also on the client to collect some information there as well. But if I just switch the dll to “client side” I get

    A call to Microsoft.Dynamics.Nav.PowerShellRunner.CreateInSandbox failed with this message: Could not load file or assembly ‘Microsoft.Dynamics.Nav.PowerShellRunner, Version=10.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35’ or one of its dependencies. Das System kann die angegebene Datei nicht finden.

    The Client should download the dll and I also tried to put it in the AddIn folder in the Client, so I guess it is not the dll itself but a dependency. Do you have any ideas what might be missing?

    Regards,
    Tobias

    1. Thanks for your comment Tobias! Excellent question. So this is something we have in pipeline for this coffee break series, to show how you can easily build up the sample provided to something you can use for both server and client diagnostic. So keep an eye on these coffee break posts, much more is coming, including running on client. But above all we want to hear from you and all your comments, examples, questions and ideas. So thanks again for this!

      But to answer your question: We use PSRunner addin here, and that one is installed only server side.
      However, copying it to Win Client add-ins folder should make it available client side too.
      A few notes: you will have to swtich all dotnet references to RunOnClient, naturally, and also make sure clients have Dynamics NAV Management module (or download it from server when importing).

      1. I was too quick, did not see your comment about the error. I had actually tried testing this by removing server side PowershellRunner folder, to make sure client side execution is done. I did not encounter errors, but my test was just a very simple quick version.
        Another post is comming up though and we will also try to include this scenario and description of requirements.

        1. Tobias Fenster says:

          I did some debugging and it fails in PSRunner.CreateInSandbox. I would investigate futher but I couldn’t find the C# source code. Am I missing something or is that code just not available?

          1. Hi Tobias,
            The only dependencies are standard libraries like mscorlib and system. You can view them using MS IL Disassembler from VS.

  2. Matthias König says:

    Hello,

    special thanks to this articles 🙂

    I tried to read the NAVServerLicenseInformation (Export-NAVServerLicenseInformation with the Microsoft.Dynamics.Nav.Model.Tools.psd1) but it failed with this exception “Access is denied. You need to be a member of the local Administrators group on the NAV Server to run this cmdlet”.

    My errorEnum was empty (the “while do loop” does not work) and in your example there are no commands which are using it.
    Could you add some of this examples?

    kind regards,
    Matthias König

    1. Matthias König says:

      or an example how to get an result without knowing the property? (like in Export-NAVServerLicenseInformation, its an String)

      1. Thank you for all the comments and suggestions. Along with previous input, this does give us some idea about usability and features we could add. The upcoming blog post will try to address some of the questions raised here. For now, you can always check the property name you want to retrieve by running commandlet in pipeline with get-member. That will list you available properties. For example:

        Get-NAVServerInstance -ServerInstance dynamicsnav100 | Get-Member -MemberType Property
        this will show that the list of available properties that yuo can retrieve from NAV. In case of export-navserverlicenseinformation, the resulting object is a String with Length as the only property . Our upcoming post will show how to read this using hashtables.
        Also it is worth noting, all NAV cmdlets require administrative rights. That is why we have tried retrieving details using other cmdlets, to enable diagnostic when running as NETWORK SERVICE. Exporting files from NAV is only done using native NAV cmdlets, so for now you can only retrieve license information if your service runs with account that has administrative rights.
        Thanks again for all the input, please keep them coming and watch out for the next post

          1. Matthias König says:

            hey, great! Thanks for that response 🙂

    2. Pallea says:

      Hi Matthias

      It works if you run powershell as administrator (You should allways run with admin-rights).

      Thanks
      Palle

  3. Pallea says:

    Hello Matthias

    You have to run the powershell script as “Run as Administrator” in order to get it to work. This is quite normal for all powershellscripts.

    Thanks
    Palle

    1. Matthias König says:

      Hi, I’ve tried that. But in my examples that does not work :/

Skip to main content