I am trying to use vbscript to call uninstall.exe, but I get a
800A0401 - Expected End of state
error.
strPath ="""%ProgramFiles%\qstopmotion 2.5.2\uninstall.exe""" Set WshShell = WScript.CreateObject("WScript.Shell") WshShell.Run strPath Set WshShell = Nothing Wscript.Sleep 5000 set svc=getobject("winmgmts:root\cimv2") sQuery="select * from win32_process where name='Au_.exe'" set cproc=svc.execquery(sQuery) iniproc=cproc.count Do While iniproc = 1 wscript.sleep 5000 set svc=getobject("winmgmts:root\cimv2") sQuery="select * from win32_process where name='Au_.exe'" set cproc=svc.execquery(sQuery) iniproc=cproc.count Loop set cproc=nothing set svc=nothing
The error is at character 63, which is at the end of the tripe quotes. Can't seem to escape the path correctly. Any ideas?
VBScript syntax expects each line to represent a statement, in the example in the question the first statement is the end of your strPath variable, because further code is written VBScript returns a compilation error
Expected end of statement
You can fix this by tidying the code so each statement is it's own line;
strPath ="""%ProgramFiles%\qstopmotion 2.5.2\uninstall.exe"""
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run strPath
Set WshShell = Nothing
Wscript.Sleep 5000
set svc=getobject("winmgmts:root\cimv2")
sQuery="select * from win32_process where name='Au_.exe'"
set cproc=svc.execquery(sQuery)
iniproc=cproc.count
Do While iniproc = 1
wscript.sleep 5000
set svc=getobject("winmgmts:root\cimv2")
sQuery="select * from win32_process where name='Au_.exe'"
set cproc=svc.execquery(sQuery)
iniproc=cproc.count
Loop
set cproc=nothing
set svc=nothing
If you did want this all to run on one line VBScript provides the Statement Separator character (:) for this purpose so the following would also be acceptable, but not very readable (would not recommend doing this);
strPath ="""%ProgramFiles%\qstopmotion 2.5.2\uninstall.exe""" : Set WshShell = WScript.CreateObject("WScript.Shell") : WshShell.Run strPath : Set WshShell = Nothing : Wscript.Sleep 5000 : set svc=getobject("winmgmts:root\cimv2") : sQuery="select * from win32_process where name='Au_.exe'" : set cproc=svc.execquery(sQuery) : iniproc=cproc.count : Do While iniproc = 1 : wscript.sleep 5000 : set svc=getobject("winmgmts:root\cimv2") : sQuery="select * from win32_process where name='Au_.exe'" : set cproc=svc.execquery(sQuery) : iniproc=cproc.count : Loop : set cproc=nothing : set svc=nothing
Useful Links
VBScript, purpose of colon?
Related
I did some code to test one process and a list of processes. I did something myself, but I do not know where to set up another process. Please look at the image below and the code. I hope I wrote it clearly.
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim arrProcesses : arrProcesses = Split(objFSO.OpenTextFile("C:\Project\list_processes.txt").ReadAll, vbCrLf)
Dim objWMIService : Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Dim colProcesses : Set colProcesses = objWMIService.ExecQuery(_
"SELECT Name FROM Win32_Process WHERE Name = '" & Join(arrProcesses, "' OR Name = '") & "'")
If colProcesses.Count > 0 Then
Set WshShell = WScript.CreateObject("WScript.Shell")
Command = "Command 1"
WshShell.Run Command
Else
Set WshShell = WScript.CreateObject("WScript.Shell")
Command = "Command 2"
WshShell.Run Command
End If
Add another lookup for your specific process:
pname = "Process 1"
Set p = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name='" & pname & "'")
If p.Count > 0 And colProcesses.Count > 0 Then
'do something
Else
'do other
End If
Make sure "Process 1" is not in arrProcesses if your condition is "process 1 and any other process from the list are running".
Is it possible to use the SendKeys the type the characters inside a variable.
for example I got characters stored in variable firstname = Regie
Here is my code.
On Error Resume Next
Dim objSysInfo, objUser
Set objSysInfo = CreateObject("ADSystemInfo")
Set objUser = GetObject("LDAP://" & objSysInfo.UserName)
firstname = objUser.givenName
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.AppActivate "Notepad"
WScript.Sleep 400
WshShell.SendKeys "{& objUser.givenName}"
WScript.Sleep 250
Or is there any way to make this happen?
Sure you can, try this little script on the command line, it will print "test".
Set WshShell = WScript.CreateObject("WScript.Shell")
keys = "test"
WshShell.SendKeys keys
i'm trying to convert this function to wait notepad.exe to a generic wait function (that can wait for any process). I can't add a variable in the sQuery without breaking the syntax, is there any way to bypass the problem ?
Function wait()
Set svc=getobject("winmgmts:root\cimv2")
sQuery="select * from win32_process where name='notepad.exe'"
Set cproc=svc.execquery(sQuery)
iniproc=cproc.count
Do While iniproc = 1
wscript.sleep 5000
set svc=getobject("winmgmts:root\cimv2")
sQuery="select * from win32_process where name='notepad.exe'"
set cproc=svc.execquery(sQuery)
iniproc=cproc.count
Loop
Set cproc=nothing
Set svc=Nothing
End Function
Set fso = WScript.CreateObject("Scripting.Filesystemobject")
Set wscr = CreateObject("Wscript.shell")
wscr.Run "notepad.exe"
wait()
MsgBox "done!"
I'm going crazy!!
The WScript.Shell object's Run method, which you're using to start Notepad, has a built-in mechanism to wait for the process to finish. You don't need the WMI-based wait() function in your script at all.
wscr.Run "notepad.exe", 1, True
The True value at the end of the line is what indicates that Run() should wait for the process to finish.
But to answer your specific question, add a parameter to the wait function. I called it ProcessName. Then use this variable instead of notepad.exe. When you call wait(), you specify the name of the process as an argument.
Function wait(ProcessName)
Set svc=getobject("winmgmts:root\cimv2")
sQuery="select * from win32_process where name='" & ProcessName & "'"
Set cproc=svc.execquery(sQuery)
iniproc=cproc.count
Do While iniproc = 1
wscript.sleep 5000
set svc=getobject("winmgmts:root\cimv2")
sQuery="select * from win32_process where name='" & ProcessName & "'"
set cproc=svc.execquery(sQuery)
iniproc=cproc.count
Loop
Set cproc=nothing
Set svc=Nothing
End Function
Set fso = WScript.CreateObject("Scripting.Filesystemobject")
Set wscr = CreateObject("Wscript.shell")
wscr.Run "notepad.exe"
wait("notepad.exe")
MsgBox "done!"
Answered by Patrick S. However, there are two superabundant lines inside the Do While loop and two unnecessary lines:
Function wait(sPName)
Set svc=getobject("winmgmts:root\cimv2")
sQuery="select * from win32_process where name='" & sPName & "'"
Set cproc=svc.execquery(sQuery)
iniproc=cproc.count
Do While iniproc > 0
wscript.sleep 5000
'superabundant' set svc=getobject("winmgmts:root\cimv2")
'superabundant' sQuery="select * from win32_process where name='notepad.exe'"
set cproc=svc.execquery(sQuery)
iniproc=cproc.count
Loop
'unnecessary' Set cproc=nothing
'unnecessary' Set svc=Nothing
End Function
Set fso = WScript.CreateObject("Scripting.Filesystemobject")
Set wscr = CreateObject("Wscript.shell")
sProcessName="calc.exe"
wscr.Run sProcessName
wait( sProcessName)
MsgBox "done " & sProcessName
wscr.Run "notepad.exe"
wait( "notepad.exe")
MsgBox "done notepad.exe"
Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2")
Set objEvents = objWMIService.ExecNotificationQuery ("SELECT * FROM Win32_ProcessTrace")
Do
Set objReceivedEvent = objEvents.NextEvent
If objReceivedEvent.ProcessName = "svchost.exe" then msgbox objReceivedEvent.ProcessName
' msgbox objReceivedEvent.ProcessName
Loop
There is Win32_ProcessTrace (starts and stops), Win32_ProcessTraceStart, and Win32_ProcessTraceStop. The script stops and waits for events to occur.
I'm not a programmer so I don't want to overly irritate the fine folk in this forum. My issue is that I would like to use VBScript to Telnet into a Linux device, issue a DF command and output all response to a log file which I can parse later. I originally found a method to successfully Telnet but I have have been experimenting without success regarding the text file output requirement. The following code certainly does not work but I am wondering if I am even close to the correct method?
Dim WshShell, oExec
Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec("cmd /c dir")
WshShell.run"cmd" '*** open command window ***
WScript.Sleep 250
WshShell.SendKeys("{Enter}")
WshShell.SendKeys"telnet 10.13.2.2"
WshShell.SendKeys("{Enter}")
WScript.Sleep 2000
WshShell.SendKeys"root"
WshShell.SendKeys("{Enter}")
WScript.Sleep 1500
WshShell.SendKeys"password"
WshShell.SendKeys("{Enter}")
WScript.Sleep 1500
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objLogFile = objFSO.OpenTextFile("C:\VBSmemSize.txt", 2, True)
WshShell.SendKeys"df /mnt/cf"
WshShell.SendKeys("{Enter}")
Do
strFromProc = oExec.Stdout.Readline()
WScript.Echo strFromProc
Loop While Not objLogFile.StdOut.atEndOfStream
You can capture output from external commands but not at the same time interact with them like you do with sendkeys. Here an example of what works
Function ExecPing(strTarget)
Set objShell = CreateObject("WScript.Shell")
Set objExec = objShell.Exec("ping -n 2 -w 1000 " & strTarget)
strPingResults = LCase(objExec.StdOut.ReadAll)
If InStr(strPingResults, "antwoord van") Then '"reply from" in E
WScript.Echo VbCrLf & strTarget & " responded to ping."
ExecPing = True
Else
WScript.Echo VbCrLf & strTarget & " did not respond to ping."
ExecPing = False
End If
End Function
ExecPing pcname
I am getting the following error when I try to send to send a "(" or ")" character using SendKeys .In my vbscript.
Invalid procedure call or argument
My script:
Set WshShell = WScript.CreateObject("WScript.Shell")
WScript.Sleep 100
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Documents and Settings\Administrator\Desktop\readme.txt", 1)
Wshshell.SendKeys "!##$%^&*()"
Do Until objFile.AtEndOfStream
strCharacters = objFile.Read(1)
WshShell.SendKeys strCharacters
Loop
It does not send the "(" and ")" when I try to send them before the loop but shows no error and continues till a little further where it encounters another "(" character and stops with the error.
Parenthesis are considered a special character and need to be surrounded in braces. See this link for more details.
Set WshShell = WScript.CreateObject("WScript.Shell")
WScript.Sleep 100
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Documents and Settings\Administrator\Desktop\readme.txt", 1)
Wshshell.SendKeys "!##$%^&*{(}{)}"
Do Until objFile.AtEndOfStream
strCharacters = objFile.Read(1)
WshShell.SendKeys strCharacters
Loop