المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Stopping remote services with WMI 'RPC not available'



C# Programming
12-06-2009, 06:11 PM
I am having a hard time figuring this out. My applications is using WMI to get information about a remote computers disk drives, installed applications, and services (all of this works fine). I can seem to get all this information but I cannot get the computer to stop a remote service.

I keep getting "RPC server is unavailable". How is this possible? I am able to retrieve information from these computers so it can't be unavailable.

At first I kept getting 'Access Denied' errors. I was trying to make it use the windows username and passwords somehow instead of having to enter a username and password.

So now I'm doing this:


ConnectionOptions conn = new ConnectionOptions();
conn.Impersonation = ImpersonationLevel.Impersonate;
conn.Username = "DOMAIN\username";
conn.Password = "password";
conn.EnablePrivileges = true;


Like I said.. I can connect and get all the information in the world I want, but cannot seem to stop a service. I read this other article about settings permissions and I have already tried that by going through computer management on the server.

Oh and by the way I am using the top domain admin user to do this.. so there is not another user in this domain that has more rights.


If you are interested in the whole code it is here: (Thanks to (Windows Management Instrumentation (WMI) Implementation (http://www.codeproject.com/KB/system/wmi.aspx)[^ (http://www.codeproject.com/KB/system/wmi.aspx)]) for most of it)



ServiceInfo service = (ServiceInfo)serviceInfo;

ManagementOperationObserver observer = new ManagementOperationObserver();
MyHandler completionHandlerObj = new MyHandler();
observer.ObjectReady += new ObjectReadyEventHandler(completionHandlerObj.Done);

try
{
ConnectionOptions conn = new ConnectionOptions();
conn.Impersonation = ImpersonationLevel.Impersonate;
conn.Username = "DOMAIN\username";
conn.Password = "password";
conn.EnablePrivileges = true;

ManagementScope scope = new ManagementScope("\\\\" + service.DNSHost + "\\root\\CIMV2", conn);
scope.Connect();

ManagementObjectSearcher mos = new ManagementObjectSearcher(scope, new ObjectQuery("SELECT * FROM Win32_Service WHERE DisplayName='" + service.item.Text + "'"));
foreach (ManagementObject mo in mos.Get())
{
bool Started = Convert.ToBoolean(service.item.SubItems[1].Text);
if (Started)
mo.InvokeMethod(observer, "StopService", null);
else if (!Started)
mo.InvokeMethod(observer, "StartService", null);

int intCount = 0;
while
(!completionHandlerObj.IsComplete)
{
if (intCount > 10)
{
MessageBox.Show("Terminate process timed out.", "Terminate Process Status");
break;
}
System.Threading.Thread.Sleep(500);
intCount++;
}

//see if call was successful.
if (completionHandlerObj.ReturnObject.Properties["returnValue"].Value.ToString() == "0")
{
if (!Started)
{
service.item.SubItems[1].Text = "True";
service.item.SubItems[1].BackColor = Color.LimeGreen;
service.item.SubItems[1].ForeColor = Color.Black;
}
else
{
service.item.SubItems[1].Text = "False";
service.item.SubItems[1].BackColor = Color.Red;
service.item.SubItems[1].ForeColor = Color.White;
}
}
else
{
MessageBox.Show("Failed.", "Start/Stop Service Failure");
}

}
}
catch (Exception ex) { MessageBox.Show("Error starting/stoppping service.\n\n" + ex.Message); }
finally
{
Invoke((Action)(() => { picProgressServices.Visible = false; }));
}
}