I am working on a c# program that would pick up IP addresses from a notepad file, and for each IP address, if the system is powered on, it would run a command on command prompt that would remotely shut it down. I have written this code, that is currently picking up my system's IP address (not by notepad file currently), check if it is on or off, and on request, remotely shut it down.
It is using a ping request but that is fine because no system here is denying a ping request. What I want is to read the IP address and if it is on, shut it down if the user says "Yes". [I have used "shutdown \s" to shut down my system right now"] This is my code right now.
Console.WriteLine("1-Check PC Status\n2-Exit");
input = Int32.Parse(Console.ReadLine());
switch (input)
{
case 1:
string LocalIP = string.Empty;
var host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
LocalIP = ip.ToString();
}
}
var PingRequest = new Ping();
var PingReply = PingRequest.Send(LocalIP);
if (PingReply.Status == IPStatus.Success)
{
Console.WriteLine("{0, -20} {1, 5}", "IP Address", "Status");
Console.WriteLine("{0, -20} {1, 5}", LocalIP, "On");
Console.WriteLine("\nShutdown the system (y/n)?");
string shutdownChoice;
shutdownChoice = Console.ReadLine();
if(shutdownChoice == "y" || shutdownChoice == "Y")
{
Process cmdProcess = new Process();
cmdProcess.StartInfo.FileName = "cmd.exe";
cmdProcess.StartInfo.RedirectStandardInput = true;
cmdProcess.StartInfo.RedirectStandardOutput = true;
cmdProcess.StartInfo.CreateNoWindow = true;
cmdProcess.StartInfo.UseShellExecute = false;
cmdProcess.Start();
cmdProcess.StandardInput.WriteLine("shutdown /s");
cmdProcess.StandardInput.Flush();
cmdProcess.StandardInput.Close();
cmdProcess.WaitForExit();
}
else
{
input = 2;
}
}
else
{
Console.WriteLine("{0, -20} {1, 5}", LocalIP, "Off");
}
break;
case 2:
input = 2;
break;
default:
Console.WriteLine("Incorrect input");
break;
}
I know this command shutdown \i
but this will open up a dialog to enter the IP addresses but I want to shut it down right through the console without having any additional dialog or window. shutdown \m //computername
isn't working as well.
Apologies for a very lengthy description. Thank you
Try using shutdown /s /f /m \\computername
If this doesn´t work, I would recommend checking out PsTools suite. They have some command line tools and PsShutdown could help.