[Webinar] Streamline your web hosting managementRegister Today


vb.net stop service as different user

Posted on 2017-10-12
Low Priority
Last Modified: 2017-10-12
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

Question by:David Modugno
  • 3
  • 3

Expert Comment

by:Flabio Gates
ID: 42329299
Have you seen ImpersonateLoggedOnUser?

Author Comment

by:David Modugno
ID: 42329346
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..
LVL 13

Expert Comment

by:Corey Scheich
ID: 42329350
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

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.


Author Comment

by:David Modugno
ID: 42329358
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
LVL 13

Accepted Solution

Corey Scheich earned 1000 total points
ID: 42329359
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

LVL 13

Expert Comment

by:Corey Scheich
ID: 42329364
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.

Author Closing Comment

by:David Modugno
ID: 42329442
using the msdn example solved the problem

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Is your organization moving toward a cloud and mobile-first environment? In this transition, your IT department will encounter many challenges, such as navigating how to: Deploy new applications and services to a growing team Accommodate employee…
In this video I will demonstrate how to set up Nine, which I now consider the best alternative email app to Touchdown.
Suggested Courses

591 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question