Get NAV Services Remotely with PowerShell

Get NAV Services Remotely with PowerShell

In your environment you may have multiple servers for different versions of NAV. Sometimes it can be difficult to maintain all the different servers and remove services that are not required or check on their configurations. I developed this PowerShell script as a tool to check the NAV instances. I hope you’ll find it useful. The script is available on GitHub along with two example (console output and excel output).

The story behind this is we wanted maintain service and database naming conventions, see if everyone is using the right parameters for the services and remove those that are not needed anymore.

I wanted to be able to execute this script from my laptop without logging into each server.

Here is an example to run the script:

. "C:\YourFolder\Get-NavServiceRemotely.ps1"

# My NAV 2017 Server
$Servers = ('NAV2017Server')
Get-NavServicesRemotely -NavVersion 100 -Severs $Servers | Format-Table -AutoSize

The script will create a remote session on NAV2017Server, it will import the Administration module from the program files folder and will use the Get-NAVServerInstance command to get the instances. It will collect the following parameters into an object:

  • Instance Name
  • Database Server
  • Database Instance
  • Database Name
  • Service Account
  • State

It will add each of these objects to a collection which will be returned.

The output of this example would look similar to this:

Get-NavServiceRemotely-TableOutput.jpg

If you execute it using multiple servers like this: $Servers = (‘NAV2017Server1’, ‘NAV2017Server2’) it will return all instances from all server together.

Excel Output

I thought it would be nice to create an excel workbook with a worksheet for each server with the instances. Here is an example for the excel file:

ExcelOutPut

Excel Output script

. "C:\YourFolder\Get-NavServicesRemotely.ps1"

$Servers = ('NAVSERVER2017', 'NAVSERVER2017UP1')
$NavServices = Get-NavServicesRemotely -NavVersion 90 -Severs $Servers

$Excel = New-Object -ComObject Excel.Application
$Excel.Visible = $true
$Workbook = $excel.Workbooks.Add()
$Sheets = $workbook.Worksheets

foreach ($Server in $Servers)
{
    $NavServicesFiltered = $NavServices | Where-Object {$_.PSComputerName -eq $Server}

    $CurrentWorkSheet = $Sheets.Add()
    $CurrentWorkSheet.Name = $Server

    $lineNo = 1

    # Create Headers
    $currentWorkSheet.Cells.Item($lineNo,1) = "Service Instance"
    $currentWorkSheet.Cells.Item($lineNo,2) = "Database Server"
    $currentWorkSheet.Cells.Item($lineNo,3) = "Database Instance"
    $currentWorkSheet.Cells.Item($lineNo,4) = "Database Name"
    $currentWorkSheet.Cells.Item($lineNo,5) = "Service Account"
    $currentWorkSheet.Cells.Item($lineNo,6) = "State"

    $format = $currentWorkSheet.UsedRange
    $format.Font.Bold = "True"

    foreach ($NavService in $NavServicesFiltered)
    {
        $lineNo = $lineNo + 1

        $currentWorkSheet.Cells.Item($lineNo,1) = $NavService.InstanceName
        $currentWorkSheet.Cells.Item($lineNo,2) = $NavService.DatabaseServer
        $currentWorkSheet.Cells.Item($lineNo,3) = $NavService.DatabaseInstance
        $currentWorkSheet.Cells.Item($lineNo,4) = $NavService.DatabaseName
        $currentWorkSheet.Cells.Item($lineNo,5) = $NavService.ServiceAccount
        $currentWorkSheet.Cells.Item($lineNo,6) = $NavService.State

    }
}
$Excel.Quit()

Get-NavServicesRemotely function

function Get-NavServicesRemotely
{
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory=$true)]
        [int] $NavVersion,
        [Parameter(Mandatory=$true)]
        [array] $Severs
    )

    Process
    {
        $Arguments = ($NavVersion)
        $ServiceConfigurations

        foreach($Server in $Servers)
        {
            Write-Host "Connecting To $($server)..." -ForegroundColor Cyan

            $LineNo = 1

            $RemoteSession = New-PSSession -ComputerName $Server -EnableNetworkAccess

            Invoke-Command -Session $RemoteSession -ArgumentList $Arguments `
            -ScriptBlock{
                Import-Module "c:\Program Files\Microsoft Dynamics NAV\$($args[0])\Service\NavAdminTool.ps1" | Out-Null
                $ServiceConfigurations = New-Object System.Collections.ArrayList

                Write-Host '..............................' -ForegroundColor Cyan
                Write-Host 'Retreiving services...' -ForegroundColor Cyan
                Write-Host '..............................' -ForegroundColor Cyan

                $Services = Get-NAVServerInstance
                foreach ($Service in $Services)
                {
                    $LineNo++

                    $NavServerInstance = $Service.ServerInstance
                    $NavServerInstance = $NavServerInstance.Replace('MicrosoftDynamicsNavServer$', '')

                    $ConfigObjectProperties = @{
                        "InstanceName" = ""
                        "DatabaseServer" = ""
                        "DatabaseInstance" = ""
                        "DatabaseName" = ""
                        "ServiceAccount" = ""
                        "State" = ""
                    }

                    $Config = New-Object -TypeName psobject -Property $ConfigObjectProperties

                    $Config.InstanceName = $NavServerInstance
                    $Config.ServiceAccount = $Service.ServiceAccount
                    $Config.State = $Service.State

                    $NavServiceConfig = Get-NavServerConfiguration $NavServerInstance

                    foreach ($Element in $NavServiceConfig)
                    {
                        if ($Element.key -eq "DatabaseServer")
                        {
                            $Config.DatabaseServer = $Element.value
                        }
                        if ($Element.key -eq "DatabaseInstance")
                        {
                            $Config.DatabaseInstance = $Element.value
                        }
                        if ($Element.key -eq "DatabaseName")
                        {
                            $Config.DatabaseName = $Element.value
                        }
                    }

                    $ServiceConfigurations.Add($Config) | Out-Null
                }
                return $ServiceConfigurations
            }
        }
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s