AD Execute Support Tool Script Datasource

AD_Execute_Support_Tool.DataSource (DataSourceModuleType)

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
CommandInitialDirectorystring$Config/CommandInitialDirectory$Command Initial Directory
ApplicationNamestring$Config/ApplicationName$Application Name
Parametersstring$Config/Parameters$Parameters
TimeoutSecondsint$Config/TimeoutSeconds$Timeout Seconds

Source Code:

<DataSourceModuleType ID="AD_Execute_Support_Tool.DataSource" Accessibility="Internal" Batching="false">
<Configuration>
<xsd:element name="IntervalSeconds" type="xsd:int"/>
<xsd:element name="CommandInitialDirectory" type="xsd:string"/>
<xsd:element name="ApplicationName" type="xsd:string"/>
<xsd:element name="Parameters" type="xsd:string"/>
<xsd:element name="TimeoutSeconds" type="xsd:int"/>
</Configuration>
<OverrideableParameters>
<OverrideableParameter ID="IntervalSeconds" Selector="$Config/IntervalSeconds$" ParameterType="int"/>
<OverrideableParameter ID="CommandInitialDirectory" Selector="$Config/CommandInitialDirectory$" ParameterType="string"/>
<OverrideableParameter ID="ApplicationName" Selector="$Config/ApplicationName$" ParameterType="string"/>
<OverrideableParameter ID="Parameters" Selector="$Config/Parameters$" ParameterType="string"/>
<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_Execute_Support_Tool.vbs$ $Config/CommandInitialDirectory$ $Config/ApplicationName$ $Config/Parameters$</CommandLine>
<TimeoutSeconds>$Config/TimeoutSeconds$</TimeoutSeconds>
<RequireOutput>true</RequireOutput>
<Files>
<File>
<Name>AD_Execute_Support_Tool.vbs</Name>
<Contents><Script>
'*************************************************************************
' Script Name - AD Execute Support Tool
'
' Purpose - Runs a command line task and puts the output into an event
'
' Parameters - ApplicationName - The name of the executable
' CommandInitialDirectory - the drive\path on the target
' machine where &lt;Application Name&gt; is located
' Parameters - the parameters that will be passed to
' &lt;Application Name&gt; as part of a task
'
'
' (c) Copyright 2008, 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 ID Constants
Const EVENT_ID_TASK_OUTPUT = 2000

' Other Constants
Const SCRIPT_NAME = "AD Execute Support Tool"
Const E_INVALIDARG = &amp;H80070057
Const E_FILENOTFOUND = &amp;H80070002
Const E_INVALIDPATH = &amp;H80070003

Const MAX_EVENT_TEXT_LENGTH = 200000

Class Error
Public Description
Public Number
Public Source

Sub Init(oErr)
Description = oErr.Description
Number = oErr.Number
Source = oErr.Source
End Sub

Sub Raise(strDescription)
Err.Raise Number, Source, strDescription &amp; GetErrorString(Number, Description)
End Sub
End Class

Dim oError
Set oError = new Error
On Error Resume Next

Dim oParams, strCommandInitialDirectory, strApplicationName, strParameters
' Other Variables
Set oParams = WScript.Arguments
if oParams.Count &lt; 3 then
Wscript.Quit -1
End if

strCommandInitialDirectory = oParams(0)
strApplicationName = oParams(1)
strParameters = oParams(2)
Err.Clear

DoExecuteTask
If Err.number &lt;&gt; 0 Then
CreateEvent EVENT_ID_TASK_OUTPUT, EVENT_TYPE_WARNING, "An error occurred while executing '" &amp; _
SCRIPT_NAME &amp; "'." &amp; vbCrLf &amp; "The error returned was: " &amp; GetErrorString(Err.number, Err.Description)
End If

'******************************************************************************
Sub DoExecuteTask
'
' Purpose: Executes the task
'
' Parameters: none
'
' Return: nothing
'
Dim strPath, strEXE, strCommandLineArgs
strPath = Trim(strCommandInitialDirectory)'ScriptContext.Parameters.Get("CommandInitialDirectory"))
strEXE = Trim(strApplicationName)'ScriptContext.Parameters.Get("ApplicationName"))
strCommandLineArgs = Trim(strParameters)'ScriptContext.Parameters.Get("Parameters"))

If Len(strEXE) = 0 Then
Err.Raise E_INVALIDARG, "ExeName", "One or more of the specified parameters is invalid. " &amp; _
"Please check the script parameters and try the task again."
End If

' Make sure we have a trailing \ in the path
If Len(strPath) &gt; 0 Then
If Right(strPath, 1) &lt;&gt; "\" Then
strPath = strPath &amp; "\"
End If
End If

Dim iExitCode

Dim oShell
Set oShell = CreateObject("WScript.Shell")
If Err = 0 Then
strPath = oShell.ExpandEnvironmentStrings(strPath)
Dim oExec, strMessage
On Error Resume Next
Set oExec = oShell.Exec(strPath &amp; strEXE &amp; " " &amp; strCommandLineArgs)
strMessage = strPath &amp; strEXE &amp; " " &amp; strCommandLineArgs &amp; vbCrLf &amp; vbCrLf
If Err.number = E_FILENOTFOUND Or Err.number = E_INVALIDPATH Then
strMessage = strEXE &amp; " " &amp; strCommandLineArgs &amp; vbCrLf &amp; vbCrLf
Set oExec = oShell.Exec(strEXE &amp; " " &amp; strCommandLineArgs)
If Err.number = E_FILENOTFOUND Or Err.number = E_INVALIDPATH Then
strMessage = "'" &amp; strEXE &amp; "' could not be found in the path "
If Len(strPath) &gt; 0 Then
strMessage = strMessage &amp; " or at '" &amp; strPath &amp; "' "
End If
strMessage = strMessage &amp; "on the target computer. Ensure that '" &amp; _
strExe &amp; "' is installed and/or update the " &amp; _
"""Command initial directory"" script parameter with the appropriate " &amp; _
"location of this utility on the target computer. " &amp; _
"'" &amp; strEXE &amp; "' is part of the Windows Server optional support " &amp; _
"utilities located on the Windows Server installation CD."
CreateEvent EVENT_ID_TASK_OUTPUT, EVENT_TYPE_WARNING, strMessage
WScript.Quit
ElseIf Err.number &lt;&gt; 0 Then
oError.Init Err
On Error Goto 0
oError.Raise "Failed to execute '" &amp; strEXE &amp; " " &amp; strCommandLineArgs &amp; "'."
End If
ElseIf Err.number &lt;&gt; 0 Then
oError.Init Err
On Error Goto 0
oError.Raise "Failed to execute '" &amp; strPath &amp; strEXE &amp; " " &amp; strCommandLineArgs &amp; "'."
End If

Dim iBlankLines
Do
Dim strLine
strLine = oExec.stdout.ReadLine()

If Len(strLine) = 0 Then
iBlankLines = iBlankLines + 1
Else
Do
If Asc(Right(strMessage, 1)) &lt;&gt; 13 Then
strMessage = strMessage &amp; vbCrLf
End If
iBlankLines = iBlankLines - 1
Loop While iBlankLines &gt; 0
strMessage = strMessage &amp; strLine
iBlankLines = 0
End If
If Len(strMessage) &gt; MAX_EVENT_TEXT_LENGTH Then
strMessage = strMessage &amp; vbCrLf &amp; vbCrLf &amp; _
"Output from this task is continued in the next event."
CreateEvent EVENT_ID_TASK_OUTPUT, EVENT_TYPE_INFORMATION, strMessage
strMessage = "Output continued from previous event." &amp; vbCrLf &amp; vbCrLf
End If
Loop While (oExec.Status = 0) Or (oExec.Stdout.AtEndOfStream = False)

iExitCode = oExec.ExitCode
End If

WScript.echo strMessage

If iExitCode = 0 Then
CreateEvent EVENT_ID_TASK_OUTPUT, EVENT_TYPE_INFORMATION, strMessage
Else
CreateEvent EVENT_ID_TASK_OUTPUT, EVENT_TYPE_WARNING, strMessage
End If
End Sub

'******************************************************************************
Sub CreateEvent(lngEventID, lngEventType, strMessage)
'
' Purpose: Creates a MOM event
'
' Parameters: lngEventID, the ID for the event
' lngEventType, the severity for the event. See constants at head of file
' strMessage, the message for the event
'
' Return: nothing
'
On Error Resume Next
oAPI.LogScriptEvent "AD Execute Support Tool", lngEventID, lngEventType, strMessage

End Sub

'******************************************************************************
Function GetErrorString(lErr, strErr)
'
' 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.)
'
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 = vbCrLf &amp; "'" &amp; strErr &amp; "' (0x" &amp; Hex(lErr) &amp; ")"
End Function

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