Restarting Windows service on remote machine.

Hello all,

I'm trying to make it possible for a user that is not a Domain Admin to programatically restart a service if necessary.  The current code works fine for Domain Admins.  What I would like in the end is to have the service restart call appear to come from the remote machine's local administrator account.  Whatever would be the easiest solution for this is what I'm looking for (of course).  I'm having trouble with getting impersonation to work for this task.

Thanks in advance!
private void restartVNCService(string hostname)
        {
            ServiceController sc = new ServiceController("winvnc", hostname);
 
 
                if (sc.Status.Equals(ServiceControllerStatus.Stopped))
                {
                    MessageBox.Show("Service already stopped.  Starting...");
                    sc.Start();
                    sc.WaitForStatus(ServiceControllerStatus.Running);
                    MessageBox.Show("The WinVNC service was started.");
                    return;
                }
                else if (sc.Status.Equals(ServiceControllerStatus.Running))
                {
                    try
                    {
                        sc.Stop();
                        sc.WaitForStatus(ServiceControllerStatus.Stopped);
                        sc.Start();
                        sc.WaitForStatus(ServiceControllerStatus.Running);
                        MessageBox.Show("The WinVNC service was restarted.");
                    }
                    catch (Exception ex)
                    { MessageBox.Show("The WinVNC service could not be re-started for some reason.  Sorry." + Environment.NewLine + ex.Message); }
                }
                else { MessageBox.Show("Error.  WinVNC service status is: " + sc.Status.ToString()); }
        }

Open in new window

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

SysExpertCommented:
You need to give the person,  permissions to start/stop services.


I hope this helps !
0
batracyAuthor Commented:
Is there no way to use impersonation for this issue?  We would rather impersonate the remote machine's local admin account if at all possible...
0
SysExpertCommented:
You can use runas possibly, but I am not sure how to implement in your script, and it requires username and PW to be entered

0
batracyAuthor Commented:
As it appears there is no answer to this, I will close this question.
0

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

From novice to tech pro — start learning today.