Announcement

Collapse
No announcement yet.

How many of one process are running?

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • How many of one process are running?

    Hi,

    I need a script to output the amount of processes of a particular running at one time. We have a terminal server and we are experiencing problems with the licensing of a particular programs............So basically I have to go to task manager and then count the number of processes of eg x.exe....

    at cmd if i run tasklist i get a read out, but if there is any way of actually running a script and getting a read out of eg "there are __ amount of this process running"..

    i have done this before in a linux environment but never in windows.

    Any ideas will be appreciated, thanks.

  • #2
    Re: How many of one process are running?

    Here you go... If you don't want a prompt set sProcessName = to the process you want to check and run it.

    Code:
    sProcessName = Trim(InputBox("Please enter the process name including" _ 
    & "extension."))
    
    sComputer = "." ' use "." for local computer
    Set oWmi = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & sComputer & "\root\cimv2")
    
    Set colProcessList = oWmi.ExecQuery _
    ("Select * from Win32_Process Where Name = '" & sProcessName & "'")
    
    iCount = colProcessList.Count
    
    If iCount > 0 Then
      If iCount = 1 Then
        wscript.echo "There is 1 instance of [" & sProcessName & "] running."
      Else
        wscript.echo "There are " & iCount & " instances of [" & sProcessName _
        & "] running."
      End If
    Else
      wscript.echo "The process [" & sProcessName & "] is not running."
    End If
    Last edited by ahinson; 13th February 2007, 06:12.
    Andrew

    ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

    Comment


    • #3
      Re: How many of one process are running?

      outstanding ...thanks so much.



      now perhaps asking for too much.............can i do this somehow.

      have a similar script that will output it to a file.......then i'd just schedule that to run every hour or so and append it..


      thanks heaps
      Last edited by dublin_101; 13th February 2007, 06:41.

      Comment


      • #4
        Re: How many of one process are running?

        Originally posted by dublin_101 View Post
        ...have a similar script that will output it to a file.......then i'd just schedule that to run every hour or so and append it..
        Sure but you'll need to know the process name to check each time.

        Are you always going to check the same process?

        If so, its simple.

        Just append a text file with the count when the scheduled process runs.

        Code:
        If iCount > 0 Then
           WriteLineToFile sProcessName, iCount
        End If
        
        Function WriteLineToFile(sProcName, iProcCount)
        
           Const ForReading = 1, ForWriting = 2, ForAppending = 8
           Dim fso, f
           Set fso = CreateObject("Scripting.FileSystemObject")
           Set f = fso.OpenTextFile("c:\users\ProcessLog.txt", ForAppending, True)
        
           f.Write Now() & " - " & sProcName & " [" & iProcCount & "]" & vbCRLF
        
        End Function
        Last edited by ahinson; 14th February 2007, 00:03.
        Andrew

        ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

        Comment


        • #5
          Re: How many of one process are running?

          Originally posted by ahinson View Post
          Sure but you'll need to know the process name to check each time.

          Are you always going to check the same process?

          If so, its simple.

          Just append a text file with the count when the scheduled process runs.
          the file name is psi.exe


          sProcessName = "psi.exe"

          this will give me a popup window with the read out..................if i run from cmd, such as x.vbs > x.txt it will not print to the text file. do i have to do this in the script instead? if so, can you please tell me how? thanks.

          initially i just thought i could do eg every hour run x.vbs >> running.txt and on the text file have a read out like, time: 1400 running processes 25

          is this achievable...

          thank you very much
          Last edited by dublin_101; 14th February 2007, 00:37.

          Comment


          • #6
            Re: How many of one process are running?

            Originally posted by dublin_101 View Post
            the file name is psi.exe


            sProcessName = "psi.exe"

            this will give me a popup window with the read out..................if i run from cmd, such as x.vbs > x.txt it will not print to the text file. do i have to do this in the script instead? if so, can you please tell me how? thanks.

            initially i just thought i could do eg every hour run x.vbs >> running.txt and on the text file have a read out like, time: 1400 running processes 25

            is this achievable...

            thank you very much
            I'm sorry but I don't quite understand what you mean/want. I'm guessing the appended log file isn't going to work for you?
            Andrew

            ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

            Comment


            • #7
              Re: How many of one process are running?

              Originally posted by ahinson View Post
              I'm sorry but I don't quite understand what you mean/want. I'm guessing the appended log file isn't going to work for you?
              how do i append it?

              i tried e.g c:\example.vbs > text.txt

              but this does not output the information to the text file.

              so can you tell me how I can append it to a text file. thank you.

              Comment


              • #8
                Re: How many of one process are running?

                Originally posted by dublin_101 View Post
                how do i append it?

                i tried e.g c:\example.vbs > text.txt

                but this does not output the information to the text file.

                so can you tell me how I can append it to a text file. thank you.
                LOL! Okay??? I guess you didn't see the function that I posted for logging... It did require some work from you like copy/paste it into the existing script, but other than that it's all there.

                Here's the entire thing including the log file etc.

                Code:
                sProcessName = "psi.exe"
                
                sComputer = "." ' use "." for local computer
                Set oWmi = GetObject("winmgmts:" _
                & "{impersonationLevel=impersonate}!\\" & sComputer & "\root\cimv2")
                
                Set colProcessList = oWmi.ExecQuery _
                ("Select * from Win32_Process Where Name = '" & sProcessName & "'")
                
                iCount = colProcessList.Count
                
                If iCount > 0 Then
                   AppendLog sProcessName, iCount
                End If
                
                Function AppendLog(sProcName, iProcCount)
                
                   Dim fso, f
                   Set fso = CreateObject("Scripting.FileSystemObject")
                   Set f = fso.OpenTextFile("c:\ProcessLog.txt", 8, True)
                
                   f.Write Now() & "  " & sProcName & " [" & iProcCount & "]" & vbCRLF
                
                End Function
                Last edited by ahinson; 14th February 2007, 06:00.
                Andrew

                ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

                Comment


                • #9
                  Re: How many of one process are running?

                  absolutely magnificent...........

                  how do i give u karma

                  Comment


                  • #10
                    Re: How many of one process are running?

                    Just click on the YingYang sign on the title bar of Andrew's post.
                    Excellent job, he deserves it!!

                    Sorin Solomon


                    In order to succeed, your desire for success should be greater than your fear of failure.
                    -

                    Comment


                    • #11
                      Re: How many of one process are running?

                      ...just for completeness, if you want, in a command script, to APPEND to a text file instead of overwrite, use TWO "greater than" symbols:

                      e.g.

                      echo >>C:\log.txt "This line is appended"


                      Tom
                      For my own and your protection, I do not provide support by private message under any circumstances. All such messages will be deleted and ignored.

                      Anything you say will be misquoted and used against you

                      Comment


                      • #12
                        Re: How many of one process are running?

                        Originally posted by Stonelaughter
                        ...just for completeness, if you want, in a command script, to APPEND to a text file instead of overwrite, use TWO "greater than" symbols:

                        e.g.

                        echo >>C:\log.txt "This line is appended"
                        I think he tried that... He did mention it earlier in one of his posts.

                        Originally posted by dublin_101 View Post
                        ...initially i just thought i could do eg every hour run x.vbs >> running.txt and on the text file have a read out like, time: 1400 running processes 25
                        Andrew

                        ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

                        Comment


                        • #13
                          Re: How many of one process are running?

                          thanks very much andrew for all your help.


                          now i'm gonna need a script to close some files..............basically workers at work leave files open and that stuffs up our backups....

                          so i need to be able to .eg close all open files on one server ... suggestions appreciated. thanks

                          Comment


                          • #14
                            Re: How many of one process are running?


                            I am trying to deal with the same problem. Especially users that leave their Outlook open and their PST files do not get backed up.
                            What I thought of was to schedule a shutdown -l (-f?) command, some time before the backup.
                            What do you think?

                            Sorin Solomon


                            In order to succeed, your desire for success should be greater than your fear of failure.
                            -

                            Comment


                            • #15
                              Re: How many of one process are running?

                              the prob is that the user may close the session but the file may still appear to be as open on the server...........

                              so i want to be able to close files at the server end/...

                              any ideas?

                              Comment

                              Working...
                              X