• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 652
  • Last Modified:

Monitor new Process on remote servers

Hi All,

I have a script below that should monitor when a notepad process is created. The script works fine on local and remote machines. However the problem I have is that the script tends to wait on command objEventSource.NextEvent().

This particular command is causing issues as the script waits on each machine in the loop to complete the task before moving on.

My objective is to trap the notepad process when it is initiated on any of the machines in the process.txt file. IE, I want the script to run across all machines when I push it out from a batch file using psexec.

Can anyone please help move past this huddle? or Suggest a better solution. I tried another approach by using vbscript to copy the file to the servers and was faced with this same problem and security issues. Please help

Run Remote Script
Dim objController, objRemoteScript, objFSO
Dim objTSIn, objTSOut, strComputer

'Create objects
Set objController = CreateObject("WshController")
Set objFSO = CreateObject("Scripting.FileSystemObject")

'Read computer names from file
Set objTSIn = objFSO.OpenTextFile("C:\computers.txt")

'Open output file for log
Set objTSOut = objFSO.CreateTextFile("C:\log.txt", True)

Do Until objTSIn.AtEndOfStream
    'Deploy script to remote computer

    strComputer = objTSIn.ReadLine
    objTSOut.WriteLine strComputer & ": Deploying at " & Now
    Set objRemoteScript = objController.CreateScript( _
        "C:\process.vbs", strComputer)
    WScript.ConnectObject objRemoteScript, "remote_"
    Do Until objRemoteScript.Status = 1
        WScript.Sleep 1000
    objTSOut.WriteLine strComputer & " Completed at " & Now
    Set objRemoteScript = Nothing


WScript.Echo "Deployment script complete."

Sub remote_Error
    Dim objError
    Set objError = objRemote.Error
    objTSOut.WriteLine strComputer & ": Error at " & Now
    objTSOut.WriteLine " Line: " & objError.Line & _
        ", Char: " & objError.Character & vbCrLf & _
            "Description: " & objError.Description
    WScript.Quit -1
End Sub
Get notepad Process
'Get computer name
Set WshNetwork = WScript.CreateObject("WScript.Network")
'Create a FileSystemObject
Set oFS = CreateObject("Scripting.FileSystemObject")
'Open a text file of computer names
'with one computer name per line
Set oTS = oFS.OpenTextFile("c:\process.txt")
'go through the text file
Do Until oTS.AtEndOfStream
 'get the next computer name
 'store it in variable strComputer
 strComputer = oTS.ReadLine
'strComputer = "."
Set objSWbemServices = GetObject("winmgmts:" &_
 "{impersonationLevel=impersonate}!" &_
 "\\" & strComputer & "\root\cimv2")
Set objEventSource = objSWbemServices.ExecNotificationQuery( _
 "SELECT * FROM __InstanceCreationEvent " &_
 "WITHIN 10 " &_
 "WHERE TargetInstance " &_
 "ISA 'Win32_Process' " &_
 "AND TargetInstance.Name = 'notepad.exe'")
'Echo Computer name
Set objEventObject = objEventSource.NextEvent()
Wscript.Echo "Computer Name: " &  strComputer
Set objEmail = CreateObject("CDO.Message")
objEmail.From = "me@test.com"
objEmail.To = "me@test.com"
objEmail.Subject = strComputer 
objEmail.Textbody = "Notepad initiated."
objEmail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _
objEmail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
'close the text file
'MsgBox "Done!"

Open in new window

  • 2
  • 2
1 Solution
Try this...
This script uses an event sink which monitors the processes.  This will not have any hang-ups as mentioned in the script above but will happily monitor endlessly.
Near the top of the script is a dictionary object so that you can monitor multiple files - why limit your script right?  In this case, the script will monitor notepad and calc.  Add more entries or take out the line, it is up to you.
My thinking is that you would copy the file onto each drive as described.  Then you would schedule the script to run at the system start up.  This way, the user would never see the script start and the script would catch any application you tell it to monitor right from the get go.

Dim ScanSpeed
Dim strComputer
Dim objDictionary
Set WshNetwork = WScript.CreateObject("WScript.Network")
strComputer = WshNetwork.ComputerName 
ScanSpeed = 5
Set objDictionary = CreateObject("scripting.dictionary")
objDictionary.Add "notepad.exe", "notepad.exe"
objDictionary.Add "calc.exe", "calc.exe"
Set ProcessSink = WScript.CreateObject("WbemScripting.SWbemSink", "ProcessSink_")
Set Process = GetObject("winMgmts:{(security)}\\" & strComputer & "\root\cimv2")
Process.ExecNotificationQueryAsync ProcessSink, "select * from __InstanceCreationEvent WITHIN " & ScanSpeed & " where targetinstance isa 'Win32_Process'"
Sub ProcessSink_OnObjectReady(objObject, objAsyncContext)
    strName = objObject.TargetInstance.Name
    strPath = objObject.TargetInstance.ExecutablePath
    if objDictionary.Exists(lcase(strName)) then
        Set objEmail = CreateObject("CDO.Message")
        objEmail.From = "me@test.com"
        objEmail.To = "me@test.com"
        objEmail.Subject = strComputer 
        objEmail.Textbody = strName & " initiated."
        objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
        objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "Testserver" 
        objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
    end if
End Sub
wscript.echo "Click OK to stop monitor."

Open in new window

neosteoAuthor Commented:
Hi Rejoinder,

Thanks a lot for the script. Its much better than mine. What I am trying to do is run the script remotely? With the echo in their, the script is also waiting for the job to complete. I am pushing out the script using the command below. These are production servers which I can't afford to reboot for the startup script to kick off. I am trying to track a defrag application, I used notepad as an example because I can play with this process without launching the defragmentation tool.

I really need to run it without schedulling it. If you can help with this, It will be much appreciated.

for /f %i in (c:\computers.txt) do @echo %i: && xcopy c:\process.vbs \\%i\c$\ /i /y /r /q
for /f %i in (c:\computers.txt) do @echo %i: && xcopy c:\process.bat \\%i\c$\ /i /y /r /q

psexec.exe @computers.txt c:\process.bat
neosteoAuthor Commented:
Hi Rejoinder

Thanks, I eventually used my script as the one stated above prompted for user input. However, I got an idea to create a scheduled task that will run it from the network. Thanks for your help.

I hope everything works out then and thanks for the points.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now