EDC.Base.AsyncCMD.WriteAction (WriteActionModuleType)

Element properties:

TypeWriteActionModuleType
IsolationAny
AccessibilityPublic
RunAsDefault
InputTypeSystem.BaseData
OutputTypeSystem.CommandOutput

Member Modules:

ID Module Type TypeId RunAs 
WA1 WriteAction System.CommandExecuter Default

Overrideable Parameters:

IDParameterTypeSelector
Argumentsstring$Config/Arguments$

Source Code:

<WriteActionModuleType ID="EDC.Base.AsyncCMD.WriteAction" Accessibility="Public" Batching="false">
<Configuration>
<IncludeSchemaTypes>
<SchemaType>System!System.ParamListSchema</SchemaType>
</IncludeSchemaTypes>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="CMDName" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="Arguments" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="CMDBody" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="SecureInput" minOccurs="0" maxOccurs="1">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="256"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="TimeoutSeconds" type="xsd:integer"/>
</Configuration>
<OverrideableParameters>
<OverrideableParameter ID="Arguments" Selector="$Config/Arguments$" ParameterType="string"/>
</OverrideableParameters>
<ModuleImplementation Isolation="Any">
<Composite>
<MemberModules>
<WriteAction ID="WA1" TypeID="System!System.CommandExecuter">
<ApplicationName>%windir%\system32\cscript.exe</ApplicationName>
<WorkingDirectory/>
<CommandLine>/nologo StartCMDAsync.vbs "$Config/CMDName$" $Config/Arguments$</CommandLine>
<SecureInput>$Config/SecureInput$</SecureInput>
<TimeoutSeconds>$Config/TimeoutSeconds$</TimeoutSeconds>
<RequireOutput>true</RequireOutput>
<Files>
<File>
<Name>StartCMDAsync.vbs</Name>
<Contents><Script>
Option Explicit
'************************************************************************************************************
' Disclaimer
'
' This sample script is not supported under any Microsoft standard support program or service. This sample
' script is provided AS IS without warranty of any kind. Microsoft further disclaims all implied warranties
' including, without limitation, any implied warranties of merchantability or of fitness for a particular
' purpose. The entire risk arising out of the use or performance of this sample script and documentation
' remains with you. In no event shall Microsoft, its authors, or anyone else involved in the creation,
' production, or delivery of this script be liable for any damages whatsoever (including, without limitation,
' damages for loss of business profits, business interruption, loss of business information, or other
' pecuniary loss) arising out of the use of or inability to use this sample script or documentation, even
' if Microsoft has been advised of the possibility of such damages.
'
'***********************************************************************************************************
'* StartCMDAsync.vbs v1.0 Version 20081201
'*
'* Starts a given batch with the given arguments asynchronously. Because of limitations of cmd.exe
'* the arguments cannot use quotes!
'*
'* Version for Operations Manager 2007
'*
'* JM
'******************************************************************************************************
'Const SCRIPT_NAME = "StartCMDAsync.vbs"
Const SCRIPT_VERSION = "1.0"

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

Const NEW_WORK_DIR_CMD_LINE_PARAMETER_PREFIX = "-StartCMDAsyncNewWorkDir:"

Const USE_WMI_CMD_LINE_PARAMETER_PREFIX = "-StartCMDAsyncUseWMI"
Const SW_HIDE = 0
Const CREATE_BREAKAWAY_FROM_JOB = &amp;H1000000


Const EVENT_ID_SUCCESS = 1000 'Use IDs in the range 1 - 1000
Const EVENT_ID_SCRIPTERROR = 999 'Then you can use eventcreate.exe to test the MP

Call Main

'********************************************************************************
' PROCEDURE: Main
' DESCRIPTION: Called on script start. Read the script's cmd line arguments
' and build the cmd line that shall be executed asynchronously.
' The first argument must be the full name of the script which
' shall be started asynchronously.
' PARAMETERS: -
'******************************************************************************
Private Sub Main()

Dim objMomScriptAPI 'As MOM.ScriptAPI
Dim objArguments 'As WScript.Arguments
Dim strCmdLine 'As String
Dim strNewWorkDir 'As String
Dim strScriptName 'As String
Dim objFSO 'As Scripting.FileSystemObject
Dim objWSHShell 'As WScript.Shell
Dim i 'As Long
Dim blnUseWMI 'As Boolean

Call SetLocale("en-us")
Set objMomScriptAPI = CreateObject("MOM.ScriptAPI")
Set objWSHShell = CreateObject("WScript.Shell")

'Get the arguments
Set objArguments = WScript.Arguments

'If there is at least one argument build the command line.
'It is assumed that the first argument is the full name of the script
'which shall be started asynchronously.
If objArguments.Count &gt; 0 Then

'The script name must be the first argument
strScriptName = objWSHShell.ExpandEnvironmentStrings(objArguments(0))

'Build the cmd line
If objArguments.Count &gt; 1 Then
For i = 1 To objArguments.Count - 1
'If a path is given the script will be copied to that location
If InStr(objArguments(i), NEW_WORK_DIR_CMD_LINE_PARAMETER_PREFIX) &gt; 0 Then
strNewWorkDir = Replace(objArguments(i), NEW_WORK_DIR_CMD_LINE_PARAMETER_PREFIX, "")
ElseIf InStr(objArguments(i), USE_WMI_CMD_LINE_PARAMETER_PREFIX) &gt; 0 Then
blnUseWMI = True
Else
strCmdLine = strCmdLine &amp; Chr(32) &amp; objArguments(i)
End If
Next
End If

On Error Resume Next

'If the a new path is given copy the script to that location
If Len(strNewWorkDir) &gt; 0 Then
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists(strNewWorkDir) And _
objFSO.FileExists(strScriptName) Then
Call objFSO.CopyFile(strScriptName, strNewWorkDir &amp; "\" &amp; strScriptName, True)
If Err.Number &lt;&gt; 0 Then
Call objMomScriptAPI.LogScriptEvent(WScript.ScriptName &amp; " " &amp; SCRIPT_VERSION, EVENT_ID_SCRIPTERROR, EVENT_TYPE_ERROR, _
"Error! Could not copy '" &amp; WScript.ScriptFullName &amp; "' to '" &amp; strNewWorkDir &amp; "\" &amp; strScriptName &amp; "'.")
Call WScript.Quit(1)
Else
strScriptName = strNewWorkDir &amp; "\" &amp; strScriptName
End If
End If

End If

'Add the script host
strCmdLine = objWSHShell.ExpandEnvironmentStrings("%windir%\system32\cmd.exe /C ") &amp; Chr(34) &amp; strScriptName &amp; Chr(34) &amp; strCmdLine

If blnUseWMI Then
Dim objWMIService
Dim objStartup
Dim objConfig
Dim objProcess
Dim intProcessID
Dim intReturn

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")

'Configure the process to break away from the job
Set objStartup = objWMIService.Get("Win32_ProcessStartup")
Set objConfig = objStartup.SpawnInstance_()
objConfig.ShowWindow = SW_HIDE
objConfig.CreateFlags = CREATE_BREAKAWAY_FROM_JOB

'Create the process
Set objProcess = objWMIService.Get("Win32_Process")
'Run the cmd line in a new process.
Call WScript.Echo("Executing cmd line '" &amp; strCmdLine &amp; "' in a new process...")
If Len(strNewWorkDir) &gt; 0 Then
intReturn = objProcess.Create(strCmdLine, strNewWorkDir, objConfig, intProcessID)
Else
intReturn = objProcess.Create(strCmdLine, Null, objConfig, intProcessID)
End If
Else
'Run the cmd line but do not wait.
Call WScript.Echo("Executing cmd line '" &amp; strCmdLine &amp; "'...")
Call objWSHShell.Run(strCmdLine, 0, False)
End If

'If the error number is 0 then the cmd was issued successfully
If Err.Number = 0 Then
Call objMomScriptAPI.LogScriptEvent(WScript.ScriptName &amp; " " &amp; SCRIPT_VERSION, EVENT_ID_SUCCESS, EVENT_TYPE_INFORMATION, _
"Executed the following cmd line asynchronously: " &amp; vbCrLf &amp; strCmdLine)
Else
Call objMomScriptAPI.LogScriptEvent(WScript.ScriptName &amp; " " &amp; SCRIPT_VERSION, EVENT_ID_SCRIPTERROR, EVENT_TYPE_ERROR, _
"Error! Could not execute the following cmd line asynchronously: " &amp; vbCrLf &amp; strCmdLine)
End If
Else
Call objMomScriptAPI.LogScriptEvent(WScript.ScriptName &amp; " " &amp; SCRIPT_VERSION, EVENT_ID_SCRIPTERROR, EVENT_TYPE_WARNING, _
"The arguments are missing.")
End If

End Sub
</Script></Contents>
<Unicode>true</Unicode>
</File>
<File>
<Name>$Config/CMDName$</Name>
<Contents><Script>$Config/CMDBody$</Script></Contents>
<Unicode>true</Unicode>
</File>
</Files>
</WriteAction>
</MemberModules>
<Composition>
<Node ID="WA1"/>
</Composition>
</Composite>
</ModuleImplementation>
<OutputType>System!System.CommandOutput</OutputType>
<InputType>System!System.BaseData</InputType>
</WriteActionModuleType>