# writes the last error record to the OperationManager eventlog
function Log-ScriptError()
{
# LogScriptEvent see http://msdn.microsoft.com/en-us/library/bb437630.aspx
[int] $severity = 1
[int] $eventId = 2015
[string] $eventSourceName = 'Microsoft Dynamics NAV 2015 Management Pack'
# Import-Module or register Snap-in, that will enable side-by-side registrations of management dll
function RegisterSnapIn($snapIn)
{
$nstPath = "HKLM:\SOFTWARE\Microsoft\Microsoft Dynamics NAV\80\Service"
$managementDllPath = Join-Path (Get-ItemProperty -path $nstPath).Path '\Microsoft.Dynamics.Nav.Management.dll'
# First try to import the management module
Import-Module $managementDllPath -ErrorVariable errorVariable -ErrorAction SilentlyContinue
if (Check-ErrorVariable -eq $true)
{
# fallback to add the snap-in
if ((Get-PSSnapin -Name $snapIn -ErrorAction SilentlyContinue) -eq $null)
{
if ((Get-PSSnapin -Registered $snapIn) -eq $null)
{
throw "Could not locate the PSSnapin: $snapIn"
}
else
{
Add-PSSnapin $snapIn
}
}
}
}
# Check if there is any error in the ErrorVariable
function Check-ErrorVariable
{
return ($errorVariable -ne $null -and $errorVariable.Count -gt 0)
}
# WebServerInstanceDiscoveryScript.ps1
# Copyright (c) Microsoft Corporation. All rights reserved.
$scriptName = 'WebServerInstanceDiscoveryScript.ps1'
# Assign global variables to parameters passed in from the command line.
$target = $args[0]
$element = $args[1]
$principalName = $args[2]
# Register Microsoft Dynamics NAV Management Snap-in
RegisterSnapIn "Microsoft.Dynamics.Nav.Management"
# Create the discovery data structure that we will return the discovered objects in
$discData = $scomapi.CreateDiscoveryData(0, $element, $target)
# Get a list of all NAV Web Server instances on the machine
$foundWebServerInstances = Get-NavWebServerInstance
# Create SCOM objects for the NAV Web Server instances
foreach ($webServerInstance in $foundWebServerInstances)
{
$instanceName = $webServerInstance.WebServerInstance
# Create the WebServerInstance object and initialize it with its key properties, no matter what. If for some reason we can't get additional information from the server instance, then
# we still want the WebServerInstance object to exist, namely in order to give the opsmgr the information that a web server instance was indeed found.
$navWebServerInstance = $discData.CreateClassInstance("$MPElement[Name='Microsoft.Dynamics.Nav.2015.WebServerInstance']$")
$navWebServerInstance.AddProperty("$MPElement[Name='Windows!Microsoft.Windows.Computer']/PrincipalName$", $principalName)