Reputation: 335
I'm trying to shutdown PC remotely programmatically using c# via command prompt and I already done a few search and found out this kind of codes.
System.Diagnostics.Process.Start("shutdown", "/s");
And since it doesn't spicify any pc which to shutdown so I tried changing that codes to this codes which I think satisfy my goal. But it turns out that this doesn't work.
System.Diagnostics.Process.Start("shutdown", "/s /m \\192.168.1.21 /t 5 /c 'Shutdown in 5 seconds'");
NO Exception in C# it just don't shutdown.
I also try this but no luck.
System.Diagnostics.Process.Start("shutdown /s /m \\192.168.1.21 /t 5 /c 'Shutdown in 5 seconds'");
C# Exception "The system cannot find file specified".
EDIT:
I forgot to tell you that I alredy set up my remote computer and server the way that it will not fail to connect to each other such as turning off the firewall, configuring Local system policy and changing network and sharing center.
Upvotes: 5
Views: 15018
Reputation: 2813
This is the code I use for shutdown / reboot remote machines:
int waitSeconds = 0;
string remoteHostNameOrIp = "192.168.0.1";
ProcessStartInfo processInfo = new ProcessStartInfo();
processInfo.FileName = "shutdown.exe";
processInfo.Arguments = $@"-s -t { waitSeconds } -m \\{ remoteHostNameOrIp }";
processInfo.WindowStyle = ProcessWindowStyle.Hidden;
processInfo.CreateNoWindow = true;
var proc = Process.Start(processInfo);
// Works without WaitForExit();
// When using this you can check in "proc" if the shutdown was accepted by
// the remote machine
//
// proc.WaitForExit();
Upvotes: 0
Reputation: 69
// Remote Shutdown
public bool RemoteShutdown(string userName, string password, string ip)
{
try
{
ConnectionOptions op = new ConnectionOptions();
op.Username = userName;
op.Password = password;
// Make a connection to a remote computer.
ManagementScope scope = new ManagementScope("\\\\" + ip + "\\root\\cimv2", op);
scope.Connect();
//Query system for Operating System information
ObjectQuery oq = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
ManagementObjectSearcher query = new ManagementObjectSearcher(scope, oq);
ManagementObjectCollection queryCollection = query.Get();
foreach (ManagementObject obj in queryCollection)
{
obj.InvokeMethod("ShutDown", null); //shutdown
}
return true;
}
catch (Exception)
{
return false;
}
}
// Remote Reboot
public bool RemoteReboot(string userName, string password, string ip)
{
try
{
ConnectionOptions op = new ConnectionOptions();
op.Username = userName;
op.Password = password;
// Make a connection to a remote computer.
ManagementScope scope = new ManagementScope("\\\\" + ip + "\\root\\cimv2", op);
scope.Connect();
//Query system for Operating System information
ObjectQuery oq = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
ManagementObjectSearcher query = new ManagementObjectSearcher(scope, oq);
ManagementObjectCollection queryCollection = query.Get();
foreach (ManagementObject obj in queryCollection)
{
obj.InvokeMethod("Reboot", null); //shutdown
}
return true;
}
catch (Exception)
{
return false;
}
}
Upvotes: 3
Reputation: 23
thanks for the createnowindow, i was already thinking to use it if everything works (but its unusefull if you want to see what is happening)
No comments without explaination about my dashes. as long as i know, you can choose between - or /
try it yourself :-) Start+R (run) you will see it works :)
i think its to much work to solve it.
So i figgerd out that a able to use a file "FILENAME.cmd"
with "shutdown -s -m \IP -t 40" in it
The delay for reminder to close or save stuff (not nessecary)
the -f doesn't work for me
but with the cmd file i can call it and let it run from my application.
So my problem is solved, i know its a little bit.... not perfect. but it works at least.
Thanx to all for quick reply's
Upvotes: -1
Reputation: 71
This should work:
System.Diagnostics.Process.Start("shutdown", @"-m \\192.168.1.21 -s -f -t 0");
Flags should be syntaxed like -m , not /m.
Even better is to create a "silent" shutdown (without cmd-window to show):
var shutdown = new ProcessStartInfo("shutdown", @"-m \\192.168.1.8 -s -f -t 0");
shutdown.CreateNoWindow = true;
shutdown.UseShellExecute = false;
Process.Start(shutdown);
Tested in win7, .Net framework 4.03
Upvotes: 1
Reputation: 3571
You can have a look at the SO post below. It talks about rebooting a remote machine.
If you look at the Win32Shutdown method
Shutdown => 1 (0x1) & Reboot => 2 (0x2)
So in the SO link above you will have to change
// Add the input parameters.
inParams["Flags"] = 2; //Reboot
to
// Add the input parameters.
inParams["Flags"] = 1; //Shutdown
Upvotes: 1
Reputation: 266
in C#, \\ in string means \
so the parameter interpreted as
/s /m \192.168.1.21 /t 5 /c 'Shutdown in 5 seconds'
you should use \\\\ to represent \\
or add an @ mark before the start quote mark like this
System.Diagnostics.Process.Start("shutdown", @"/s /m \\192.168.1.21 /t 5 /c 'Shutdown in 5 seconds'");
Upvotes: 1
Reputation: 109852
You must also enable the "Remote Registry" service on the target computer, AND you must have admin rights on the remote computer.
Upvotes: -1