How to terminate a VBS File using Vb script - vbscript

How to terminate a VBS File using Vb script.. I tried this code and it is not working,
Call StopProcessVBS(strComputer,strProcess)
Function StopProcessVBS (strComputerArg,strProcessArg)
Set WshShell = CreateObject("WScript.Shell")
Dim objWMIService, colProcessList
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputerArg & "\root\cimv2")
Set colItems = objWMIService.ExecQuery ("Select * from Win32_Process Where Name = 'cscript.exe' OR Name = 'wscript.exe'")
For Each objItem in colItems
If objItem.CommandLine = strProcessArg Then
**objItem.CommandLine.Terminate()**
End If
Next
Set WshShell = Nothing
Set objWMIService = Nothing
Set colItems = Nothing
End Function

Finally Worked, Tried the below code
strComputer = "."
Call StopProcessVBS(strComputer,strProcess)
Function StopProcessVBS (strComputerArg,strProcessArg)
Set WshShell = CreateObject("WScript.Shell")
Dim objWMIService, colProcessList
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputerArg & "\root\cimv2")
Set colItems = objWMIService.ExecQuery ("Select * from Win32_Process Where Name = 'cscript.exe' OR Name = 'wscript.exe'")
For Each objItem in colItems
If Instr (1,Replace(objItem.CommandLine,"""",""),strProcessArg) Then
objItem.Terminate()
End If
Next
Set WshShell = Nothing
Set objWMIService = Nothing
Set colItems = Nothing
End Function

Related

WScript.Shell not opening command window

I have an executable EMC_WS.EXE and I want to run this using the command
WshShell.Run "EMC_WS.EXE", 1, False
The executable runs correctly, but the command window is not appearing. I have used mingwstudio to generate EMC_WS.EXE.
Dim pathString, UserPath
Dim position
Dim WshShell, Shell
Set WshShell = WScript.CreateObject("WScript.Shell")
Set Shell = WshShell.Environment("User")
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name = 'EMC_WS.EXE'")
For Each objProcess In colProcessList
objProcess.Terminate()
Next
WshShell.Run "EMC_WS.EXE", 1, False
Set WshShell = Nothing
WScript.Quit(0)

Check to see if the script has already run before on a particular machine?

So I have the following scipt that I run when a user logs off using the logoff script section in Group Policy (I would like to run a check to see if it has already run before on the particular computer. If it has run before I would like the script to exit. If it hasn't then I want it to run the script and mark itself as "already been run". How can I do that?):
strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery _
("Select * from Win32_Printer Where Network = TRUE")
For Each objPrinter in colInstalledPrinters
objPrinter.Delete_ Next
I figured it out. Here's the revised script:
Option Explicit
Dim oShell,strComputer,objWMIService,colInstalledPrinters,objPPrinter
Private Function KeyExists (keyName)
Dim bKey
On Error Resume Next
bKey = oShell.RegRead(keyName)
If TypeName (bKey) = "Empty" Then
KeyExists = False
Else
KeyExists = True
End If
End Function
Set oShell = CreateObject("Wscript.Shell")
If keyExists("HKEY_LOCAL_MACHINE\Software\CRusse\RemovePrinters") Then
wscript.quit
Else
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery _
("Select * from Win32_Printer Where Network = TRUE")
For Each objPrinter in colInstalledPrinters
objPrinter.Delete_
Next
oshell.RegWrite "HKEY_LOCAL_MACHINE\Software\CRusse\RemovePrinters", 1, "REG_SZ"
End If
Set oShell = Nothing

Using VBScript how can I check if the Spooler service is started and if not start it?

I'd like to use VBScript to check if the Spooler service is started and if not start it, the code below checks the service status but I need some help modifying this so I can check if it is started.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colRunningServices = objWMIService.ExecQuery _
("Select * from Win32_Service")
For Each objService in colRunningServices
Wscript.Echo objService.DisplayName & VbTab & objService.State
Next
Many thanks
Steven
How about something like this. This command will start it if it isn't already running. No need to check in advance.
Dim shell
Set shell = CreateObject("WScript.Shell")
shell.Run "NET START spooler", 1, false
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colRunningServices = objWMIService.ExecQuery _
("select State from Win32_Service where Name = 'Spooler'")
For Each objService in colRunningServices
If objService.State <> "Running" Then
errReturn = objService.StartService()
End If
Next
Note you can also use objService.started to check if its started.
Just for the completeless, here's an alternative variant using the Shell.Application object:
Const strServiceName = "Spooler"
Set oShell = CreateObject("Shell.Application")
If Not oShell.IsServiceRunning(strServiceName) Then
oShell.ServiceStart strServiceName, False
End If
Or simply:
Set oShell = CreateObject("Shell.Application")
oShell.ServiceStart "Spooler", False ''# This returns False if the service is already running

How to terminate process using VBScript

I have this VBScript code to terminate one process
Const strComputer = "."
Dim objWMIService, colProcessList
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name = 'Process.exe'")
For Each objProcess in colProcessList
objProcess.Terminate()
Next
It works fine with some processes, but when it comes to any process runs under SYSTEM, it can't stop it.
Is there is anything I need to add to kill the process under SYSTEM?
The way I have gotten this to work in the past is by using PsKill from Microsoft's SysInternals. PsKill can terminate system processes and any processes that are locked.
You need to download the executable and place it in the same directory as the script or add it's path in the WshShell.Exec call. Here's your sample code changed to use PsKill.
Const strComputer = "."
Set WshShell = CreateObject("WScript.Shell")
Dim objWMIService, colProcessList
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name = 'Process.exe'")
For Each objProcess in colProcessList
WshShell.Exec "PSKill " & objProcess.ProcessId
Next
Try explicit assert debug privilege {impersonationLevel=impersonate,(debug)}:
Set wmi = GetObject("winmgmts:{impersonationLevel=impersonate,(debug)}!\\.\root\CIMV2")
Set procs = wmi.ExecQuery("SELECT * FROM Win32_Process WHERE Name='SearchIndexer.exe'", , 48)
For Each proc In procs
proc.Terminate
Next

VB Script GetObject method error...Help please WMI

at this point i get error message:
Set objWMI = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Error: Invalid procedure call or Argument: "GetObject"
Code: 800A005
Source: Microsoft VBScript Runtime error
Complete rewrite.
Here is the code generated by WMI Code Creator, with very minor editing.
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery( _
"SELECT * FROM Win32_ComputerSystem",,48)
For Each objItem in colItems
MsgBox "CurrentTimeZone: " & objItem.CurrentTimeZone
Next

Resources