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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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..
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

Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.