Monitoring a process on a server....

Discussion in 'Scripting' started by dnec, Dec 20, 2007.

  1. dnec

    dnec Guest

    I've got a script I'm working on but I've run into an error. I'm trying to
    monitor a process on remote servers (its the same process for all three), and
    if the process ever ends I'd like this script to run a command line email
    program called Postie. Notifying me the process has stopped.

    Unfortunately I've got to embed the credentials within the script. I'm
    currently logged on as myself under XP (trying to develop the script), not
    the user embedded within the script. Once I get the script working I'll
    transfer it to a WIN2K3 server and, using Windows Task Scheduler, have it run
    intermittently throughout the day. The credentials that are embedded ARE
    local admins on the respective servers.

    Here's the code that's been developed so far:

    ====================

    ' Create Objects
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objShell = CreateObject("Wscript.Shell")

    ' Create Variables
    arrComputer = array("Computer1","Computer2","Computer3")
    strProcess = "Process.exe"
    strUser = "domain\username"
    strPassword = "respective_password"
    strResults = ""
    strResultsFile = "C:\Windows\output.txt"

    ' Process Each Computer
    For Each Computer in arrComputer
    Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
    Set objWMIService = objSWbemLocator.ConnectServer(strComputer, _
    "root\CIMV2", strUser, strPassword, "MS_409", "NTLMDomain:" +
    strDomain)
    Set colProcesses = objWMIService.ExecQuery _
    ("Select * from Win32_Process Where Name = '" & strprocess & "'")

    ' Only Log if Process Not Running
    If colProcesses.Count = 0 Then
    strResults = strResults & Computer & "," & strProcess & "," & " Not
    Running" & vbCrLf
    End If
    Next

    'Write Process Results to File
    Set logFile = objFSO.CreateTextFile(strResultsFile, True)
    logFile.WriteLine strResults
    logFile.Close

    'Email Results (Modify this line as needed, if you can send a text body
    ' as a parameter, then you won't need to create the results
    file)
    objShell.Run "postie -host:mailserver.com -from:fred@here -to:joe@there
    -s:processResults -nomsg -a:" & strResultsFile

    ====================

    However, when I try to run it I get the following error message:

    Script: c:\documents and settings\user\desktop\scripts\XXXXXX.vbs

    Line: 16
    Char: 5
    Error: User credentials cannot be used for local connections
    Code: 80041064
    Source: SWbemLocator

    I'm not entirely sure why I'm getting this error, so any help would be
    greatly appreciated.
    thx.
     
    dnec, Dec 20, 2007
    #1
    1. Advertisements

  2. Having the statement "option explicit" at the start of your code
    would be a wonderful thing. It would tell you immediately that
    you're using the variable "Computer" in one place and "strComputer"
    in another, even though the two refer to one and the same thing.
     
    Pegasus \(MVP\), Dec 20, 2007
    #2
    1. Advertisements

  3. dnec

    dnec Guest

    ok. I'll be sure to make remember that. Another source seems to indicate
    that you cannot impersonate to the local computer with WMI. You can only
    impersonate to a remote host. However, I'm not exactly sure what that means
    and I'm not sure where to make a change if necessary.
    Thanks.
     
    dnec, Dec 20, 2007
    #3
  4. Maybe I did not express myself clearly. This is what your code should
    look like. Spot the difference!

    ' Create Objects
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objShell = CreateObject("Wscript.Shell")

    ' Create Variables
    arrComputer = array("Computer1","Computer2","Computer3")
    strProcess = "Process.exe"
    strUser = "domain\username"
    strPassword = "respective_password"
    strResults = ""
    strResultsFile = "C:\Windows\output.txt"

    ' Process Each Computer
    For Each strComputer in arrComputer
    Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
    Set objWMIService = objSWbemLocator.ConnectServer(strComputer, _
    "root\CIMV2", strUser, strPassword, "MS_409", "NTLMDomain:" +
    strDomain)
    Set colProcesses = objWMIService.ExecQuery _
    ("Select * from Win32_Process Where Name = '" & strprocess & "'")

    ' Only Log if Process Not Running
    If colProcesses.Count = 0 Then
    strResults = strResults & Computer & "," & strProcess & "," & " Not
    Running" & vbCrLf
    End If
    Next

    'Write Process Results to File
    Set logFile = objFSO.CreateTextFile(strResultsFile, True)
    logFile.WriteLine strResults
    logFile.Close

    'Email Results (Modify this line as needed, if you can send a text body
    ' as a parameter, then you won't need to create the results
    file)
    objShell.Run "postie -host:mailserver.com -from:fred@here -to:joe@there
    -s:processResults -nomsg -a:" & strResultsFile
    ==================
    If you run the code on the local machine then you must not supply
    a user name or password.
     
    Pegasus \(MVP\), Dec 20, 2007
    #4
  5. dnec

    dnec Guest

    ahhh....I spotted the difference.

    Now its working.

    Thanks very much for your patience.
     
    dnec, Dec 21, 2007
    #5
  6. dnec

    Eric Guest

    There is a free monitoring tool you could use http://www.real-user-monitoring.com



    dne wrote:

    Monitoring a process on a server....
    20-Dec-07

    I've got a script I'm working on but I've run into an error. I'm trying to
    monitor a process on remote servers (its the same process for all three), and
    if the process ever ends I'd like this script to run a command line email
    program called Postie. Notifying me the process has stopped.

    Unfortunately I've got to embed the credentials within the script. I'm
    currently logged on as myself under XP (trying to develop the script), not
    the user embedded within the script. Once I get the script working I'll
    transfer it to a WIN2K3 server and, using Windows Task Scheduler, have it run
    intermittently throughout the day. The credentials that are embedded ARE
    local admins on the respective servers.

    Here's the code that's been developed so far:

    ====================

    ' Create Objects
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objShell = CreateObject("Wscript.Shell")

    ' Create Variables
    arrComputer = array("Computer1","Computer2","Computer3")
    strProcess = "Process.exe"
    strUser = "domain\username"
    strPassword = "respective_password"
    strResults = ""
    strResultsFile = "C:\Windows\output.txt"

    ' Process Each Computer
    For Each Computer in arrComputer
    Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
    Set objWMIService = objSWbemLocator.ConnectServer(strComputer, _
    "root\CIMV2", strUser, strPassword, "MS_409", "NTLMDomain:" +
    strDomain)
    Set colProcesses = objWMIService.ExecQuery _
    ("Select * from Win32_Process Where Name = '" & strprocess & "'")

    ' Only Log if Process Not Running
    If colProcesses.Count = 0 Then
    strResults = strResults & Computer & "," & strProcess & "," & " Not
    Running" & vbCrLf
    End If
    Next

    'Write Process Results to File
    Set logFile = objFSO.CreateTextFile(strResultsFile, True)
    logFile.WriteLine strResults
    logFile.Close

    'Email Results (Modify this line as needed, if you can send a text body
    ' as a parameter, then you won't need to create the results
    file)
    objShell.Run "postie -host:mailserver.com -from:fred@here -to:joe@there
    -s:processResults -nomsg -a:" & strResultsFile

    ====================

    However, when I try to run it I get the following error message:

    Script: c:\documents and settings\user\desktop\scripts\XXXXXX.vbs

    Line: 16
    Char: 5
    Error: User credentials cannot be used for local connections
    Code: 80041064
    Source: SWbemLocator

    I'm not entirely sure why I'm getting this error, so any help would be
    greatly appreciated.
    thx.

    Previous Posts In This Thread:

    Monitoring a process on a server....
    I've got a script I'm working on but I've run into an error. I'm trying to
    monitor a process on remote servers (its the same process for all three), and
    if the process ever ends I'd like this script to run a command line email
    program called Postie. Notifying me the process has stopped.

    Unfortunately I've got to embed the credentials within the script. I'm
    currently logged on as myself under XP (trying to develop the script), not
    the user embedded within the script. Once I get the script working I'll
    transfer it to a WIN2K3 server and, using Windows Task Scheduler, have it run
    intermittently throughout the day. The credentials that are embedded ARE
    local admins on the respective servers.

    Here's the code that's been developed so far:

    ====================

    ' Create Objects
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objShell = CreateObject("Wscript.Shell")

    ' Create Variables
    arrComputer = array("Computer1","Computer2","Computer3")
    strProcess = "Process.exe"
    strUser = "domain\username"
    strPassword = "respective_password"
    strResults = ""
    strResultsFile = "C:\Windows\output.txt"

    ' Process Each Computer
    For Each Computer in arrComputer
    Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
    Set objWMIService = objSWbemLocator.ConnectServer(strComputer, _
    "root\CIMV2", strUser, strPassword, "MS_409", "NTLMDomain:" +
    strDomain)
    Set colProcesses = objWMIService.ExecQuery _
    ("Select * from Win32_Process Where Name = '" & strprocess & "'")

    ' Only Log if Process Not Running
    If colProcesses.Count = 0 Then
    strResults = strResults & Computer & "," & strProcess & "," & " Not
    Running" & vbCrLf
    End If
    Next

    'Write Process Results to File
    Set logFile = objFSO.CreateTextFile(strResultsFile, True)
    logFile.WriteLine strResults
    logFile.Close

    'Email Results (Modify this line as needed, if you can send a text body
    ' as a parameter, then you won't need to create the results
    file)
    objShell.Run "postie -host:mailserver.com -from:fred@here -to:joe@there
    -s:processResults -nomsg -a:" & strResultsFile

    ====================

    However, when I try to run it I get the following error message:

    Script: c:\documents and settings\user\desktop\scripts\XXXXXX.vbs

    Line: 16
    Char: 5
    Error: User credentials cannot be used for local connections
    Code: 80041064
    Source: SWbemLocator

    I'm not entirely sure why I'm getting this error, so any help would be
    greatly appreciated.
    thx.

    Re: Monitoring a process on a server....

    Having the statement "option explicit" at the start of your code
    would be a wonderful thing. It would tell you immediately that
    you're using the variable "Computer" in one place and "strComputer"
    in another, even though the two refer to one and the same thing.

    ok. I'll be sure to make remember that.
    ok. I'll be sure to make remember that. Another source seems to indicate
    that you cannot impersonate to the local computer with WMI. You can only
    impersonate to a remote host. However, I'm not exactly sure what that means
    and I'm not sure where to make a change if necessary.
    Thanks.

    :

    Maybe I did not express myself clearly. This is what your code shouldlook like.
    Maybe I did not express myself clearly. This is what your code should
    look like. Spot the difference!

    ' Create Objects
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objShell = CreateObject("Wscript.Shell")

    ' Create Variables
    arrComputer = array("Computer1","Computer2","Computer3")
    strProcess = "Process.exe"
    strUser = "domain\username"
    strPassword = "respective_password"
    strResults = ""
    strResultsFile = "C:\Windows\output.txt"

    ' Process Each Computer
    For Each strComputer in arrComputer
    Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
    Set objWMIService = objSWbemLocator.ConnectServer(strComputer, _
    "root\CIMV2", strUser, strPassword, "MS_409", "NTLMDomain:" +
    strDomain)
    Set colProcesses = objWMIService.ExecQuery _
    ("Select * from Win32_Process Where Name = '" & strprocess & "'")

    ' Only Log if Process Not Running
    If colProcesses.Count = 0 Then
    strResults = strResults & Computer & "," & strProcess & "," & " Not
    Running" & vbCrLf
    End If
    Next

    'Write Process Results to File
    Set logFile = objFSO.CreateTextFile(strResultsFile, True)
    logFile.WriteLine strResults
    logFile.Close

    'Email Results (Modify this line as needed, if you can send a text body
    ' as a parameter, then you won't need to create the results
    file)
    objShell.Run "postie -host:mailserver.com -from:fred@here -to:joe@there
    -s:processResults -nomsg -a:" & strResultsFile
    ==================
    If you run the code on the local machine then you must not supply
    a user name or password.



    ahhh....I spotted the difference.Now its working.
    ahhh....I spotted the difference.

    Now its working.

    Thanks very much for your patience.


    :


    Submitted via EggHeadCafe - Software Developer Portal of Choice
    LINQ With Strings
    http://www.eggheadcafe.com/tutorial...47db-adb9-db7fe2c6ab8c/linq-with-strings.aspx
     
    Eric, May 25, 2010
    #6
  7. dnec

    Peter Foldes Guest

    Deadhead from egghead answering to a 3yr old post

    --
    Peter

    Please Reply to Newsgroup for the benefit of others
    Requests for assistance by email can not and will not be acknowledged.
    http://www.microsoft.com/protect
     
    Peter Foldes, May 26, 2010
    #7
  8. From: "Peter Foldes" <>

    | Deadhead from egghead answering to a 3yr old post

    Hey, don't degrade Dead-Heads like that.
     
    David H. Lipman, May 27, 2010
    #8
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.