Announcement

Collapse
No announcement yet.

Services Script.

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

  • Services Script.

    Hi, I'm new to the forum but I am looking for a little bit of help with a script.
    Basically, what we want the script to do is kick off to show us all running services on the machine the script is run on, but only the services assigned to a certain user (ie. admin, Localsystem, etc...) I am able to run the script to show all running services, but not assigned to a specific user. Can Someone help. here is the script below as well as the error I'm getting. I'm rather new to scripting so go easy on me Thanks!

    -Matt

    Code:

    Const col1 = 65
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

    Set colRunningServices = objWMIService.ExecQuery _
    ("Select * from Win32_Service Where State = 'Running'")
    ("Select * from Win32_Service Where Startname = 'LocalSystem' ")

    For Each objService in colRunningServices
    intSpaces = col1 - Len(objService.DisplayName)
    Wscript.Echo objservice.DisplayName & Space(intSpaces) & objService.State
    Wscript.Echo objService.DisplayName & Space(intSpaces) & objService.Startname

    Next



    Error:

    Line 8
    Char 1
    Expected statement
    800A0400
    Microsoft VBScript compilation error.

  • #2
    Re: Services Script.

    Hi Matt,

    Welcome to this forum.

    In the lines 7 and 8 you make different conditions for the services to query.

    Where Line 7 is part of the objWMIService.ExecQuery in line 6 (line 6 ends with underscore _ , meaning the command continues on the next line).
    In line 8 you make a second condition but the script won't understand any of it because it will now be just a new line with unreconizable characters.

    You have to Delete line 8 .
    And modify line 7:
    Code:
    ("Select * from Win32_Service Where State = 'Running' And Startname = 'LocalSystem'")

    \Rems


    other example:
    Code:
    strComputer = "."  '<--- or the name of a remote computer
    srvQuery    = "State = 'Running' And Startname = 'LocalSystem'"
    
    
    Set objWMIService = GetObject("winmgmts:" _
              & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    
    Set colRunningServices = objWMIService.ExecQuery _ 
              ("Select * from Win32_Service Where " & srvQuery )
    
    For Each objService in colRunningServices 
        Wscript.Echo chr(34) & objservice.DisplayName & chr(34) & "   (" & objService.Name & ")" _
                     & vbnewline & objService.State & "  under " & objService.Startname
    Next

    This posting is provided "AS IS" with no warranties, and confers no rights.

    __________________

    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts

    Comment


    • #3
      Re: Services Script.

      That worked perfectly!! Thanks so much for your help!

      Comment


      • #4
        Re: Services Script.

        Thanks so much for your help!
        No problem Matt, glad I could help.


        I liked in your script how intSpaces = col1 - Len(objService.DisplayName) is used for creating columns in the output on screen. But to see the real effect of that, you have to execute the script with cscript.exe in a dosprompt .

        An other way to show the columns on display, and running the script the normal way, is by writing to a temporary output-file during the FOR - NEXT process instead of using wscript.echo in the script. After the query part is finished the script opens the completed (temporary) file. Now you can see the columns also.

        example:
        Code:
        strComputer = "."    '<-- or... the name of a "remotecomputer"
        
        Const col1 = 75   '(in this example this is the distance over the two first columns)
        
        '*** create a temporary output-file to write the values of each 'objService' to
         Const ForAppending = 8
         Set objFSO = CreateObject("Scripting.FileSystemObject")
         Set objShell = CreateObject("Wscript.Shell")
         sOutTmp = objShell.ExpandEnvironmentStrings("%temp%\" & objFSO.GetTempName)
        
            Set objWMIService = GetObject("winmgmts:{impersonationLevel=" _
                       & "impersonate}!\\" & strComputer & "\root\cimv2")
        
            Set colRunningServices = objWMIService.ExecQuery _ 
                   ("Select * from Win32_Service Where State = 'Running' " _
                   & "And Startname = 'LocalSystem'")
        
        With objFSO.OpenTextFile(sOutTmp, ForAppending, True)
        
            For Each objService in colRunningServices
                intSpaces = col1 - Len(objService.DisplayName + objService.Name)
                If intSpaces < 1 then intSpaces = 1
                 .WriteLine(objservice.DisplayName & Space(intSpaces) & "["& objService.Name &"]" _
                            & vbTab & objService.State & " under " & objService.Startname)
            Next
        
        .Close 
        End With '***
        
         objShell.Run "WordPad " & sOutTmp, 3, True
        
         objFSO.DeleteFile(sOutTmp) 'deletes the temp-file from disk
        
        Set objShell = Nothing
        Set objFSO = Nothing
        Set objWMIService = Nothing
        WScript.Quit
        \Rems
        Last edited by Rems; 9th May 2007, 22:25. Reason: (Len(Aaa)+Len(Bbb)) shortened to Len(Aaa+Bbb)

        This posting is provided "AS IS" with no warranties, and confers no rights.

        __________________

        ** Remember to give credit where credit's due **
        and leave Reputation Points for meaningful posts

        Comment


        • #5
          Re: Services Script.

          hey, I just checked out the updated version, wow that looks a lot cleaner. Thanks for the pointers on that, I'm going to show that to one of my co-workers... I appreciate the help.

          Comment

          Working...
          X