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
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


Salman BegRank #284 · 8.8K reputationPosted May 29, 2017, 6:21 AM EDT
Rajeev PunhaniRank #551 · 4.4K reputationPosted May 29, 2017, 8:36 AM EDT
Vivek KumarRank #272 · 9.3K reputationPosted May 29, 2017, 11:34 AM EDT
Pavithra V40 reputationPosted Mar 17, 2020, 2:51 AM EDT
Jignesh KumarRank #91 · 22.9K reputationPosted Mar 17, 2020, 6:18 AM EDT