option explicit
dim objWMIService
dim fileSystem
const wmiSuccessful = 0
Main()
'-----------------------------------------------------------------
' Main rountine
'-----------------------------------------------------------------
Sub Main()
dim vmName, computer, objArgs, mouse, video, computerSystem, buttonIndex
set fileSystem = Wscript.CreateObject("Scripting.FileSystemObject")
computer = "."
set objWMIService = GetObject("winmgmts:\\" & computer & "\root\virtualization")
set objArgs = WScript.Arguments
if WScript.Arguments.Count = 2 then
vmName= objArgs.Unnamed.Item(0)
buttonIndex = objArgs.Unnamed.Item(1)
else
WScript.Echo "usage: cscript ClickButton.vbs vmName buttonIndex"
WScript.Quit
end if
set computerSystem = GetComputerSystem(vmName)
set mouse = GetComputerMouse(computerSystem)
set video = GetComputerVideoHead(computerSystem)
if ClickButton(mouse, buttonIndex, video) then
WriteLog "Done"
WScript.Quit(0)
else
WriteLog "ClickButton failed"
WScript.Quit(1)
end if
End Sub
'-----------------------------------------------------------------
' Retrieve Msvm_VirtualComputerSystem from base on its ElementName
'-----------------------------------------------------------------
Function GetComputerSystem(vmElementName)
On Error Resume Next
dim query
query = Format1("select * from Msvm_ComputerSystem where ElementName = '{0}'", vmElementName)
set GetComputerSystem = objWMIService.ExecQuery(query).ItemIndex(0)
if (Err.Number <> 0) then
WriteLog Format1("Err.Number: {0}", Err.Number)
WriteLog Format1("Err.Description:{0}",Err.Description)
WScript.Quit(1)
end if
End Function
'-----------------------------------------------------------------
' Retrieve Msvm_SyntheticMouse from given computer system
'-----------------------------------------------------------------
Function GetComputerMouse(computerSystem)
On Error Resume Next
dim query
query = Format1("ASSOCIATORS OF {{0}} WHERE resultClass = Msvm_SyntheticMouse", computerSystem.Path_.Path)
set GetComputerMouse = objWMIService.ExecQuery(query).ItemIndex(0)
if (Err.Number <> 0) then
WriteLog Format1("Err.Number: {0}", Err.Number)
WriteLog Format1("Err.Description:{0}",Err.Description)
WScript.Quit(1)
end if
End Function
'-----------------------------------------------------------------
' Retrieve Msvm_VideoHead from given computer system
'-----------------------------------------------------------------
Function GetComputerVideoHead(computerSystem)
On Error Resume Next
dim query, controller
query = Format1("ASSOCIATORS OF {{0}} WHERE resultClass = Msvm_SyntheticDisplayController", computerSystem.Path_.Path)
set controller = objWMIService.ExecQuery(query).ItemIndex(0)
if (Err.Number <> 0) then
WriteLog Format1("Err.Number: {0}", Err.Number)
WriteLog Format1("Err.Description:{0}",Err.Description)
WScript.Quit(1)
end if
query = Format1("ASSOCIATORS OF {{0}} WHERE resultClass = Msvm_VideoHead", controller.Path_.Path)
set GetComputerVideoHead = objWMIService.ExecQuery(query).ItemIndex(0)
if (Err.Number <> 0) then
WriteLog Format1("Err.Number: {0}", Err.Number)
WriteLog Format1("Err.Description:{0}",Err.Description)
WScript.Quit(1)
end if
End Function
'-----------------------------------------------------------------
' set mouse relative position on a virtual machine
'-----------------------------------------------------------------
Function setAbsolutePosition(mouse, video)
WriteLog Format1("setAbsolutePosition({0})", mouse.ElementName)
dim objInParam, objOutParams
setAbsolutePosition = false
set objInParam = mouse.Methods_("setAbsolutePosition").InParameters.SpawnInstance_()
objInParam.horizontalPosition = 1
objInParam.verticalPosition = video.CurrentVerticalResolution - 1
set objOutParams = mouse.ExecMethod_("setAbsolutePosition", objInParam)
if objOutParams.ReturnValue = wmiSuccessful then
WriteLog Format1("Mouse position is set at (1,{0})", objInParam.verticalPosition)
setAbsolutePosition = true
end if
End Function
'-----------------------------------------------------------------
' Click mouse on a virtual machine
'-----------------------------------------------------------------
Function ClickButton(mouse, buttonIndex, video)
WriteLog Format2("ClickButton({0}, {1})", mouse.ElementName, buttonIndex)
dim objInParam, objOutParams
ClickButton = false
if Not setAbsolutePosition(mouse, video) then
Exit Function
end if
set objInParam = mouse.Methods_("ClickButton").InParameters.SpawnInstance_()
objInParam.buttonIndex = buttonIndex
set objOutParams = mouse.ExecMethod_("ClickButton", objInParam)
if objOutParams.ReturnValue = wmiSuccessful then
WriteLog Format2("{0} button {1} was clicked.", mouse.ElementName, buttonIndex)
ClickButton = true
end if
End Function
'-----------------------------------------------------------------
' Create the console log files.
'-----------------------------------------------------------------
Sub WriteLog(line)
dim fileStream
set fileStream = fileSystem.OpenTextFile(".\clickButton.log", 8, true)
WScript.Echo line
fileStream.WriteLine line
fileStream.Close
End Sub
'------------------------------------------------------------------------------
' The string formating functions to avoid string concatenation.
'------------------------------------------------------------------------------
Function Format2(myString, arg0, arg1)
Format2 = Format1(myString, arg0)
Format2 = Replace(Format2, "{1}", arg1)
End Function
'------------------------------------------------------------------------------
' The string formating functions to avoid string concatenation.
'------------------------------------------------------------------------------
Function Format1(myString, arg0)
Format1 = Replace(myString, "{0}", arg0)
End Function