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)
            Return True
            Return False
        End If

    End Function

Open in new window

David ModugnoAsked:
Who is Participating?
Corey ScheichDeveloperCommented:
This appears to be the .Net Version of Flabio's suggestion there's a full example at the bottom of the page on it's use

Flabio GatesCommented:
Have you seen ImpersonateLoggedOnUser?
David ModugnoAuthor Commented:
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..
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Corey ScheichDeveloperCommented:
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

Open in new window

David ModugnoAuthor Commented:
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
Corey ScheichDeveloperCommented:
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.
David ModugnoAuthor Commented:
using the msdn example solved the problem
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.