<DataSourceModuleType ID="Microsoft.Windows.Server.2012.R2.AD.LostObjectCount.DataSource" Accessibility="Internal" Batching="false">
<Configuration>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="Frequency" type="xsd:int"/>
<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="Threshold" type="xsd:int"/>
</Configuration>
<OverrideableParameters>
<OverrideableParameter ID="Frequency" Selector="$Config/Frequency$" ParameterType="int"/>
<OverrideableParameter ID="TimeoutSeconds" Selector="$Config/TimeoutSeconds$" ParameterType="int"/>
<OverrideableParameter ID="Threshold" Selector="$Config/Threshold$" ParameterType="int"/>
</OverrideableParameters>
<ModuleImplementation Isolation="Any">
<Composite>
<MemberModules>
<DataSource ID="DS" TypeID="System!System.CommandExecuterPropertyBagSource">
<IntervalSeconds>$Config/Frequency$</IntervalSeconds>
<ApplicationName>%windir%\system32\cscript.exe</ApplicationName>
<WorkingDirectory/>
<CommandLine>//nologo $file/AD_Lost_And_Found_Object_Count.vbs$ $Config/Threshold$</CommandLine>
<TimeoutSeconds>$Config/TimeoutSeconds$</TimeoutSeconds>
<RequireOutput>true</RequireOutput>
<Files>
<File>
<Name>AD_Lost_And_Found_Object_Count.vbs</Name>
<Contents><Script>'*************************************************************************
' Script Name - AD Lost And Found Object Count
'
' Purpose - Monitors the number of objects in lost and found container
'
' (c) Copyright 2014, Microsoft Corporation, All Rights Reserved
' Proprietary and confidential to Microsoft Corporation
'*************************************************************************
Option Explicit
SetLocale("en-us")
Sub Main()
Dim oParams, oAPI, oBag, sError, iThreshold, oADO, oCmd, oRoot, strDNSDomain, oRecords
Set oParams = WScript.Arguments
Set oAPI = CreateObject("Mom.ScriptAPI")
Set oBag = oAPI.CreatePropertyBag()
if oParams.Count <> 1 then
sError = "The number of command line arguments is incorrect: " & vbCrLf & _
"Expected: 1" & vbCrLf & _
"Actual: " & oParams.Count
Set oADO = CreateObject("ADODB.Connection")
If Err Then
oBag.AddValue "State", "BAD"
oBag.AddValue "ErrorString", "Unable to create ADODB Connection." & GetErrorString(Err)
Call oAPI.Return(oBag)
Exit Sub
End IF
Set oCmd = CreateObject("ADODB.Command")
If Err Then
oBag.AddValue "State", "BAD"
oBag.AddValue "ErrorString", "Unable to create ADODB Command." & GetErrorString(Err)
set oRoot = GetObject("LDAP://RootDSE")
If Err Then
oBag.AddValue "State", "BAD"
oBag.AddValue "ErrorString", "Unable to contact Root DSE." & GetErrorString(Err)
Call oAPI.Return(oBag)
Exit Sub
End If
strDNSDomain = oRoot.Get("defaultNamingContext")
If Err Then
oBag.AddValue "State", "BAD"
oBag.AddValue "ErrorString", "Unable to connect to the default naming context." & GetErrorString(Err)
Call oAPI.Return(oBag)
Exit Sub
End If
oCmd.CommandText = "Select Name From 'LDAP://CN=LostAndFound," & strDNSDomain & "'"
set oRecords = oCmd.Execute
If Err Then
oBag.AddValue "State", "BAD"
oBag.AddValue "ErrorString", "Unable to query the LostAndFound container to obtain object count." & vbCrLf & _
"Query: " & oCmd.CommandText & GetErrorString(Err)
Call oAPI.Return(oBag)
Exit Sub
End If
if (iThreshold > (oRecords.RecordCount - 1)) Then
oBag.AddValue "State", "GOOD"
Else
sError = "The number of objects in the Lost and Found container is above the configured threshold:" & vbCrLf & _
"Threshold: " & iThreshold & vbCrLf & _
"Objects: " & (oRecords.RecordCount - 1)
oBag.AddValue "State", "BAD"
oBag.AddValue "ErrorString", sError
End If
Call oAPI.Return(oBag)
End Sub
'******************************************************************************
' Name: GetErrorString
'
' 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.)
'
Function GetErrorString(oErr)
Dim lErr, strErr
lErr = oErr
strErr = oErr.Description
On Error Resume Next
If 0 >= 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 = &HFFFF0000
Const HiWord8007 = &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 " & (lErr And LoWordMask))
Dim strMessage, i
Do
strMessage = oExec.stdout.ReadLine()
i = i + 1
Loop While (Len(strMessage) = 0) And (i < 5)
strErr = strMessage
End If
End If
End If
GetErrorString = vbCrLf & vbCrLf & "The error returned was: '" & strErr & "' (0x" & Hex(lErr) & ")"
End Function