I have a scenario setup where I need to test to see if the results of a .bat file execution returned results. The .bat file calls up another .exe and the .bat file has a CMD DOS window that outputs critical error info to that DOS box from the .exe. If the .exe does not start correctly, I am able to check the results in our SQL DB. I need to close the current .bat file and re-launch it.
This seems fairly simple, I can get the ProcessID using the WMI call. I can terminate the process with a Terminate() command. This works for any .exe I use for testing: notepad, calc, iexplorer, etc. When I run the VBScript to kill the .bat file, it says it terminates the PID (which it does, since I cannot see the PID anymore), however, the DOS box is still open and the .exe called from the .bat file is still running. If I click on the "X" on the DOS box or right-click on the title bar and select "Close", the DOS box and the .exe are both killed. How do I get this script to work correctly. The server is running Windows Server 2003 (some are x32 and others are x64). Any ideas? Here is a version of my code, there have been several revs:
If colFiles.Count = 0 Then
Wscript.Echo "The file does not exist on the remote computer."
Else
Wscript.Echo "The file exists on the remote computer."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery("Select * from Win32_Process")
For Each objItem in colProcessList
If InStr(objItem.CommandLine, "r15_A.bat") Then
Wscript.Echo "Batch file: " & objItem.CommandLine & " Process ID: " & objItem.ProcessID & " Parent ID: " & objItem.ParentProcessID
Call TerminateProcess(objItem.ParentProcessID)
Call TerminateProcess(objItem.ProcessID)
End If
Next
dim shell
set shell=createobject("wscript.shell")
shell.run BATDIR & BATFILE
'set shell=nothing
End If
Function TerminateProcess(PID)
Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2")
Set colProcesses = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Handle = '" & PID & "'")
For Each objProcess in colProcesses
On Error Resume Next
return = objProcess.Terminate()
Set colProcesses = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Handle = '" & PID & "'")
If Error Then
TerminateProcess = FALSE
Else
TerminateProcess = TRUE
msgBox colProcesses.Count
End If
Next
End Function
I have even tried to use the SendKeys to send ALt-F4, X, C, etc. AppActivate does bring the correct DOS box to the front.
Set WshShell = CreateObject("WScript.Shell")
WshShell.AppActivate "r15_A"
'WshShell.SendKeys("+{F10}")
WshShell.SendKeys("C")
Thanks in advance
EDIT--------------
Yes, I am going to have to kill the process that this .bat file calls. Only down side is this DOS box stays open. Annoying but I can live with it. In the end I wrote a script similar to my first one but only checks for the instance I want to kill by checking the CommandLine parameter: (The first bit of code with "-FAIL.txt" test to see if a FAIL file is present, if it is, then the rest of the scipts executes)
Set colFiles = objWMIService. _
ExecQuery("Select * From CIM_DataFile Where Name = 'S:\\servers\\Logs\\" & LOGFILE & "-FAIL.txt'")
If colFiles.Count = 0 Then
'Wscript.Echo "The file does not exist on the remote computer."
Else
'Wscript.Echo "The file exists on the remote computer."
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = 'AppStart.exe'")
For Each objProcess in colProcessList
IF instr(objProcess.CommandLine, "robo05-A") > 0 THEN
'msgBox "Found text"
objProcess.Terminate()
'This part restarts the failed .bat file.
wscript.sleep (200)
dim shell
set shell=createobject("wscript.shell")
shell.run BATDIR & BATFILE
'set shell=nothing
ELSE
'msgBox "Not found"
END IF
Next
End If
The problem your having is the the executable that is being called from within your batch file is still running. I tried to recreated your setup with a bat the just did a ping -t %computername%, and sure enough the vbscript killed the cmd.exe but not the ping (Please note the Parent PID was explorer.exe, so you probably don't want to attempt killing that). Now I could easily kill ping.exe from task manager which then closed the window...but I only knew to do that because I knew what the bat was doing and what was left open. You could do a WMI search for the process that contains "ping.exe", grab the PID and then close that and that would basically be doing the same thing. Of course in your situation "ping.exe" is probably not the same executable as mine. If the exe changes every now and then then you could grab the commandline from your process find the parameter which contains the filepath to the r15_A.bat file, then use the filesystemobject to open this bat file, look for any executables which it calls which could possibly be still running and then kill those....but really that may be overkill for what your trying to accomplish.
Related
I am attempting to write a script to an uninstall of some applications. I have the script working, but every program throws up a prompt when run.
I know that the applications support the “/S” parameter so a silent uninstall can be done. I can run the uninstall /s command from a command prompt and it works fine. No prompts, it just uninstalls.
My problem is invoking the /S parameter in a script. No matter how I try, I keep getting syntax errors. I know this is just me and my non-understanding of quotes and parenthesis, but I’m sort of tired of trying to get it to work. The problem is compounded by the fact that all the paths have spaces in them, which necessitates more of those dang quotes.
Hopefully someone can show me what I am doing wrong.
Also, I really don’t know what I’m doing with VBS stuff, so I’d appreciate it if you could all overlook how ugly the script is. :-)
I also have a question about the “true” parameter. My understanding is that this indicates that the current operation should be completed before moving on to the next operation. But the uninstalls seem to run all at the same time. Am I understanding the “true” parameter correctly?
The command for a silent uninstall is:
C:\Program Files\Juniper Networks\Network Connect 7.1.9\uninstall.exe /S
Here is my script without the "/S' parameter.
'Uninstall Juniper Networks Network Connect 7.1.9
Wscript.Echo "Uninstalling 'Juniper Networks Network Connect 7.1.9'"
Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run ("""C:\Program Files\Juniper Networks\Network Connect 7.1.9\uninstall.exe"""), 1, True
Set objShell = nothing
Here's my self-explainig solution:
' VB Script Document
'http://stackoverflow.com/questions/23569022/trying-to-script-a-silent-uninstall-with-vbscript
option explicit
Dim strResult
strResult = Wscript.ScriptName _
& vbTab & "Uninstalling 'Juniper Networks Network Connect 7.1.9'"
Dim strCommand, intWindowStyle, bWaitOnReturn, intRetCode
' strCommand
' String value indicating the command line you want to run.
' You must include any parameters you want to pass to the executable file.
strCommand = """C:\Program Files\Juniper Networks\Network Connect 7.1.9\uninstall.exe"" /S"
'for test only strCommand = """C:\Program Files\Mozilla Firefox\firefox.exe"" -safe-mode"
' intWindowStyle
' Optional. Integer value indicating the appearance of the program's window.
' Note that not all programs make use of this information.
intWindowStyle = 1
' bWaitOnReturn
' Optional. Boolean value indicating whether the script should wait
' for the program to finish executing before continuing
' to the next statement in your script.
' If set to true, script execution halts until the program finishes,
' and Run returns any error code returned by the program.
' If set to false (the default), the Run method returns 0 immediately
' after starting the program (not to be interpreted as an error code).
bWaitOnReturn = True
strResult = strResult & vbNewLine & strCommand _
& vbNewLine & CStr( intWindowStyle) _
& vbNewLine & CStr( bWaitOnReturn)
Wscript.Echo strResult
Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
' intRetCode
' The Run method returns an integer
intRetCode = objShell.Run( strCommand, intWindowStyle, bWaitOnReturn)
Set objShell = nothing
Wscript.Echo strResult & vbNewLine & "result=" & CStr( intRetCode)
Wscript.Quit
total vbs scripting newb here. I'm trying to automate closing a certain open window, namely a program called HostsMan. This is on Windows 8.1 Pro 64 bit, and this is what my script currently looks like:
Set WshShell = CreateObject("WScript.Shell")
WshShell.AppActivate "HostsMan"
WshShell.SendKeys "%{F4}"
The second line doesn't seem to work. I know line 3 works because it activates the Windows shutdown menu. Is there something I'm missing?
Update/more info: Manually entering alt-F4 does close it, so I know this should work. I also tested this script with other open windows and they close just fine. Additionally, HostsMan is opened with Admin privileges, so I tried running the script as a task set with highest privileges to see if that would do it, and still no go. But that does work with other open windows running with Admin privileges. Frustrating!
I've tried it, too, and couldn't get it to work. There must be something about the window class, perhaps, where AppActivate doesn't see it as a top-level window?
In any event, AppActivate also lets you pass the process ID instead of the window title. When I installed HostsMan, the process name was hm.exe, so I'll use that in my example below.
Set Processes = GetObject("winmgmts:").InstancesOf("Win32_Process")
For Each Process In Processes
If StrComp(Process.Name, "hm.exe", vbTextCompare) = 0 Then
' Activate the window using its process ID...
With CreateObject("WScript.Shell")
.AppActivate Process.ProcessId
.SendKeys "%{F4}"
End With
' We found our process. No more iteration required...
Exit For
End If
Next
Alternative solution using WMIService (no loop through all processes required):
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * from Win32_Process WHERE Name = '" & ProcessName & "'")
If colItems.Count = 0 Then
WScript.Echo "Process not found"
Wscript.Quit
End If
For Each objProcess in colItems
WshShell.AppActivate(objProcess.ProcessId)
Exit For
Next
The key here is to put a small pause after the 'run' command but before the 'appactivate' command so the application shows up in the process list.
WshShell.Run "calc"
WScript.Sleep 100
WshShell.AppActivate "Calculator"
To solve the problem of AppActivate you have to use loop and if condition statement to check if the active windows is the target windows or not because sometime you deselect or the system deselect the target windows before execute the sendkeys directly so you got error.
I create this tight strict way
Set WshShell = CreateObject("WScript.Shell")
for i=0 to 300 ' this loop will continue about 30 sec if this not enough increase this number
Rtn=WshShell.AppActivate("HostsMan") ' HostMan have to be the windows title of application or its process ID
If Rtn = True Then
WshShell.SendKeys "%{F4}" ' send key to click ALT+F4 to close
wscript.sleep 100 ' stop execute next line until finish close app
Rtn=WshShell.AppActivate("HostsMan")
If Rtn=False Then ' using nested If to sure of selected windows is false because its close
Exit For ' exit for loop directly and execute what after for next
End If
End If
wscript.sleep 100
Next
Dim sh : Set sh =CreateObject("Wscript.Shell")
sh.Exec "calc.exe"
Wscript.Sleep 1000
sh.Exec "taskkill /f /FI ""WINDOWTITLE eq ""calc*"""
OR
sh.Run "taskkill /f /im calc.exe",0,False
Under windows 7 with uac activated.
This is all around an issue. THIS ISSUE:
upnphost excessive cpu load
Especifically:
Mine has this problem frequently, and I hate to go restart the upnphost service all the time, so instead i just created a task in the
task scheduler to run a once a day, and repeat every 5 minutes. The
task runs a .bat file:
net stop upnphost
net start upnphost
if you want to make it run in the background without the cmd window
coming up, run this .vbs with the above .bat already created as
"C:\upnphost.bat":
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "C:\upnphost.bat" & Chr(34), 0 Set
WshShell = Nothing
this way the upnphost service will automatically restart every 5
minutes with no visible presentation, so if it decides to misbehave
and go high cpu, it will be for 5 minutes tops, you could change this
to any interval you want.
Everything works except for the fact that for the bat to actually work i need to right click directly on the bat and execute as admin.
For that reason if i click the vbs it will execute it but not as admin and it wont work.
So scheduling it as a task wont work either.
Can you fix the code in the vbs:
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "C:\upnphost.bat" & Chr(34), 0 Set
WshShell = Nothing
So it executes the bat with admin priviledge.
It that matters, my route to the bat has spaces.
Respectfully
You can launch an program as an administrator with UAC enabled, but you'll still get prompted whether you want to launch this program. For example,
Set objSA = CreateObject ("Shell.Application")
objSA.ShellExecute "cmd.exe","uac","","runas",1
However,
I was able to use this vbscript to stop and restart this service using a scheduled task, setting the task to run as "Hidden" and run with the highest privileges.
strService = "upnphost"
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colService = objWMIService.ExecQuery("SELECT Name FROM Win32_Service " _
& "where Name='" & strService & "'")
For Each objService In colService
return = objService.StopService()
return = objService.StartService()
Next
The scheduled task must be run as a user who can start and stop the service (usually an admin account) and with the option "Run with highest privileges" enabled.
I have a vbs file that need to be run in 32-bit, even though I am running Windows 7 64-bit. I can launch this file with the command
C:\Windows\SysWOW64\cscript.exe my-file.vbs
and that works fine, but it leaves me with a redundant command prompt window that I have to close manually each time. It also makes it very cumbersome to run this vbs-file as a startup item.
Is there a way to start my 32-bit vbs-file in the background?
Try this for the 64bit problem, if it works you can combine it with the other answers
EDIT: here a question that goes more in depth on the 32/64 bit issue
How do I check if wscript/cscript runs on x64 host OS?
here the modified version, should make sure the script runs on 64bit platform
On Error Resume Next
Dim WshShell, OsType
Set WshShell = CreateObject("WScript.Shell")
OsType = WshShell.RegRead("HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\PROCESSOR_ARCHITECTURE")
If OsType = "x86" then
wscript.echo "Windows 32bit system detected"
else
wscript.echo "Windows 64bit system detected"
If InStr(LCase(WScript.FullName),"system32") Then
CreateObject("WScript.Shell").Run """%systemroot%\SysWOW64\wscript.exe"" """ & WScript.ScriptFullName & """"
Wscript.Quit
End If
end if
Msgbox("I ran..")
If you need to use cscript this is IMHO a cool solution
Const HIDDEN_WINDOW = 0
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set objStartup = objWMIService.Get("Win32_ProcessStartup")
Set objConfig = objStartup.SpawnInstance_
objConfig.ShowWindow = HIDDEN_WINDOW
Set objProcess = GetObject("winmgmts:\\" & strComputer & "\root\cimv2:Win32_Process")
objProcess.Create "Cscript.exe h:\Script\Test1.vbs", null, objConfig, intProcessID
If you can use wscript you could do the following, it's the simplest approach
Set objShell = CreateObject("Wscript.Shell")
objShell.Run "Wscript.exe h:\Script\Test1.vbs"
actually you can do it as a one-liner (i'm a Ruby guy 8>)
CreateObject("Wscript.Shell").Run("Wscript.exe h:\Script\Test1.vbs")
I'm trying to write a Windows command file to open a webpage in IE, wait for it to load, then close the IE window. The following works but will kill all IE windows so any that were already open before running the .cmd will also get closed.
start iexplore.exe "page to load"
ping localhost -n 10 > nul
taskkill /IM iexplore.exe
I only want to kill the IE that was opened. I know I can just kill a particular process if I know its PID but how can find this from the command line? Is there a way to get it when starting the IE window? What I really want to do is:
start iexplore.exe "page to load"
ping localhost -n 10 > nul
taskkill /PID ?
where ? is the PID of the IE that gets opened but how can I get this? This needs to run as a .cmd file without any input from a user.
IE already supports automation, there is no point in finding and killing the correct process:
Set IE = CreateObject("InternetExplorer.Application")
IE.visible=true
IE.navigate "http://stackoverflow.com/"
while IE.Busy
WScript.Sleep 555
wend
IE.Quit
Save as .vbs (And run with wscript.exe from parent program/batch file)
use vbscript
Set objFS=CreateObject("Scripting.FileSystemObject")
Set objArgs = WScript.Arguments
strProcess = objArgs(0) 'argument, which is the process name
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
' call WMI service Win32_Process
Set colProcessList = objWMIService.ExecQuery("Select * from Win32_Process Where Name = '"&strProcess&"'")
t=0
For Each objProcess in colProcessList
' do some fine tuning on the process creation date to get rid of "." and "+"
s = Replace( objProcess.CreationDate ,".","")
s = Replace( objProcess.CreationDate ,"+","")
' Find the greatest value of creation date
If s > t Then
t=s
strLatestPid = objProcess.ProcessID
End If
Next
WScript.Echo "latest: " & t , strLatestPid
'Call WMI to terminate the process using the found process id above
Set colProcess = objWMIService.ExecQuery("Select * from Win32_Process where ProcessId =" & strLatestPid)
For Each objProcess in colProcess
objProcess.Terminate()
Next
usage:
c:\test>cscript //nologo kill.vbs "iexplore.exe"