VB.NET - Service Query - Asked By Tony Reilly-Cooper on 10-Jun-13 05:56 AM

Morning Guys and Girls. 

I am using the below code to query the status of a service, the only problem comes when you try to pull the status for a hostname and ithe currently logged in user doesn't have permissions. 

I have been trying to work out a way I can add in a username and password function so details can be inserted for a user that does have persmissions. basically a "Run as" way of doing things. 

Any pointers? 

        Try
            Dim ServName As String = txtServiceName.Text
            Dim HostName As String = txtHostName.Text
            Dim sqlSvc As ServiceController
            sqlSvc = New ServiceController(ServName, HostName)
            If sqlSvc.Status.ToString.Equals("Running") Then
                lblServiceStatus.Text = ("Service: " & sqlSvc.Status.ToString)
            Else
                lblServiceStatus.Text = ("Service: " & sqlSvc.Status.ToString)
            End If
        Catch ex As Exception
            lblServiceStatus.Text = "Error: " & Err.Description
        End Try

Cheers! 
Robbe Morris replied to Tony Reilly-Cooper on 10-Jun-13 11:13 AM
The remote server would need to have permissions set to allow this to happen anyway which most system administrators won't permit.  You'd want to see if a ICredentials type class (class that implements this interface like NetworkCredentials) is a possible parameter to ServiceController or one of its accepted properties.

You'd want to pass in System.Net.CredentialCache.DefaultCredentials if you want it to use the windows account you'd assigned your app to run under.  This is typically done when the app is a windows service.