get the status of a windows server service using vb.net

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rjszum
    New Member
    • Jan 2007
    • 5

    get the status of a windows server service using vb.net

    THis is my vb.net code

    Code:
        Public Class TS_Service
    
            ' Is the service running?
            ' Returns true if we need to start the service
            Public Function CheckService(ByVal PC As String) As Boolean
                Dim obj As ManagementObject
                obj = New ManagementObject("\\" & PC & "\root\cimv2:Win32_Service.Name='SBAMSvc'")
                '     If Not IsNothing(obj) Then
                If obj("State").ToString = "Running" Then
                    Return False
                    'End If
                End If
                Return True
            End Function




    I am receiving an error,
    Class 'myprojectname. managementobjec t' cannot be indexed because it has no default property
    Last edited by acoder; Jan 2 '13, 02:00 AM. Reason: Please use [code] tags when posting code
  • PsychoCoder
    Recognized Expert Contributor
    • Jul 2010
    • 465

    #2
    Try changing your Dim statement to look like this:

    Code:
    Dim obj As New ManagementObject("\\" & PC & "\root\cimv2:Win32_Service.Name='SBAMSvc'")

    Comment

    • rjszum
      New Member
      • Jan 2007
      • 5

      #3
      Unfortunatly, this did not fix issue. same error

      Comment

      • rjszum
        New Member
        • Jan 2007
        • 5

        #4
        I simplified my code but still same error.

        Code:
        Private Sub listservices3()
                '  Dim obj As ManagementObject
        
                Dim obj As New ManagementObject("\\server01\root\cimv2:Win32_Service.Name='tomcat6SPI'")
              obj("State").ToString = "Running" Then
                If obj("State").ToString = "Running" Then
                    Me.Label2.ForeColor = Color.AliceBlue
                End If
           
            End Sub
        Last edited by zmbd; Jan 2 '13, 06:47 PM. Reason: [Z{Please use the <CODE/> button to format posted code/html/sql}]

        Comment

        • Rabbit
          Recognized Expert MVP
          • Jan 2007
          • 12517

          #5
          You need to declare a default property in the ManagementObjec t class. If you cannot, as I suspect it is a class where you don't have access to the source, then you must specify which property you are setting.

          Comment

          • rjszum
            New Member
            • Jan 2007
            • 5

            #6
            I am unsure how to do this, but it seems like this is probably the issue. I am trying to get the Status of the service. the state which would be "Running", "Stopping" or blank(meaning Stopped)

            Comment

            • PsychoCoder
              Recognized Expert Contributor
              • Jul 2010
              • 465

              #7
              I guess you could contact the original writer of that code here and ask if they got the same error and if so how they resolved it.

              Comment

              • Rabbit
                Recognized Expert MVP
                • Jan 2007
                • 12517

                #8
                Well, since you changed the way you are instantiating your object, the issue may be that you didn't escape your forward slashes.

                Comment

                Working...