This is my first post here and my second time encoding. Therefore, in order to find out how all this works, I try to find code fragments and copy / paste them until my application is launched. I used to write some batch and ps scripts, but to be honest, I'm more in system administration and hardware ... so far!
My project is a simple GUI tool to start and stop the TeamViewer service on a specific server. I wanted to keep it as simple as possible, and it seemed to work until I ran the application on my colleagues' computers to show them how to use it.
I get an error: System.InvalidOperationException: Der Dienst TeamViewer kann nicht auf dem Computer MYSERVERNAME geöffnet werden. ---> System.ComponentModel.Win32Exception: Zugriff verweigert, which is obviously related to user privileges. So I have been looking for impersonation and WMI for a long time, but now I'm stuck and should ask you guys for your help.
So here is my code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void EIN_Click(object sender, EventArgs e)
{
String svcName = "TeamViewer";
String machineName = "MYSERVERNAME";
var sc = new System.ServiceProcess.ServiceController(svcName, machineName);
sc.Start();
sc.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Running);
}
private void AUS_Click(object sender, EventArgs e)
{
String svcName = "TeamViewer";
String machineName = "MYSERVERNAME";
var sc = new System.ServiceProcess.ServiceController(svcName, machineName);
sc.Stop();
sc.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Stopped);
}
}
I would be very happy if someone could help me!
ps: My Powershell scripts worked like a charm, but I want to make it more complicated :)
Edit1: the server on which I am trying to stop / start the service is not a member of the domain, but each member of the domain must be able to stop / start the service.
source
share