Link to home
Start Free TrialLog in
Avatar of David Modugno
David Modugno

asked on

vb.net stop service as different user

I  have a vb.net forms application that checks the status, stops and starts services on remote computers.
It works well, except i need to be able to run it as a user that does not have permissions to perform those operations.
Any idea how i can elevate the permissions in my code... here is one of the functions. Thanks for the help

Public Function StopService(ByVal PC As String, ByVal Service As String) As Boolean
        Dim obj As ManagementObject

        Dim sc As ServiceController

        obj = New ManagementObject("\\" & PC & "\root\cimv2:Win32_Service.Name='" & Service & "'")



        ' Stop the service
        If obj("State").ToString = "Running" Then
            ' now start the service
            sc = New ServiceController(Service, PC)
            sc.Stop()
            sc.WaitForStatus(ServiceControllerStatus.Stopped)
            Return True
        Else
            Return False
        End If

    End Function

Open in new window

Avatar of Flabio Gates
Flabio Gates

Have you seen ImpersonateLoggedOnUser?
Avatar of David Modugno

ASKER

does that do as it sounds?  If so, the logged on user will not have the correct permissions.  I need to be able to prompt the user for login information and use that information to pass to a server.  The server will have that account setup with the minimum permissions to handle a service query, stop start... by doing it this way i will only have to add one service account to each server..
If you separate the code that needs elevated privileges into another process you can use this code to provide different credentials for that process.

        Dim pstartinfo As New ProcessStartInfo("C:\windows\notepad.exe")
        pstartinfo.UserName = "Administrator"
        pstartinfo.Password = "password"

        Dim proc As New Process
        proc.StartInfo = pstartinfo
        proc.Start()

Open in new window

I might be missing something.. are you opening notepad with Administrator? ... i don't get how I would do that to my code with your example
Thanks for taking the time to help
ASKER CERTIFIED SOLUTION
Avatar of Corey Scheich
Corey Scheich
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
My suggestion was to have the code you need elevated privileges to run in a separate .net application.  Then you use the code I posted as a starting point to kick off the application.  Replace C:\windows\notepad.exe with the location of the application that needs elevated account.  Replace Administrator and Password with the credentials of the appropriate user. You would have to create a form to prompt them for that information. Visual Studio provides a template for a Login Form use CTRL-Shift-A to add a new item find Ligin Form under Common Items.
using the msdn example solved the problem