I'm trying to control Windows Services that are installed in a remote computer. I'm using the ServiceController
class.
I have this:
ServiceController svc = new ServiceController("MyWindowsService", "COMPUTER_NAME");
With this, I can get the status of the Windows Service like this:
string status = svc.Status.ToString();
But I can't control the Windows Service (by doing开发者_StackOverflow svc.Start();
or svc.Stop();
).
I get the following exception:
Cannot open Servicexxx service on computer 'COMPUTER_NAME'
That's normal, I suppose there is something to do with access permissions. But how? I've looked into Google but didn't find what I was looking for. However I often read something related to impersonation, but I don't know what that means.
NB: The local and remote computers are both running Win XP Pro.
Problem solved.
Impersonation consists in running a piece of code using a certain logon/password. I found this very useful project: http://www.codeproject.com/KB/cs/svcmgr.aspx?display=Print that helped me a lot!
Starting and stopping services is a highly privileged operation, normally available only to administrators. Ensure that the user account you use has sufficient privileges on the target machine. Ask more questions about it at serverfault.com
In order to solve the issue , give your name the admin permissions on remote computer/server like domain/username and you will able to run the package successfully since i had the same issue and when i gave permissions to my self services were accessible on remote server
i had same issue but is done. try this code bellow
protected void Button4_Click1(object sender, EventArgs e)
{
//1º connect to remote computer
ConnectionOptions connection = new ConnectionOptions();
connection.Username = "USER NAME OF REMOTE COMPUTER";
connection.Password = "PASS WORD OF REMOTE COMPUTER";
connection.Authority = "NTLMDOMAIN:DOMINE NAME OF YOUR LAN";
connection.EnablePrivileges = true;
connection.Authentication = AuthenticationLevel.Default;
connection.Impersonation = ImpersonationLevel.Impersonate;
ManagementScope scope = new ManagementScope(
"\\\\NAME OR IP OF REMOTE COMPUTER\\root\\CIMV2", connection);
scope.Connect();
// finsh connection
ManagementObjectSearcher moSearcher = new ManagementObjectSearcher();
moSearcher.Scope = scope;
moSearcher.Query = new ObjectQuery("SELECT * FROM win32_Service WHERE Name ='SERVICE NAME IN REMOTE COMPUTER'");
ManagementObjectCollection mbCollection = moSearcher.Get();
// ServiceController svc = new ServiceController("Jenkins", "10.224.62.35");
//namelbl.Text = svc.Status.ToString();
foreach (ManagementObject oReturn in mbCollection)
{
// invoke start
ManagementBaseObject outParams = oReturn.InvokeMethod("StartService", null, null);
//invoke stop
ManagementBaseObject outParams2 = oReturn.InvokeMethod("StopService", null, null);
//get result
string a = outParams["ReturnValue"].ToString();
// get service state
string state = oReturn.Properties["State"].Value.ToString().Trim();
MessageBox.Show(state);// TO DISPLAY STATOS FROM SERVICE IN REMOTE COMPUTER
}
}//THE CODE ABOVE IS WRITER IN C# I HOPE HELP SOME ONE. THANKS
精彩评论