Microsoft Windows Server DNS 遞迴範圍探索
Microsoft.Windows.DNSServer.2016.RecursionScope.Discovery (Discovery)
探索遞迴範圍。
Knowledge Base article:
Element properties:
Object Discovery Details:
Member Modules:
Source Code:
<Discovery ID="Microsoft.Windows.DNSServer.2016.RecursionScope.Discovery" Target="Microsoft.Windows.DNSServer.2016.Server" Enabled="true" ConfirmDelivery="false" Remotable="true" Priority="Normal">
<Category>Discovery</Category>
<DiscoveryTypes>
<DiscoveryClass TypeID="Microsoft.Windows.DNSServer.2016.RecursionScope">
<Property TypeID="Microsoft.Windows.DNSServer.2016.RecursionScope" PropertyID="ScopeName"/>
<Property TypeID="System!System.Entity" PropertyID="DisplayName"/>
</DiscoveryClass>
</DiscoveryTypes>
<DataSource ID="DS" TypeID="Windows!Microsoft.Windows.TimedPowerShell.DiscoveryProvider">
<IntervalSeconds>14630</IntervalSeconds>
<SyncTime/>
<ScriptName>Microsoft.Windows.Server.10.0.RecursionScope.Discovery.Script</ScriptName>
<ScriptBody><Script>
param ([String] $ElementID, [String] $TargetID, [String] $ServerName)
$SCRIPT_NAME = "DiscoverDNSRecursionScopeServer10.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
$Type = "$MPElement[Name='Microsoft.Windows.DNSServer.2016.RecursionScope']$"
$CompNameProp = "$MPElement[Name='Windows!Microsoft.Windows.Computer']/PrincipalName$"
$DNSNameProp = "$MPElement[Name='Microsoft.Windows.DNSServer.2016.Server']/Name$"
$EntityDNProp = "$MPElement[Name='System!System.Entity']/DisplayName$"
$NameProp = "$MPElement[Name='Microsoft.Windows.DNSServer.2016.RecursionScope']/ScopeName$"
#Write-EventLog -EventId 0 -LogName "Operations Manager" -Message $SCRIPT_NAME+" started" -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 Recursion Scope Discovery Powershell Script"
#Write-EventLog -EventId 0 -LogName "Operations Manager" -Message $ElementID -Source HealthService -EntryType Warning
#Write-EventLog -EventId 0 -LogName "Operations Manager" -Message $TargetID -Source HealthService -EntryType Warning
#Write-EventLog -EventId 0 -LogName "Operations Manager" -Message $ServerName -Source HealthService -EntryType Warning
$discoveryData = $momAPI.CreateDiscoveryData(0, $ElementID, $TargetID)
$discoveryData.IsSnapshot = $true
try
{
$TargetObjects = Get-DnsServerRecursionScope -ComputerName $ServerName
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($Type)
$ScomInstance.AddProperty($CompNameProp, $ServerName)
$ScomInstance.AddProperty($DNSNameProp, $ServerName)
$ScomInstance.AddProperty($NameProp, $Obj.Name)
$ScomInstance.AddProperty($EntityDNProp, $Obj.Name + " on " + $ServerName)
$discoveryData.AddInstance($ScomInstance)
Write-Host "$SCRIPT_NAME - DNS 2016 Recursion Scope Added to Discovery data"
}
}
$discoveryData
Write-Host "$SCRIPT_NAME - Discovery data returned"
}
catch
{
Process-DiscoveryFailure
}
#Write-EventLog -EventId 0 -LogName "Operations Manager" -Message $SCRIPT_NAME+" finished" -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/Property[Type='Windows!Microsoft.Windows.Computer']/PrincipalName$</Value>
</Parameter>
</Parameters>
<TimeoutSeconds>300</TimeoutSeconds>
</DataSource>
</Discovery>