AD Monitor Trusts Script Data Source

AD_Monitor_Trusts.DataSource.Addendum (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
LogSuccessEventstring$Config/LogSuccessEvent$
ExcludedTrustsListstring$Config/ExcludedTrustsList$Excluded Trusts List

Source Code:

<DataSourceModuleType ID="AD_Monitor_Trusts.DataSource.Addendum" 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"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="LogSuccessEvent" type="xsd:boolean"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="ExcludedTrustsList" type="xsd:string"/>
</Configuration>
<OverrideableParameters>
<OverrideableParameter ID="IntervalSeconds" Selector="$Config/IntervalSeconds$" ParameterType="int"/>
<OverrideableParameter ID="TimeoutSeconds" Selector="$Config/TimeoutSeconds$" ParameterType="int"/>
<OverrideableParameter ID="LogSuccessEvent" Selector="$Config/LogSuccessEvent$" ParameterType="string"/>
<OverrideableParameter ID="ExcludedTrustsList" Selector="$Config/ExcludedTrustsList$" ParameterType="string"/>
</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$ false $Config/LogSuccessEvent$ "$Config/ExcludedTrustsList$"</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 trusts for given computers.
'
' Assumptions - Script is run by a timed event
'
' Parameters - LogSuccessEvent - Indicates whether to log an event when
' the script completes successfully (that is without logging
' any errors)
'
' ExcludedTrustsList - Comma-separated list of trusts ti be excluded
'
' (c) Copyright 2001, Microsoft Corporation, All Rights Reserved
' Proprietary and confidential to Microsoft Corporation
'*************************************************************************

Option Explicit

'Event Constants
Const EVENT_TYPE_SUCCESS = 0
Const EVENT_TYPE_ERROR = 1
Const EVENT_TYPE_WARNING = 2
Const EVENT_TYPE_INFORMATION = 4

'Event IDs
Const EVENT_ID_SUCCESS = 99
Const EVENT_ID_SCRIPT_ERROR = 1000
Const EVENT_ID_EVENT_RULE_ONLY = 2
Const EVENT_ID_FAILED_TRUST = 83
Const EVENT_ID_SUCCEEDED_TRUST = 17
Const EVENT_ID_AGENTLESS = 98

' Other Constants
Const SCRIPT_NAME = "AD Monitor Trusts"

' Trust Enumeration Constants
Const ENUMERATE_ONLY = 0
Const ENUMERATE_WITH_SC_QUERY = 1
Const ENUMERATE_WITH_PASSWORD_CHECK = 2
Const ENUMERATE_WITH_SC_RESET = 3

' TypedPropertyBag
const PerformanceDataType = 2
const StateDataType = 3

' Other Variables
Dim oParams, TargetFQDNComputer, IsTargetAgentless, bIsRODC, sExcludedTrustsList
Set oParams = WScript.Arguments
if oParams.Count &lt;&gt; 4 then
Wscript.Quit -1
End if

TargetFQDNComputer = oParams(0)
bIsRODC = oParams(1)
sExcludedTrustsList = oParams(3)


IsTargetAgentless = False

Dim oAPI,oBag
Set oAPI = CreateObject("Mom.ScriptAPI")
Err.Clear

Sub Main()
On Error Resume Next

If Not(IsTargetAgentless) Then
Dim dtStart, bLogSuccess
dtStart = Now
bLogSuccess = CBool(oParams(2))

Dim strComputer
strComputer = TargetFQDNComputer

Dim oConfigTrustMon
Set oConfigTrustMon = GetObject("winmgmts:\\" &amp; strComputer &amp; "\root\MicrosoftActiveDirectory:Microsoft_TrustProvider=@")
If Err &lt;&gt; 0 Then
ScriptError " failed to get the TrustProvider configuration object." &amp; GetErrorString(Err)
Else
oConfigTrustMon.ReturnAll = True

Dim oAllTrusts, oTrust, strTrustErrors, bIsTrustExcluded
Set oAllTrusts = GetObject("winmgmts:\\" &amp; strComputer &amp; "\root\MicrosoftActiveDirectory").InstancesOf("Microsoft_DomainTrustStatus")
If 0 &lt;&gt; Err Then
bLogSuccess = False
ScriptError "failed to get all the trusts for this DC." &amp; GetErrorString(Err)
Else
For Each oTrust in oAllTrusts
bIsTrustExcluded = CheckExcludedTrusts(oTrust, sExcludedTrustsList)
If ((oTrust.TrustType = 1) Or (oTrust.TrustType = 2)) And (oTrust.TrustStatus &lt;&gt; 0) And ((oTrust.TrustStatus &lt;&gt; 1786) Or Not bIsRODC) And (Not bIsTrustExcluded) Then
strTrustErrors = strTrustErrors &amp; FormatTrust(oTrust) &amp; "." &amp; vbCrlF &amp; vbCrlF &amp; "The error is: " &amp; _
oTrust.TrustStatusString &amp; " (0x" &amp; Hex(oTrust.TrustStatus) &amp; ")" &amp; vbCrLf
End If
Next
End If

Err.Clear

Dim oLocalDomain, strLocalDomain
Set oLocalDomain = GetObject("winmgmts:\\" &amp; strComputer &amp; "\root\MicrosoftActiveDirectory:Microsoft_LocalDomainInfo=@")
If 0 = Err Then
strLocalDomain = oLocalDomain.DNSName
End If

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

Set oBag = oAPI.CreateTypedPropertyBag(StateDataType)
oBag.AddValue "State", "BAD"
oBag.AddValue "EventID", "" &amp; EVENT_ID_FAILED_TRUST
oBag.AddValue "ErrorString", strError
oAPI.AddItem oBag

CreateEvent EVENT_ID_FAILED_TRUST, EVENT_TYPE_WARNING, strError

bLogSuccess = False
Else
Set oBag = oAPI.CreateTypedPropertyBag(StateDataType)
oBag.AddValue "State", "GOOD"
oBag.AddValue "EventID", "" &amp; EVENT_ID_SUCCEEDED_TRUST
oAPI.AddItem oBag
End If

If bLogSuccess Then
CreateEvent EVENT_ID_SUCCEEDED_TRUST, EVENT_TYPE_INFORMATION, "The script '" &amp; SCRIPT_NAME &amp; "' completed successfully in "&amp; _
DateDiff("s", dtStart, Now) &amp; " seconds."
End If
End If
Else
CreateEvent EVENT_ID_AGENTLESS, EVENT_TYPE_ERROR, "The AD Management Pack does not support the agentless management mode." &amp; vbCrLf &amp; _
"The script '" &amp; SCRIPT_NAME &amp; "' will not execute." &amp; vbCrLf &amp; _
"To prevent this alert being generated again, either change the monitoring " &amp; _
"mode of the computer '" &amp; TargetFQDNComputer &amp; "' to agent-managed " &amp; _
"or disable the rule that generated this alert."

End If
oAPI.ReturnItems
'Else
' CreateEvent EVENT_ID_EVENT_RULE_ONLY, EVENT_TYPE_WARNING, "The script '" &amp; SCRIPT_NAME &amp; "' can only be executed by an event rule."
'End If
End Sub

'******************************************************************************
Sub CreateEvent(lngEventID, lngEventType, strMessage)
oAPI.LogScriptEvent "AD Monitor Trusts" ,lngEventID, lngEventType, strMessage
End Sub

'******************************************************************************
Sub ScriptError(strMessage)
'
' Purpose: To log a script error event
'
' Arguments: strMessage, the description of the error that occurred
'
CreateEvent EVENT_ID_SCRIPT_ERROR, EVENT_TYPE_WARNING, "The script '" &amp; SCRIPT_NAME &amp; "' " &amp; strMessage
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

'******************************************************************************
Function CheckExcludedTrusts(oTrust, sExcludedTrustsList)
'
' Purpose: Check if the trust is part of the exclusion list
'
' Parameters: oTrust, the trust to check
' sExcludedTrustsList, the list of excluded trusts
'
' Return: Boolean, true if trust is excluded
'
On Error Resume Next
CheckExcludedTrusts = False
If Len(sExcludedTrustsList) then
Dim strTrust, sExcludeList, sExcludeListItem, vRetValue
strTrust = oTrust.TrustedDomain
sExcludeList = Split(sExcludedTrustsList,",")

For Each sExcludeListItem in sExcludeList
vRetValue = StrComp(strTrust,sExcludeListItem,1)
If (vRetValue = 0) Then
CheckExcludedTrusts = True
Exit For
End If
Next
End If

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>