Microsoft Windows Server DNS Zone Level Policy Discovery

Microsoft.Windows.DNSServer.2016.Policy.ZoneLevel.Discovery (Discovery)

Discovers Zone Level Policies.

Knowledge Base article:

Summary

Discovers Zone Level Policies.

Element properties:

TargetMicrosoft.Windows.DNSServer.2016.Zone
EnabledTrue
Frequency14660
RemotableFalse

Object Discovery Details:

Discovered Classes and their attribuets:

Member Modules:

ID Module Type TypeId RunAs 
DS DataSource Microsoft.Windows.TimedPowerShell.DiscoveryProvider Default

Source Code:

<Discovery ID="Microsoft.Windows.DNSServer.2016.Policy.ZoneLevel.Discovery" Target="Microsoft.Windows.DNSServer.2016.Zone" Enabled="true" ConfirmDelivery="false" Remotable="true" Priority="Normal">
<Category>Discovery</Category>
<DiscoveryTypes>
<DiscoveryClass TypeID="Microsoft.Windows.DNSServer.2016.Policy.QueryResolution.ZoneLevel">
<Property TypeID="Microsoft.Windows.DNSServer.2016.Policy.ZoneLevel" PropertyID="PolicyName"/>
<Property TypeID="System!System.Entity" PropertyID="DisplayName"/>
</DiscoveryClass>
<DiscoveryClass TypeID="Microsoft.Windows.DNSServer.2016.Policy.ZoneTransfer.ZoneLevel">
<Property TypeID="Microsoft.Windows.DNSServer.2016.Policy.ZoneLevel" PropertyID="PolicyName"/>
<Property TypeID="System!System.Entity" PropertyID="DisplayName"/>
</DiscoveryClass>
</DiscoveryTypes>
<DataSource ID="DS" TypeID="Windows!Microsoft.Windows.TimedPowerShell.DiscoveryProvider">
<IntervalSeconds>14660</IntervalSeconds>
<SyncTime/>
<ScriptName>Microsoft.Windows.Server.10.0.ZoneLevelPolicy.Discovery.Script</ScriptName>
<ScriptBody><Script>

param ([String] $ElementID, [String] $TargetID, [String] $ServerName, [String] $ZoneName)

$SCRIPT_NAME = "DiscoverDNSZoneLevelPoliciesServer10.0"

$ErrorActionPreference = "Stop"

# Event type constants
$EVENT_TYPE_LOG = 0
$EVENT_TYPE_ERROR = 1
$EVENT_TYPE_WARNING = 2
$EVENT_TYPE_INFORMATION = 4

# Typed property bag constants
$PROPERTY_TYPE_ALERT = 0
$PROPERTY_TYPE_EVENT = 1
$PROPERTY_TYPE_PERFORMANCE = 2
$PROPERTY_TYPE_STATE = 3

# State type constants
$STATE_SUCCESS = "Success"
$STATE_WARNING = "Warning"
$STATE_ERROR = "Error"

$momAPI = new-object -comObject MOM.ScriptAPI

$DNS_NOT_RUNNING_EVENT_ID = 7654
$DNS_NOT_RUNNING_SCRIPT_MESSAGE = "DNS Server Service is not running. Exiting."

$ErrorInfo = 5704
$EventWarn = 5702
$EventError = 5702
$EventSuccess = 5700


function FuncCheckService{
param($ServiceName)
try
{
$arrService = Get-Service -Name $ServiceName
if ($arrService.Status -ne "running")
{
return $false
}
return $true
}
catch
{
return $false
}
}

Function Set-Error($momAPI,[String]$ErrorMessage,$EventLevel,$EventType,[String]$ScriptName)
{
if ($null -eq $momAPI)
{
return
}

try
{
if ($null -ne $momAPI)
{
$momAPI.LogScriptEvent($ScriptName,$EventLevel,$EventType,$ErrorMessage)
}
}
catch
{
}

}

Function Import-CmdLets ($momAPI,[string]$ScriptName)
{
try
{
$dnsmodule = Get-Module -Name "DnsServer"
if ($null -eq $dnsmodule)
{
Import-Module DnsServer
}
}
catch [System.IO.FileNotFoundException]
{
$ErrorMessage = "Dns cmdlets doesn't exist."
Set-Error -momAPI $momAPI -ScriptName $ScriptName -EventLevel $EventError -EventType $EVENT_TYPE_ERROR -ErrorMessage $ErrorMessage
exit
}
catch
{
$ErrorMessage = Get-ErrorMessage -Exception $_.Exception -ScriptName $ScriptName
Set-Error -momAPI $momAPI -ScriptName $ScriptName -EventLevel $EventError -EventType $EVENT_TYPE_ERROR -ErrorMessage $ErrorMessage
exit
}
}

Function Get-ErrorMessage($Exception,[string]$ScriptName)
{
$ErrorMes = $Exception.Message
$ErrorMessage = @"
Module: $ScriptName

Error(s) was(were) occurred:
Error(s):
$ErrorMes

"@

return $ErrorMessage
}

Function Process-DiscoveryFailure
{
$ErrorMessage = Get-ErrorMessage -Exception $_.Exception -ScriptName $Script:SCRIPT_NAME
Set-Error -momApi $Script:momApi -ScriptName $Script:SCRIPT_NAME -EventLevel $Script:EventError -EventType $Script:EVENT_TYPE_ERROR -ErrorMessage $ErrorMessage

$discoveryData = $Script:momApi.CreateDiscoveryData(0, $Script:ElementID, $Script:TargetID)
$discoveryData.IsSnapshot = $false
$discoveryData
}

Import-Cmdlets -momAPI $momAPI -ScriptName $SCRIPT_NAME



$Type1 = "$MPElement[Name='Microsoft.Windows.DNSServer.2016.Policy.QueryResolution.ZoneLevel']$"
$Type2 = "$MPElement[Name='Microsoft.Windows.DNSServer.2016.Policy.ZoneTransfer.ZoneLevel']$"
$CompNameProp = "$MPElement[Name='Windows!Microsoft.Windows.Computer']/PrincipalName$"
$DNSNameProp = "$MPElement[Name='Microsoft.Windows.DNSServer.2016.Server']/Name$"
$ZoneNameProp = "$MPElement[Name='Microsoft.Windows.DNSServer.2016.Zone']/ZoneName$"
$ZoneHostProp = "$MPElement[Name='Microsoft.Windows.DNSServer.2016.Zone']/HostName$"
$EntityDNProp = "$MPElement[Name='System!System.Entity']/DisplayName$"
$NameProp = "$MPElement[Name='Microsoft.Windows.DNSServer.2016.Policy.ZoneLevel']/PolicyName$"

#$msg = $SCRIPT_NAME + " for zone " + $ZoneName + " started"
#Write-EventLog -EventId 0 -LogName "Operations Manager" -Message $msg -Source HealthService -EntryType Warning

if (-Not (FuncCheckService "DNS"))
{
$momAPI.LogScriptEvent($SCRIPT_NAME, $DNS_NOT_RUNNING_EVENT_ID, $EVENT_TYPE_ERROR, $DNS_NOT_RUNNING_SCRIPT_MESSAGE)
return
}
Write-Host "$SCRIPT_NAME - Executing DNS 2016 Zone Level Policies Discovery Powershell Script"

#Write-EventLog -EventId 0 -LogName "Operations Manager" -Message "Zone: "$ZoneName -Source HealthService -EntryType Warning
#Write-EventLog -EventId 0 -LogName "Operations Manager" -Message $ZoneName": "$ElementID -Source HealthService -EntryType Warning
#Write-EventLog -EventId 0 -LogName "Operations Manager" -Message $ZoneName": "$TargetID -Source HealthService -EntryType Warning
#Write-EventLog -EventId 0 -LogName "Operations Manager" -Message $ZoneName": "$ServerName -Source HealthService -EntryType Warning


$discoveryData = $momAPI.CreateDiscoveryData(0, $ElementID, $TargetID)
$discoveryData.IsSnapshot = $true


try
{
$TargetObjects = Get-DnsServerQueryResolutionPolicy -ComputerName $ServerName -ZoneName $ZoneName

if($TargetObjects -ne $null)
{
if($TargetObjects.Count -eq $null) # if single object returned
{
$ObjCount = 1
}
else
{
$ObjCount = $TargetObjects.Count
}

for ($i=0; $i -lt $ObjCount; $i++)
{
if($ObjCount -eq 1)
{
$Obj = $TargetObjects
}
else
{
$Obj = $TargetObjects.Item($i)
}

$ScomInstance = $discoveryData.CreateClassInstance($Type1)
$ScomInstance.AddProperty($CompNameProp, $ServerName)
$ScomInstance.AddProperty($DNSNameProp, $ServerName)
$ScomInstance.AddProperty($ZoneNameProp, $ZoneName)
$ScomInstance.AddProperty($ZoneHostProp, $ServerName)
$ScomInstance.AddProperty($NameProp, $Obj.Name)
$ScomInstance.AddProperty($EntityDNProp, "Query resolution policy " + $Obj.Name + " on " + $ServerName)

#$msg = $Obj.Name
#Write-EventLog -EventId 0 -LogName "Operations Manager" -Message $msg -Source HealthService -EntryType Warning

$discoveryData.AddInstance($ScomInstance)

Write-Host "$SCRIPT_NAME - DNS 2016 Server Level Policies Added to Discovery data"

}

}


$TargetObjects = Get-DnsServerZoneTransferPolicy -ComputerName $ServerName -ZoneName $ZoneName

if($TargetObjects -ne $null)
{
if($TargetObjects.Count -eq $null) # if single object returned
{
$ObjCount = 1
}
else
{
$ObjCount = $TargetObjects.Count
}

for ($i=0; $i -lt $ObjCount; $i++)
{
if($ObjCount -eq 1)
{
$Obj = $TargetObjects
}
else
{
$Obj = $TargetObjects.Item($i)
}

$ScomInstance = $discoveryData.CreateClassInstance($Type2)
$ScomInstance.AddProperty($CompNameProp, $ServerName)
$ScomInstance.AddProperty($DNSNameProp, $ServerName)
$ScomInstance.AddProperty($ZoneNameProp, $ZoneName)
$ScomInstance.AddProperty($ZoneHostProp, $ServerName)
$ScomInstance.AddProperty($NameProp, $Obj.Name)
$ScomInstance.AddProperty($EntityDNProp, "Zone transfer policy " + $Obj.Name + " on " + $ServerName)

#$msg = $Obj.Name
#Write-EventLog -EventId 0 -LogName "Operations Manager" -Message $msg -Source HealthService -EntryType Warning

$discoveryData.AddInstance($ScomInstance)

Write-Host "$SCRIPT_NAME - DNS 2016 Zone Level Policies Added to Discovery data"

}

}


$discoveryData

Write-Host "$SCRIPT_NAME - Discovery data returned"
}
catch
{
Process-DiscoveryFailure
}

#$msg = $SCRIPT_NAME + " for zone " + $ZoneName + " finished"
#Write-EventLog -EventId 0 -LogName "Operations Manager" -Message $msg -Source HealthService -EntryType Warning

</Script></ScriptBody>
<Parameters>
<Parameter>
<Name>ElementID</Name>
<Value>$MPElement$</Value>
</Parameter>
<Parameter>
<Name>TargetID</Name>
<Value>$Target/Id$</Value>
</Parameter>
<Parameter>
<Name>ServerName</Name>
<Value>$Target/Host/Host/Property[Type='Windows!Microsoft.Windows.Computer']/PrincipalName$</Value>
</Parameter>
<Parameter>
<Name>ZoneName</Name>
<Value>$Target/Property[Type='Microsoft.Windows.DNSServer.2016.Zone']/ZoneName$</Value>
</Parameter>
</Parameters>
<TimeoutSeconds>300</TimeoutSeconds>
</DataSource>
</Discovery>