Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

vb.net stop service as different user

Posted on 2017-10-12
7
Low Priority
?
31 Views
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)
            sc.Stop()
            sc.WaitForStatus(ServiceControllerStatus.Stopped)
            Return True
        Else
            Return False
        End If

    End Function

Open in new window

0
Comment
Question by:David Modugno
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 5

Expert Comment

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

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..
0
 
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
        proc.Start()

Open in new window

0
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 

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
0
 
LVL 13

Accepted Solution

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

https://msdn.microsoft.com/en-us/library/w070t6ka(v=vs.110).aspx
1
 
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.
0
 

Author Closing Comment

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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

609 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