this is my first time posting here and my second time coding. So in order to learn how all this works, i try to look up code snippets and copy/paste them until my application seems to run. I have written some batch and ps scripts before but to be honest, i'm more into system administration and hardware....until now!
My project is a simple GUI tool to start and stop the TeamViewer service on a certain server. I wanted to keep it as simple as possible and it seemed to work until i started the application on my colleagues computers to show them how to use it.
I get the error: System.InvalidOperationException: Der Dienst TeamViewer kann nicht auf dem Computer MYSERVERNAME geöffnet werden. ---> System.ComponentModel.Win32Exception: Zugriff verweigert, which obviously has to do with user privileges. So i googled for quite a while about Impersonation and WMI service credentials but now i'm stuck and have to ask you guys for your help.
So here's 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'd be very happy if anybody could help me out!
p.s.: My Powershell Scripts worked like a charm but i want to make it look more sophisticated :)
Edit1: The server where i try to stop/start the service is not a member of the domain but every member of the domain should be able to stop/start the service.
Method 1: Using Command SC It interacts with local and remote services quite easily like this: SC \computername STOP servicename. SC \computername START servicename.
Step 2: Start or Stop the service While still in the command prompt type: SC \COMPUTERNAME stop SERVICENAME and press enter. This will tell the remote service to stop, it will not auto refresh when it stops so you have to manually check, you can do this by typing: SC \COMPUTERNAME query SERVICENAME.
i found a post, that helped me completing my code. It can be found here.
Edit1: Here's how the code looks like now:
private void EIN_Click(object sender, EventArgs e)
{
try
{
#region Code to start the service
string serviceName = "TeamViewer";
string IP="actual-IP-address";
string username ="actual-username";
string password ="actual-password";
ConnectionOptions connectoptions = new ConnectionOptions();
//connectoptions.Impersonation = ImpersonationLevel.Impersonate;
connectoptions.Username = username;
connectoptions.Password = password;
//IP Address of the remote machine
ManagementScope scope = new ManagementScope(@"\\" + IP + @"\root\cimv2");
scope.Options = connectoptions;
//WMI query to be executed on the remote machine
SelectQuery query = new SelectQuery("select * from Win32_Service where name = '" + serviceName + "'");
using (ManagementObjectSearcher searcher = new
ManagementObjectSearcher(scope, query))
{
ManagementObjectCollection collection = searcher.Get();
foreach (ManagementObject service in collection)
{
if (service["Started"].Equals(false))
{
//Start the service
service.InvokeMethod("StartService", null);
//here i added a picture box which shows a green button when the service is started
pictureBox1.Image = Properties.Resources._120px_Green_Light_I_svg;
}
}
}
#endregion
}
catch (NullReferenceException)
{
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With