Get a specific Dynamics NAV Server Setting with PowerShell (a better Get-NAVServerConfiguration)

You might remember this blogpost: NAV 2013 R2: Powershell function to check if ServerInstance is Multitenant or not. It was a small tip on how to check a certain setting.

Recently I was working on a script, where I needed some other serversettings, and looking back on the way I was doing the above .. there had to be a better way. I mean .. looping all settings until I met the right one? Come on.. ;-).

Now, I don’t know if the following is “the best” method, but I find it at least somewhat better then above.

What is the problem?

Well, the output of the CmdLet “Get-NAVServerConfiguration” looks interesting and default, like:


It looks like the output is a number of objects with two properties: Key and value. But it isn’t. If you look at the members (with Get-Member), you actually have no decent properties at all :-/.

You might want to filter like this, but it doesn’t give you anything:

Get-NAVServerConfiguration -ServerInstance LIVE | where Key -eq “Multitenant”

There must be a reason for this. I can imagine in the background, it’s going to read the config.settings file in the server-folder.

There is a better way …

Now, you can look at it as xml as well, by using the “AsXML” parameter. When you do that, you can treat this output as xml, navigating the different nodes.. . For example, to fill an xml-variable with the output of this CmdLet, you can do like:

[xml]$myxml = Get-NAVServerConfiguration -ServerInstance LIVE -AsXml

And so you can look at it like xml .. not that I have too much experience in that 😉 .. but I noticed I could “browse” through the nodes by using intellisense. It didn’t take me much to figure out where everything was, quite an odd place, but hey .. Here, I found about the same output as the CmdLet itself:

$myxml.configuration.appSettings.add

But if you look at the member, you can actually see the “Key” and “Value” property:

$myxml.configuration.appSettings.add | Get-Member

So, I could definitely filter on this one with:

Get-NAVServerInstance | Get-NAVServerConfiguration2 | where Key -EQ “MultiTenant”

Knowing this, there are numerous ways to make it easy on you, and create a few new commandlets that can get you to the right properties easily. Below is an example on how to create your own “Get-NAVServerConfiguration”: 

function Get-NAVServerConfiguration2
{
    [CmdletBinding()]
    param (
        [parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true)]
        [String]$ServerInstance
    )
    BEGIN
    {
        $ResultObjectArray =  @()          
        
    }
    PROCESS
    {   
        $CurrentServerInstance = Get-NAVServerInstance -ServerInstance $ServerInstance
        $CurrentConfig = $CurrentServerInstance | Get-NAVServerConfiguration -AsXml
        
        foreach ($Setting in $CurrentConfig.configuration.appSettings.add)
        {
            $ResultObject = New-Object System.Object
            $ResultObject | Add-Member -type NoteProperty -name ServiceInstance -value $CurrentServerInstance.ServerInstance
            $ResultObject | Add-Member -type NoteProperty -name Key -value $Setting.Key
            $ResultObject | Add-Member -Type NoteProperty -Name Value -Value $Setting.Value
            $ResultObjectArray += $ResultObject
        }

    }
    END
    {
        $ResultObjectArray
    }
}

Now, you can do stuff like:
 Get-NAVServerInstance | Get-NAVServerConfiguration2 | where Key -EQ “MultiTenant”

To get a list of all the “MultiTenant” settings of all the instances on your machine. In my opinion: now the CmdLet behaves like it’s supposed to 😉
Enjoy!

3.00 avg. rating (64% score) - 6 votes

Permanent link to this article: https://www.waldo.be/2015/02/10/get-a-specific-dynamics-nav-server-setting-with-powershell-a-better-get-navserverconfiguration/

2 comments

2 pings

    • Peter on February 12, 2015 at 3:05 pm
    • Reply

    Hi,
    as you are a powerShell Expert, do you know how to fix the error “This database is registered with several NAV Server instances. You must choose an instance to use before performing this activity. Do you want to continue?” that occurs to me if I call

    “#Export-NAVApplicationObject -Path ‘MyPath.txt’ -DatabaseName ‘databasename’ -DataBaseServer ‘databaseServer\nav’ -ExportTxtSkipUnlicensed -Force ”

    Is there anyway of selecting the instance?

      • waldo on March 17, 2015 at 4:25 pm
        Author

      I’m afraid not. You should try to find out why it says that (it will give you the same when exporting manually, most probably). As far as I know, there is no way around in PowerShell – as this is no PowerShell issue, but a finsql-issue.. .

  1. […] are all aware that getting specific configuration properties isn’t as easy as it should be. I blogged about that here. Getting to specific settings in PowerShell should be easy. And what is easier then only having to […]

  2. […] are all aware that getting specific configuration properties isn’t as easy as it should be. I blogged about that here. Getting to specific settings in PowerShell should be easy. And what is easier then only having to […]

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.