Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

getting service status

Posted on 2009-07-01
3
Medium Priority
?
1,805 Views
Last Modified: 2012-08-13
This is more of a DOS question than a vbscript question, but it's for a vbscript app.
Is there a way to get only the STATE of a service rather than a list of all the statuses for a service, then get these into vbscript?
I haven't tried this code below, because it's doesn't seem acceptable to loop through the output until you find the word "RUNNING" in the output, but I may have to do something like this. So it's not a afunction yet.
It gives you and idea of what I'm doing.
This is the output that I would be working with, and the line that I specifically want to get is"
        STATE              : 4  RUNNING
I don't know how to get that, I know how to get the below:
SERVICE_NAME: SMTPSVC
DISPLAY_NAME: Simple Mail Transfer Protocol (SMTP)
        TYPE               : 20  WIN32_SHARE_PROCESS
        STATE              : 4  RUNNING
                                (STOPPABLE,PAUSABLE,ACCEPTS_SHUTDOWN)
        WIN32_EXIT_CODE    : 0
        SERVICE_EXIT_CODE  : 0
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0

Sub SMTP_STATUS
' this is a subroutine, but could be a function that returns the SMPT status
Dim strCommand
Dim intSMPTStatus
Dim intDosError
' the DOS command will be to query the SMTPSVC status
' I could not figure out how to get the 
strCommand = "CMD /C SC QUERY SMTPSVC"
intStyle = 10
intDOSError = 0
intSMTPStatus = 0
Dim fldTemp, strTempName
 
     set fsoRun = CreateObject("Scripting.FileSystemObject")
     Set fldTemp = fsoRun.GetSpecialFolder(TEMP_FOLDER)
     strTempName = fldTemp.Path & "\" & fsoRun.GetTempName
     Dim WshShell
     Set WshShell = CreateObject("Wscript.Shell")
     Return = WshShell.Run(strCommand & " >" & strTempName, intStyle, True )
     'Get the output from the command
     Dim filOutput
     Set filOutput = fsoRun.OpenTextFile(strTempName, FOR_READING)
     While Not filOutput.AtEndOfStream
          strOutput = filOutput.ReadLine
          objEntry.WriteLine (strOutput)
	' This is where I would like to get the SMTP status
	If InStr(strOutput,"STATE") > 0 And InStr(strOutput,"RUNNING") > 0 Then
		' If the status of SMPTSVC is running then flag it as 1
		intSMPTStatus = 1
	End If
     Wend
     filOutput.Close
intDOSError = Return
     'Delete the temporary file
     fsoRun.DeleteFile strTempName
Set WshShell = Nothing     
End Sub

Open in new window

0
Comment
Question by:harmono
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 1

Author Comment

by:harmono
ID: 24757468
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 2000 total points
ID: 24761670
Hi there. You can try using this VBS function to retrieve the State of a service.  You need to call the function using

strComputer = "."
strServiceName = "browser"
strState = Get_Service_State(strComputer, strServiceName)

Regards,

Rob.

Function Get_Service_State(strComputer, strServiceName)
	Const wbemFlagReturnImmediately = &h10
	Const wbemFlagForwardOnly = &h20
	On Error Resume Next
	Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
	Set colItems = objWMIService.ExecQuery("SELECT State FROM Win32_Service WHERE Name='" & strServiceName & "'", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
	For Each objItem In colItems
		strState = objItem.State
	Next
	If Err.Number <> 0 Then
		strState = "N/A"
		Err.Clear
	End If
	On Error GoTo 0
	Get_Service_State = strState
End Function

Open in new window

0
 
LVL 1

Author Closing Comment

by:harmono
ID: 31598876
We decided to move this project to Visual Basic 2008, but I might re-visit this later. Thanks.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question