Shalu Shalini

Shalu Shalini

  • NA
  • 123
  • 16.7k

How to restart and shutdown remote server in c#?

May 29 2017 8:15 AM
Can anybody help to shutdown or restart the remote server using c#..
ManagementScope scope = new ManagementScope();
try
{
//get the credentials to connect to this computer with
ConnectionOptions options = new ConnectionOptions();
options.Username = "******";
options.Password = "xxxxxxxxxx";
options.EnablePrivileges = true;
options.Impersonation = ImpersonationLevel.Delegate;
options.Authority = "ntlmdomain:host.getkitaholic.com";
//Create the scope that will connect to the default root for WMI
scope = new ManagementScope(@"\\IP\root\CIMV2", options);
scope.Connect();
SelectQuery query = new SelectQuery("SELECT * FROM Win32_OperatingSystem");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
using (ManagementObjectCollection queryCollection = searcher.Get())
{
foreach (ManagementObject m in queryCollection)
{
string[] ss = { "" };
m.InvokeMethod("Reboot", ss);
}
}
}
catch (UnauthorizedAccessException ex)
{
MessageBox.Show("Unable to connect – Access is denied for those credentials"); return;
}
catch (Exception ex)
{
MessageBox.Show("Unable to connect due to an error: "); return;
}
here returns
 RPC is UNAVAILABLE .. i dont know what i missed pls review the code or give me new solution
Thanks in advance
 
 

Answers (5)