Vbscript to find critical process - vbscript

I wrote a vbscript to find the critical process details from server.I have few process name if that process exists then the script should give the mentioned output.But the problem here is even though if the process exist or not exist the script give same output as "CriticalProcesses=NA". Can anyone help to fix this ? Any help is much appreciated
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Process", "WQL", _
wbemFlagReturnImmediately + wbemFlagForwardOnly)
For Each objItem In colItems
'WScript.Echo "Caption: " & objItem.Caption
Dim process
Process = objItem.Caption
Next
If inStr(process, "SQLserver") Or inStr(process, "mysql") then
wscript.echo "CriticalProcesses=Database"
else if inStr(process, "java") Or inStr(process, "weblogic") Then
wscript.echo "CriticalProcesses=wls"
else
wscript.echo "CriticalProcesses=NA"
End if
End if

The problem here is that you are iterating through your processes in a for loop and then you test the process name outside of the loop. So your If instr(process.. bit only checks the last process found from your for loop.
To fix, stick the if test inside your loop, so it can test each process it finds.
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Process", "WQL", _
wbemFlagReturnImmediately + wbemFlagForwardOnly)
For Each objItem In colItems
'WScript.Echo "Caption: " & objItem.Caption
Dim process
Process = objItem.Caption
If inStr(process, "SQLserver") Or inStr(process, "mysql") Then
wscript.echo "CriticalProcesses=Database"
ElseIF inStr(process, "java") Or inStr(process, "weblogic") Then
wscript.echo "CriticalProcesses=wls"
Else
'wscript.echo "CriticalProcesses=NA"
End If
Next
I've also changed your Else If to an ElseIf and removed the unnecessary second End If.
Lastly, I commented out that echo in your Else since you probably don't want the script to Echo NA over and over and over again. At least... I wasn't to hip on pressing Enter 200 times.
To capture whether a wls or database type process is running and then only echo once at the end, you can capture the true/false for each type in a boolean variable, then test after the for loop:
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Process", "WQL", _
wbemFlagReturnImmediately + wbemFlagForwardOnly)
Dim hasDBProcess
Dim hasWLSProcess
hasDBProcess = False
hasWLSProcess = False
For Each objItem In colItems
'WScript.Echo "Caption: " & objItem.Caption
Dim process
Process = objItem.Caption
If inStr(process, "SQLserver") Or inStr(process, "mysql") Then
hasDBProcess = True
ElseIF inStr(process, "java") Or inStr(process, "weblogic") Then
hasWLSProcess = True
End If
Next
If hasDBProcess Then
wscript.echo "CriticalProcess=Database"
ElseIf hasWLSProcess Then
wscript.echo "CriticalProcess=wls"
Else
wscript.echo "CriticalProcess=NA"
End if

Related

Remotely Killing a process in 1 shot

I have this code to remotely kill a certain process on 5 servers. My problem is that i want to cut it short and kill the process in all servers in 1 click. The code now asks if i am sure i want to kill the process, then kills the process, then asks if i want to proceed to the next server. i would like to just have a messge saying, "Killing process on all servers" and done
`Option Explicit
Dim objWMIService, objProcess, colProcess
Dim strProcessKill, item
Dim strComputer(4)
Dim ans, killFLag
strComputer(0) = "server1"
strComputer(1) = "server2"
strComputer(2) = "server3"
strComputer(3) = "server4"
strComputer(4) = "server5"
strProcessKill = "'notepad.exe'"
For item = 0 To 4 Step 1
if MsgBox("Are you sure you want to kill " & strProcessKill & " on " & strCOmputer(item) & "?" , vbYesNo + vbQuestion) = vbYes then
killFLag = 1
end if
if KillFlag = 1 then
msgbox "KILLING PROCESS ON" & strComputer(item)
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer(item) & "\root\cimv2")
Set colProcess = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = " & strProcessKill)
For Each objProcess in colProcess
objProcess.Terminate()
Next
WSCript.Echo "Just killed process " & strProcessKill & " on " & strComputer(item)
else
WScript.Quit
end if
Next`
Move the for loop down so that you are not prompted for each server. Something like this should work
Option Explicit
Dim objWMIService, objProcess, colProcess
Dim strProcessKill, item
Dim strComputer(4)
Dim ans, killFLag
strComputer(0) = "server1"
strComputer(1) = "server2"
strComputer(2) = "server3"
strComputer(3) = "server4"
strComputer(4) = "server5"
strProcessKill = "'notepad.exe'"
if MsgBox("Are you sure you want to kill " & strProcessKill & " on all servers?" , vbYesNo + vbQuestion) = vbYes then
killFLag = 1
end if
if KillFlag = 1 then
msgbox "KILLING PROCESS ON ALL SERVERS"
For item = 0 To 4 Step 1
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer(item) & "\root\cimv2")
Set colProcess = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = " & strProcessKill)
For Each objProcess in colProcess
objProcess.Terminate()
Next
next
msgBox "Done"
else
WScript.Quit
end if

Getting Volume Serial Number Of Script Location

here is a vbscript which shows all drives' volume serial numbers. But I need to customize to return only the volume serial number of the drive which the script is running from.
How to do it?
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
str = ""
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_LogicalDisk")
For Each objItem In colItems
str = str & objItem.Name & " SerialNumber: " & objItem.VolumeSerialNumber & vbCrlf & vbCrlf
Next
MsgBox str
This should do what you need:
' Get the drive designator...
With CreateObject("Scripting.FileSystemObject")
strDrive = .GetDriveName(WScript.ScriptFullName)
End With
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_LogicalDisk WHERE DeviceId='" & strDrive & "'")
' There should only be one item in the collection...
For Each objItem In colItems
MsgBox objItem.Name & " SerialNumber: " & objItem.VolumeSerialNumber
Next

VBscript to get info from pc

Hello I´m writing this script where i need Serial number, IP address, login user, produkt key from office, autoroute to a text file. I have begun to write a script but having some issues. Not a done script but im kinda stuck to here.
Here´s the script:
On Error Resume Next
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
arrComputers = Array("localhost")
For Each strComputer In arrComputers
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService1.ExecQuery("SELECT * FROM Win32_ComputerSystemProduct", "WQL", _ wbemFlagReturnImmediately + wbemFlagForwardOnly)
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService2.ExecQuery("SELECT * FROM Win32_ComputerSystem", "WQL", _ wbemFlagReturnImmediately + wbemFlagForwardOnly)
Set objWMIService = GetObject( "winmgmts:\\" & strCoputer & ".\root\CIMV2" )
Set colItems = objWMIService.ExecQuery( "SELECT * FROM Win32_NetworkAdapterConfiguration", "WQL", _ wbemFlagReturnImmediately + wbemFlagForwardOnly)
next
For Each objItem In colItems2
Dim fso, tf
Set fso = CreateObject("Scripting.FileSystemObject")
Set tf = fso.CreateTextFile ("c:\" & objItem.UserName & ".txt", True)
tf.Write "Username: " & objItem.UserName
tf.WriteBlankLines(1)
tf.Write "Hostname: " & objItem.Name
tf.WriteBlankLines(1)
tf.Write "Domain: " & objItem.Domain
tf.WriteBlankLines(2)
next
For Each objItem In colItems1
tf.Write "Serial: " & objItem.IdentifyingNumber
tf.WriteBlankLines(1)
tf.Write "Model: " & objItem.Name
tf.WriteBlankLines(2)
tf.Write "Vendor: " & objItem.Vendor
tf.WriteBlankLines(1)
tf.Write "Version: " & objItem.Version
tf.WriteBlankLines(2)
Next
For Each objItem In colItems3
tf.Write "IP Address: " & objItem.IPAddress
tf.WriteBlankLines(1)
tf.Close
next
Microsoft has created a free tool called Scriptomatic which automatically creates vbscript code for such kind of task. You can download scriptomatic from the below location.
http://www.microsoft.com/en-in/download/details.aspx?id=12028

searching arrays with vbs

I'm trying to search through an array for a printer and if the printer exists display the name in the HTA. That bit works ok, but when no printer is found in the array all the installed printers on the device are displayed. is there a way to only show printers that are found
Set objFSO = CreateObject("Scripting.FileSystemObject")
arrPrinters = Split(objFSO.OpenTextFile("C:\Windows\DEW\denied-printers.txt" ,ForReading).ReadAll(), VbCrLf)
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery("Select * from Win32_Printer")
For Each objPrinter in colInstalledPrinters
localprinter = objPrinter.Name
For Each strLine in arrPrinters
If inStr(localprinter,strLine) > 0 Then
strHTML = strHTML & "<tr><td>" & localprinter & "</td></tr>"
End If
Next
Next
try this
If inStr(localprinter,strLine) > 0 OR inStr(localprinter,strLine) = NULL Then
End If
the problem is if array is empty, strline is NULL and when you used it in inStr, it returns NULL instead of '0'. That is one possibility – tunmise fasipe 3 mins ago edit
Your main problem - i guess without knowing the contents of yoyr file - is that in instr(textToSearch, searchString) you switch the two parameters .
Anyway, here a version of your code i tested.
const ForReading = 1
strComputer = "."
set objFSO = createObject("Scripting.FileSystemObject")
printers = objFSO.OpenTextFile("denied-printers.txt" ,ForReading).ReadAll()
set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
set colInstalledPrinters = objWMIService.ExecQuery("Select * from Win32_Printer")
for Each objPrinter in colInstalledPrinters
localprinter = objPrinter.Name
if instr(printers, localprinter) then
strHTML = strHTML & "<tr><td>" & localprinter & "</td></tr>"
end if
next
EDIT: here the stand alone vbscript version, save it to a .vbs file and run to test
on error resume next
const ForReading = 1
strComputer = "."
file = "denied-printers.txt"
set objFSO = createObject("Scripting.FileSystemObject")
set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
set colInstalledPrinters = objWMIService.ExecQuery("Select * from Win32_Printer")
if err.number=0 then
printers = objFSO.OpenTextFile(file ,ForReading).ReadAll()
if err.number=0 then
set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
set colInstalledPrinters = objWMIService.ExecQuery("Select * from Win32_Printer")
for Each objPrinter in colInstalledPrinters
localprinter = objPrinter.Name
if instr(printers, localprinter) then
wscript.echo localprinter & " found in " & file
end if
next
else
wscript.echo "file " & file & " not found, showing all printers"
for Each objPrinter in colInstalledPrinters
wscript.echo objPrinter.Name
next
end if
else
wscript.echo "Error" & err.description
end if

Obtaining process that launched (for instance) iexplore with VBScript/JScript

Is there a way to (ideally with a scripting language like VBScript / JScript) get details of a process that spawned a different program i.e., In the case when Computrace LoJack launches iexplore, to handle communications with the internet?
You can use WMI to check the ParentProcessId for the process you are interested in. In the case of "normal" user mode applications, the parent process should be explorer.exe.
strProcess = "iexplore.exe"
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colProcesses = objWMIService.ExecQuery("Select * from Win32_Process " _
& " Where name = '" & strProcess & "'")
For Each objProcess in colProcesses
WScript.Echo objProcess.ParentProcessId
Next
In the case of Internet Explorer, make sure you check for the ID of IE as well since it will spawn multiple instances of itself. Try something like this:
strProcess = "iexplore.exe"
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colProcesses = objWMIService.ExecQuery("Select * from Win32_Process " _
& " Where name = 'explorer.exe' OR name = 'iexplore.exe'")
i = 0
arrIds = Array()
For Each objProcess in colProcesses
ReDim Preserve arrIds(i)
arrIds(i) = objProcess.ProcessId
i = i + 1
Next
Set colProcesses = objWMIService.ExecQuery("Select * from Win32_Process " _
& " Where name = '" & strProcess & "'")
For Each objProcess in colProcesses
intParentID = objProcess.ParentProcessId
blnIsFound = False
For Each intID in arrIds
If intID = intParentID Then
blnIsFound = True
Exit For
End If
Next
If blnIsFound = False Then
WScript.Echo "Process " & objProcess.ProcessId & " spawned by process " & objProcess.ParentProcessId
End If
Next

Resources