REM *****************************************
REM This script will trigger an EDX task in Qlikview Server 9 or 10. The task must be must have a 
REM "Triggered by External Event" trigger defined. (This was called "EDX" in QVS 8).
REM
REM Execute this script from a windows command line as:
REM    cscript RequestEdx.vbs taskname
REM where taskname is the name assigned to the task. If the taskname contains spaces, enclose it on quotes.
REM For Publisher installations, this task name is assigned in 
REM the Task definition. For sites where Publisher is not licensed, the task name is the qvw document name.
REM
REM !! The windows account running this script must be a member of either the "Qlikview Adminstrators" or "Qlikview EDX" local group
REM on the machine designated in the url= value below. See section 26.2 (qv9) or 27.5 (qv10) of the QVS manual for more info.
REM
REM Written by Rob Wunderlich.
REM *****************************************

Option explicit
Dim url, xmlhttp, task, key
REM Change the url= value below to reflect the URL of the Distribution service. It must be the exact URL as shown in
REM the QEMC. Do not use a DNS alias or IP address.
url = "http://yblprdakqlikapp:4720/qtxs.asmx"

If Wscript.Arguments.Count <> 1 Then
	log "Usage: cscript RequestEdx.vbs taskname"
	log "If taskname contains spaces, it must be enclosed in quotes."
	Wscript.Quit(1)
End If

task = WScript.Arguments.Item(0)

set xmlhttp = createobject("msxml2.xmlhttp.3.0")  

key = getKey()
If len(key) > 0 Then
	Call StartTask( task, key)
End If

Sub StartTask(task, requestKey)  
	Dim requestData, resultCode   

	requestData = "<Global method=""RequestEDX"" key=""" & requestKey  _
		& """><i_TaskIDOrTaskName>" & task _
		& "</i_TaskIDOrTaskName><i_Password /><i_VariableName /><i_VariableValueList /></Global>" 
	xmlhttp.open "post", url, false 
	xmlhttp.send requestData
	
	'log "Status: " & xmlhttp.statusText
	'log "Response: " & xmlhttp.responseText
	
	If xmlhttp.responseXML.selectSingleNode("//TaskStartResultCode") Is Nothing Then
		resultCode = ""
	Else 
		resultCode = xmlhttp.responseXML.selectSingleNode("//TaskStartResultCode").text
	End If
	 
	Select Case  resultCode
		Case "" 
			log "Start failed for task: " & task & ", httpStatus=" & xmlhttp.status & ":"& xmlhttp.statusText
			log xmlhttp.responseText  
		Case "0"
			log "Task " & task &  " started."
		Case Else
			log "Start failed for task: " & task 
			log "Error: " & xmlhttp.responseXML.selectSingleNode("//TaskStartResult").text
	End Select
	  
End Sub

Function GetKey()
	Dim requestData
	xmlhttp.open "post", url, false
	requestData = "<Global method=""GetTimeLimitedRequestKey"" />" 
	xmlhttp.send requestData
	'log "Status: " & xmlhttp.statusText
	'log "Response: " & xmlhttp.responseText
	If xmlhttp.status = 200 Then
		GetKey = xmlhttp.responseXML.selectSingleNode("//GetTimeLimitedRequestKeyResult").text
	Else
		log "Error return from GetKey, statusText: " & xmlhttp.status & ":"& xmlhttp.statusText
		log "Response: " & xmlhttp.responseText
		
		GetKey = ""
	End If
End Function

Sub log(message)
	Wscript.Echo message
End Sub