AD Monitor Trusts Script Data Source

AD_Monitor_Trusts.DataSource (DataSourceModuleType)

Data Source for the AD Monitor Trusts monitors.

Element properties:

TypeDataSourceModuleType
IsolationAny
AccessibilityInternal
RunAsDefault
OutputTypeSystem.PropertyBagData

Member Modules:

ID Module Type TypeId RunAs 
DS DataSource System.CommandExecuterPropertyBagSource Default

Overrideable Parameters:

IDParameterTypeSelectorDisplay NameDescription
IntervalSecondsint$Config/IntervalSeconds$Interval Seconds
TimeoutSecondsint$Config/TimeoutSeconds$Timeout Seconds

Source Code:

<DataSourceModuleType ID="AD_Monitor_Trusts.DataSource" Accessibility="Internal" Batching="false">
<Configuration>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="IntervalSeconds" type="xsd:int"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="TargetComputerName" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="TimeoutSeconds" type="xsd:int"/>
</Configuration>
<OverrideableParameters>
<OverrideableParameter ID="IntervalSeconds" Selector="$Config/IntervalSeconds$" ParameterType="int"/>
<OverrideableParameter ID="TimeoutSeconds" Selector="$Config/TimeoutSeconds$" ParameterType="int"/>
</OverrideableParameters>
<ModuleImplementation Isolation="Any">
<Composite>
<MemberModules>
<DataSource ID="DS" TypeID="System!System.CommandExecuterPropertyBagSource">
<IntervalSeconds>$Config/IntervalSeconds$</IntervalSeconds>
<ApplicationName>%windir%\system32\cscript.exe</ApplicationName>
<WorkingDirectory/>
<CommandLine>//nologo $file/AD_Monitor_Trusts.vbs$ $Config/TargetComputerName$ $Target/Property[Type="AD2012R2Core!Microsoft.Windows.Server.2012.R2.AD.DomainControllerRole"]/IsRODC$</CommandLine>
<TimeoutSeconds>$Config/TimeoutSeconds$</TimeoutSeconds>
<RequireOutput>true</RequireOutput>
<Files>
<File>
<Name>AD_Monitor_Trusts.vbs</Name>
<Contents><Script>'*************************************************************************
' Script Name - AD Monitor Trusts
'
' Purpose - Checks the status of the AD trusts.
'
' (c) Copyright 2014, Microsoft Corporation, All Rights Reserved
' Proprietary and confidential to Microsoft Corporation
'*************************************************************************

Option Explicit

SetLocale("en-us")

On Error Resume Next

Sub Main()

Dim oParams, sTargetFQDNComputer, bIsRODC, oAPI, oBag, sError, oConfigTrustMon

Set oParams = WScript.Arguments
Set oAPI = CreateObject("Mom.ScriptAPI")
Set oBag = oAPI.CreatePropertyBag()

if oParams.Count &lt;&gt; 2 then
sError = "The number of command line arguments is incorrect: " &amp; vbCrLf &amp; _
"Expected: 2" &amp; vbCrLf &amp; _
"Actual: " &amp; oParams.Count

oBag.AddValue "State", "BAD"
oBag.AddValue "ErrorString", sError

Call oAPI.Return(oBag)
Exit Sub
End if

sTargetFQDNComputer = oParams(0)
bIsRODC = oParams(1)

Err.Clear

Set oConfigTrustMon = GetObject("winmgmts:\\" &amp; sTargetFQDNComputer &amp; "\root\MicrosoftActiveDirectory:Microsoft_TrustProvider=@")

If Err &lt;&gt; 0 Then
sError = "Failed to get the TrustProvider configuration object: " &amp; vbCrLf &amp; _
"Error: " &amp; GetErrorString(Err)

oBag.AddValue "State", "BAD"
oBag.AddValue "ErrorString", sError

Call oAPI.Return(oBag)
Exit Sub
Else
Dim oAllTrusts, oTrust, strTrustErrors, oLocalDomain

oConfigTrustMon.ReturnAll = True

Set oAllTrusts = GetObject("winmgmts:\\" &amp; sTargetFQDNComputer &amp; "\root\MicrosoftActiveDirectory").InstancesOf("Microsoft_DomainTrustStatus")
If 0 &lt;&gt; Err Then
sError = "Failed to get the domain trusts for this DC: " &amp; vbCrLf &amp; _
"Error: " &amp; GetErrorString(Err)

oBag.AddValue "State", "BAD"
oBag.AddValue "ErrorString", sError

Call oAPI.Return(oBag)
Exit Sub
Else
For Each oTrust in oAllTrusts
If ((oTrust.TrustType = 1) Or (oTrust.TrustType = 2)) And (oTrust.TrustStatus &lt;&gt; 0) And ((oTrust.TrustStatus &lt;&gt; 1786) Or Not bIsRODC) Then
strTrustErrors = strTrustErrors &amp; "Trust: " &amp; FormatTrust(oTrust) &amp; vbCrlF &amp; _
"Error: " &amp; oTrust.TrustStatusString &amp; " (0x" &amp; Hex(oTrust.TrustStatus) &amp; ")" &amp; vbCrLf &amp; vbCrLf
End If
Next
End If

Err.Clear

Set oLocalDomain = GetObject("winmgmts:\\" &amp; sTargetFQDNComputer &amp; "\root\MicrosoftActiveDirectory:Microsoft_LocalDomainInfo=@")

If Len(strTrustErrors) &gt; 0 Then
Dim strError
strError = "The trusts between this domain (" &amp; oLocalDomain.DNSName &amp; ") and the following domain(s) are in an error state: " &amp; strTrustErrors

oBag.AddValue "State", "BAD"
oBag.AddValue "ErrorString", strError
oAPI.AddItem oBag
Else
oBag.AddValue "State", "GOOD"
End If
End If

Call oAPI.Return(oBag)

End Sub


'******************************************************************************
Function GetErrorString(oErr)
'
' Purpose: Attempts to find the description for an error if an error with
' no description is passed in.
'
' Parameters: oErr, the error object
'
' Return: String, the description for the error. (Includes the error code.)
'
Dim lErr, strErr
lErr = oErr
strErr = oErr.Description

On Error Resume Next
If 0 &gt;= Len(strErr) Then
' If we don't have an error description, then check to see if the error
' is a 0x8007xxxx error. If it is, then look it up.
Const ErrorMask = &amp;HFFFF0000
Const HiWord8007 = &amp;H80070000
Const LoWordMask = 65535 ' This is equivalent to 0x0000FFFF

If (lErr And ErrorMask) = HiWord8007 Then
' Attempt to use 'net helpmsg' to get a description for the error.
Dim oShell
Set oShell = CreateObject("WScript.Shell")
If Err = 0 Then
Dim oExec
Set oExec = oShell.Exec("net helpmsg " &amp; (lErr And LoWordMask))

Dim strMessage, i
Do
strMessage = oExec.stdout.ReadLine()
i = i + 1
Loop While (Len(strMessage) = 0) And (i &lt; 5)

strErr = strMessage
End If
End If
End If

GetErrorString = "The error returned was: '" &amp; strErr &amp; "' (0x" &amp; Hex(lErr) &amp; ")"
End Function

'******************************************************************************
Function FormatTrust(oTrust)
'
' Purpose: Formats a trust in a readable manner.
'
' Parameters: oTrust, the trust to format
'
' Return: String, the description of the trust
'
On Error Resume Next
Dim strTrust
strTrust = oTrust.TrustedDomain
If oTrust.TrustDirection = TRUST_DIR_INBOUND Then
strTrust = strTrust &amp; " (inbound)"
ElseIf oTrust.TrustDirection = TRUST_DIR_OUTBOUND Then
strTrust = strTrust &amp; " (outbound)"
ElseIf oTrust.TrustDirection = TRUST_DIR_BIDIRECTIONAL Then
strTrust = strTrust &amp; " (bidirectional)"
End If

FormatTrust = strTrust
End Function

Call Main()
</Script></Contents>
<Unicode>1</Unicode>
</File>
</Files>
</DataSource>
</MemberModules>
<Composition>
<Node ID="DS"/>
</Composition>
</Composite>
</ModuleImplementation>
<OutputType>System!System.PropertyBagData</OutputType>
</DataSourceModuleType>