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:
$myxml.configuration.appSettings.add | 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!
This was originally posted here.
*This post is locked for comments