创建一个表单应用程序以重新启动远程PC

时间:2018-09-28 09:49:14

标签: c# remote-server windows-console

我只需要创建一个工具来重新启动我们市政厅中的特定Windows设备。我将始终在192.168.cityID.33中找到一种方法来检测IP的城市代码部分,并在GetIPMethod中为其添加33。

提到的设备具有不同的用户名和密码集,在将重新启动命令传递给cmd时遇到问题

PS:我不是一个专职开发人员,我只是一个网络管理员,他试图减轻我的日常工作量:)

预先感谢

public void Command1()
{
    String IP = GetIPAddress().ToString();
    string NewIP = IP.Substring(0, IP.LastIndexOf("."));
    string TOPIP = NewIP + ".33";

    Process process = new Process();
    process.StartInfo.FileName = "cmd.exe";
    process.StartInfo.CreateNoWindow = true; 
    process.StartInfo.RedirectStandardInput = true;
    process.StartInfo.RedirectStandardOutput = true;
    process.StartInfo.UseShellExecute = false;
    process.StandardInput.WriteLine("NET USE \\" + TOPIP + "\receiver /USER:GenusDS G3nu5DS");
    process.StandardInput.WriteLine("shutdown /m \\" + TOPIP + " /r /f -t 00");
    process.Start();
    process.StandardInput.Flush();
    process.Close();
    process.WaitForExit();
    Console.WriteLine(process.StandardOutput.ReadToEnd());
    Console.ReadKey();
    string strCmdText;
    strCmdText = "NET USE \\" + TOPIP + "\receiver /USER:GDS G3nS";
    System.Diagnostics.Process.Start("CMD.exe",strCmdText);
}

public static IPAddress GetIPAddress()
{
    IPAddress ip = Dns.GetHostAddresses(Dns.GetHostName()).Where(address =>
    address.AddressFamily == AddressFamily.InterNetwork).First();
    return ip;
}

2 个答案:

答案 0 :(得分:0)

我通常会执行以下操作:

Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C net use \\[My IP] [My Password] /USER:[My Username]";
process.StartInfo = startInfo;
Process.Start(startInfo);

this question显示了如何在一个进程中运行多个命令。

希望有帮助。

答案 1 :(得分:0)

var proc1 = new ProcessStartInfo();
            string Command;
            proc1.UseShellExecute = true;
            Command = "net use " + slash + TOPIP + "\\receiver /user:GenusDS G3nu5DS&shutdown /m " + slash + TOPIP + " /r /f -t 00";
            proc1.WorkingDirectory = @"C:\Windows\System32";
            proc1.FileName = @"C:\Windows\System32\cmd.exe";
            /// as admin = proc1.Verb = "runas";
            proc1.Arguments = "/c " + Command;
            proc1.WindowStyle = ProcessWindowStyle.Maximized;
            Process.Start(proc1);