# 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 = 2013
[string] $eventSourceName = 'Microsoft Dynamics NAV 2013 Management Pack'
# main()
[Boolean] $noDiscData = 1
try
{
# Create the discovery data structure that we will return the discovered objects in
$scomapi = new-object -comObject "MOM.ScriptAPI"
$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)
{
# 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.2013.WebServerInstance']$")
$navWebServerInstance.AddProperty("$MPElement[Name='Windows!Microsoft.Windows.Computer']/PrincipalName$", $principalName)
# add the WebServerInstance to the DiscoveryData object
$discData.AddInstance($navWebServerInstance)
$noDiscData = 0
}
$discData
}
catch
{
Generate-ErrorDetails "main"
Log-ScriptError
}
finally
{
# the last exception must be thrown to aleret the operator, but only if we don't have any discovery data to return.
if($noDiscData)
{
if ($Error.Count -gt 0)
{
throw $Error[0]
}
}
} </Script></ScriptBody>
<SnapIns>
<SnapIn>Microsoft.Dynamics.Nav.Management</SnapIn>
</SnapIns>
<Parameters>
<Parameter>
<Name>target</Name>
<Value>$Target/Id$</Value>
</Parameter>
<Parameter>
<Name>element</Name>
<Value>$MPElement$</Value>
</Parameter>
<Parameter>
<Name>principalName</Name>
<Value>$Target/Host/Property[Type="Windows!Microsoft.Windows.Computer"]/PrincipalName$</Value>
</Parameter>
</Parameters>
<TimeoutSeconds>300</TimeoutSeconds>
<StrictErrorHandling>true</StrictErrorHandling>
</DataSource>
</Discovery>