Microsoft Dynamics NAV 2013 Website Discovery (IIS7)

Microsoft.Dynamics.Nav.2013.IIS7WebsiteDiscovery (Discovery)

This object discovery finds all Microsoft Dynamics NAV 2013 websites, hosted by IIS7.

Knowledge Base article:

Summary

This rule is run against all IIS 7 websites and discovers the subsets of websites on which Microsoft Dynamics NAV 2013 Web Server components are installed.

Element properties:

TargetMicrosoft.Windows.InternetInformationServices.2008.WebSite
EnabledTrue
Frequency14400
RemotableFalse

Object Discovery Details:

Discovered Classes and their attribuets:

Member Modules:

ID Module Type TypeId RunAs 
DS DataSource Microsoft.Windows.TimedPowerShell.DiscoveryProvider Microsoft.Dynamics.Nav.2013.DefaultAccount

Source Code:

<Discovery ID="Microsoft.Dynamics.Nav.2013.IIS7WebsiteDiscovery" Target="IIS7!Microsoft.Windows.InternetInformationServices.2008.WebSite" Enabled="true" ConfirmDelivery="false" Remotable="true" Priority="Normal">
<Category>Discovery</Category>
<DiscoveryTypes>
<DiscoveryClass TypeID="Microsoft.Dynamics.Nav.2013.IIS7.WebsiteConfiguration"/>
</DiscoveryTypes>
<DataSource ID="DS" TypeID="Windows!Microsoft.Windows.TimedPowerShell.DiscoveryProvider" RunAs="Microsoft.Dynamics.Nav.2013.DefaultAccount">
<IntervalSeconds>14400</IntervalSeconds>
<SyncTime/>
<ScriptName>WebSiteDiscoveryScript.ps1</ScriptName>
<ScriptBody><Script># WebSiteDiscoveryScript.ps1
# Copyright (c) Microsoft Corporation. All rights reserved.

param($target, $element, $principalName, $path, $siteId, $name, $iisVersion)

# Generates a well formatted error details message, that helps troubleshooting
function Generate-ErrorDetails([string] $functionName)
{
[string] $errorRecord = [string]::Format("{0}`n{1}`n{2}`n{3}`n",$Error[0].Exception.Message,$Error[0].InvocationInfo.Line,$Error[0].InvocationInfo.PositionMessage,$Error[0].Exception)
if($Error[0].Exception.InnerException -ne $null) { $errorRecord = $errorRecord + $Error[0].Exception.InnerException }
$errorRecord = "`nScript: WebSiteDiscoveryScript.ps1 `nFunctionName: " + $functionName + "`n`nError: " + $errorRecord
$Error[0].ErrorDetails = $errorRecord
}

# writes the last error record a error 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'

$scomapi.LogScriptEvent($eventSourceName, $eventId, $severity, $Error[0].ErrorDetails)
}

# main()
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)

# Discovery of websites, isn't completed by IIS discovery, so the Path field isn't filled out yet.
if([System.String]::IsNullOrEmpty($Path))
{
return
}

# Read the web.config file
$webconfigFilePath = Join-Path $Path "web.config"
# return if the web.config file doesn't exist
if (!(Test-Path -PathType Leaf $webconfigFilePath))
{
return
}

$xmlDoc = [xml] (Get-Content $webconfigFilePath)

# Get the NAV section.
$navSettingsNode = $xmlDoc.configuration.DynamicsNAVSettings
# return if there is no NAV section
if ($navSettingsNode -eq $null)
{
return
}

# Create the Nav-WebSite object and initialize it with its key properties, in order to give the opsmgr the information that a NAV website was detected.
$navWebSite = $null
if ($iisVersion -eq '7')
{
$navWebSite = $DiscData.CreateClassInstance("$MPElement[Name='Microsoft.Dynamics.Nav.2013.IIS7.WebsiteConfiguration']$")
}
elseif ($iisVersion -eq '8')
{
$navWebSite = $DiscData.CreateClassInstance("$MPElement[Name='Microsoft.Dynamics.Nav.2013.IIS8.WebsiteConfiguration']$")
}
else
{
# return if the iisVersion isn't supported
return
}

# fill out the key properties
$navWebSite.AddProperty("$MPElement[Name='IISCommon!Microsoft.Windows.InternetInformationServices.WebSite']/SiteID$", $siteID)
$navWebSite.AddProperty("$MPElement[Name='System!System.Entity']/DisplayName$", $name)
$navWebSite.AddProperty("$MPElement[Name='Windows!Microsoft.Windows.Computer']/PrincipalName$", $principalName)

# add the WebSite to the DiscoveryData object
$discData.AddInstance($navWebSite)
$discData
}
catch
{
Generate-ErrorDetails "main"
Log-ScriptError
}
finally
{
# the last exception must be thrown to aleret the operator.
if ($Error.Count -gt 0)
{
throw $Error[0]
}
}
</Script></ScriptBody>
<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/Host/Host/Property[Type="Windows!Microsoft.Windows.Computer"]/PrincipalName$</Value>
</Parameter>
<Parameter>
<Name>Path</Name>
<Value>$Target/Property[Type="IISCommon!Microsoft.Windows.InternetInformationServices.WebSite"]/Path$</Value>
</Parameter>
<Parameter>
<Name>SiteID</Name>
<Value>$Target/Property[Type="IISCommon!Microsoft.Windows.InternetInformationServices.WebSite"]/SiteID$</Value>
</Parameter>
<Parameter>
<Name>Name</Name>
<Value>$Target/Property[Type="System!System.Entity"]/DisplayName$</Value>
</Parameter>
<Parameter>
<Name>IISVersion</Name>
<Value>7</Value>
</Parameter>
</Parameters>
<TimeoutSeconds>300</TimeoutSeconds>
<StrictErrorHandling>true</StrictErrorHandling>
</DataSource>
</Discovery>