VB script Kill process and delete folder - vbscript

I am a noop when it comes to vbscript but I managed to make something. But now I am kinda stuck.
Let me try to explain what I am trying to do
I need to kill a process on a computer [That part I know and made] VBpart 1
When the process is killed I need to delete a folder with all it content [ That I managed also] VB part 2
VB part 1
Option Explicit
Dim objWMIService, objProcess, colProcess
Dim xComputer, xProcessKill
xComputer = "."
xProcessKill = "'diamant client.exe'"
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& xComputer & "\root\cimv2")
Set colProcess = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = " & xProcessKill)
For Each objProcess in colProcess
objProcess.Terminate()
next
VB part 2
strFolder = "\\srvad01\DFS\Sandbox$\username\sandbox\DiaSoft Framework Diamant Client 1.0 NL"
set objFSO = createobject("Scripting.FileSystemObject")
objFSO.DeleteFolder strFolder
But somehow I can't merge these 2 in 1 file While they work separate fine
What am I doing wrong?
Kind regarts Arie

Related

Close an open OneNote application using VBS

I'm trying to close an open OneNote application on a user's computer using VB Script. However, I cannot seem to get it to work. I need to close any open OneNotes before I run the rest of the VBS file. So far I've tried this, but doesn't work for OneNote.
Set oNote= CreateObject("WScript.Shell")
oNote.Exec "onenote"
oNote.Terminate
This is another code I've tried. Neither work.
Set oNote= CreateObject("onenote")
oNote.Quit
You can try like this way to kill Onenote.exe process
Option Explicit
Dim Process
Process = "Onenote.exe"
Call Kill(Process)
'****************************************************
Sub Kill(Process)
Dim Ws,Command,Execution
Set Ws = CreateObject("Wscript.Shell")
Command = "cmd /c Taskkill /F /IM "& Process &""
Execution = Ws.Run(Command,0,True)
Set Ws = Nothing
End Sub
'****************************************************
Or by this way :
Option Explicit
Dim objWMIService, objProcess, colProcess
Dim strComputer, strProcessKill
strComputer = "."
strProcessKill = "'Onenote.exe'"
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colProcess = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = " & strProcessKill )
For Each objProcess in colProcess
objProcess.Terminate(1)
Next

VBScript and sendkeys not working with DirectX?

I am playing GTA SA:MP. It uses DirectX, and if you are AFK on the server you get automatically kicked, and I would like to let it AFK on the server while I'm left.
I tried creating a VBScript that automatically after 8 minutes presses W and then loops but it actually doesn't work. The player doesn't move.
Code:
Dim a
Dim antiafk
set antiafk=createobject("wscript.shell")
msgbox "Atentie! Ca sa opriti scriptul, trebuie sa il opriti NUMAI din Task Manager. Nu exista alta cale."
Do until a=5
wscript.sleep 480000
antiafk.sendkeys "w"
Loop
Any ideas?
SendKeys method sends one or more keystrokes to the active window. Use AppActivate method to activate your application window first.
The script is a simplified version of your one, with no 8 minutes loop and notepad.exe instead of your application name.
option explicit
On Error Goto 0
Dim antiafk
set antiafk=createobject("wscript.shell")
Dim lngPID, booSuccess, foo
lngPID = getlngPID()
If lngPID = 0 Then
foo = antiafk.Popup( "No instance found", 5, _
"29445451", vbOKOnly + vbCritical)
Else
booSuccess = antiafk.AppActivate( lngPID)
If not booSuccess Then
foo = antiafk.Popup( "AppActivate Method unsuccesfull", 5, _
"29445451", vbOKOnly + vbExclamation)
Else
antiafk.sendkeys "w"
End If
End If
Function getLngPID()
getLngPID = 0
Dim strComputer
Dim objWMIService, colProcessList, objProcess
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = 'Notepad.exe'")
'find appropriate and suitable Process ID
For Each objProcess in colProcessList
getLngPID = objProcess.ProcessId
Next
End Function

Output Vbscript info to txt file

Newbie question - I have here a VBScript that looks for an Upgrade Code, and based on that finds the Product Codes for the specified Upgrade Code. The Upgrade Code is always the same, but Product Code changes from version to version, and that can make uninstalling software troublesome. And no, I didn't make this script myself.
This script works very well, but I'd like to make it output all the product codes it found to a text file. I've looked on Google for hours, found some clues, but I've not been able to make it work. Always turns up with a blank text file.
Here's the code:
strComputer = "."
Set WshShell = CreateObject("Wscript.Shell")
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
On Error Resume Next
Set colSoftware = objWMIService.ExecQuery _
("Select * from Win32_Property Where Property = 'UpgradeCode'")
For Each objSoftware in colSoftware
If objSoftware.Value = "{BCCCB25E-C6A6-4340-9018-DA0FB34AF226}" Then
strCMD = "MsiExec.exe /x " & objSoftware.ProductCode & " /qn"
objExec = WshShell.Run(strCMD,1,True)
If objExec <> 0 Then
WScript.Quit objExec
End If
End If
Next
WScript.Quit 0
How do I output objSoftware.ProductCode to a text file? Or do I need to output something else to get the Product Codes I'm looking for?
The easy way to write text to a file is to WScript.Echo the desired info and run the script like cscript x.vbs > output.txt.
If that seems to pedestrian, start your research here.
Played around with it, Googled it, and I found a solution that works for me. This prints out all the Product Codes on your computer based on the specificed Upgrade Code. Here's the script:
strComputer = "."
Set WshShell = CreateObject("Wscript.Shell")
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
On Error Resume Next
Set colSoftware = objWMIService.ExecQuery _
("Select * from Win32_Property Where Property = 'UpgradeCode'")
For Each objSoftware in colSoftware
If objSoftware.Value = "{BCCCB25E-C6A6-4340-9018-DA0FB34AF226}" Then
Wscript.Echo objSoftware.ProductCode
strCMD = "MsiExec.exe /x " & objSoftware.ProductCode & " /qn"
objExec = WshShell.Run(strCMD,1,True)
If objExec <> 0 Then
WScript.Quit objExec
End If
End If
Next
WScript.Quit 0
Running //NoLogo scriptname.vbs > log.txt in command prompt, gives me a txt file with all the product codes for the upgrade code specified.
Please note this code also uninstalls the software afterwards.

Vbscript rename a remote folder

I'm working on a script that renames a folder on a remote pc. But it it's not working.
Iff I execute the script nothing happens. I use a modified version of the Hey Scripting Guy blog. If I use normal pathnames (c:\data) instead of remotepath names (\\"& strcomputer &"C$\data) it works. But if i use remote pathnames nothing happens.
Do you guys know whats wrong?
strComputer = "hostname"
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colFolders = objWMIService.ExecQuery _
("Select * From Win32_Directory Where Name = '\\\\"& strComputer &"C$\\Data'")
For Each objFolder in colFolders
strNewName = objFolder.Name & ".old"
objFolder.Rename strNewName
Next
When you connect with WMI you don't use a UNC path with Win32_Directory (since it's local to that WMI repository).
So use ("Select * From Win32_Directory Where Name = 'C:\\Data'")
You should be able to accomplish your task using the FileSystemObject...
strComputer = "hostname"
strFolderName = "\\"& strComputer &"\C$\Data"
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists(strFolderName) Then
Set objFolder = objFSO.GetFolder(strFolderName)
strNewName = objFolder.Name & ".old"
objFolder.Name = strNewName
End If

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

Resources