0

I just need to create a tool to restart a specific windows device in our city offices. which will always in 192.168.cityID.33 I have found a way to detect the citycode portion of the IP and to add 33 to it in GetIPMethod.

thee mentioned device have different set of username and password im having issues passing the restart command to the cmd

PS: I'm not a full time developer i'm just a network admin who tries to reduce my daily work load :)

Thanks in advance

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;
}
Hossein Golshani
  • 1,847
  • 5
  • 16
  • 27
Binku
  • 43
  • 1
  • 11
  • 1
    _I'm having issues_ Please be more descriptive! – TaW Sep 28 '18 at 09:58
  • @TaW Simply I just need to pass above given commands to cmd when a button was pressed – Binku Sep 28 '18 at 10:01
  • Does this comment work: `"NET USE \\" + TOPIP + "\receiver G3nu5DS /USER:GenusDS"` –  Sep 28 '18 at 10:27
  • 1
    "i'm just a network admin who tries to reduce my daily work load" you may be better off looking at Powershell. https://www.techrepublic.com/article/10-powershell-commands-to-make-remote-management-easier/ see number 3 for your needs – phuzi Sep 28 '18 at 11:04
  • @phuzi actually these pcs aren't in a domain or powershell capable – Binku Sep 28 '18 at 11:53
  • @b3hdad yeas it works if i directly typed it on the cmd but when comes to C# forms this dose not pass to console – Binku Sep 28 '18 at 11:58
  • I think it might be something really silly: `process.StandardInput.WriteLine("NET USE \\" + TOPIP + "\receiver /USER:GenusDS G3nu5DS");` I don't think this'll work. This however should=> `process.StandardInput.WriteLine(@"NET USE \\" + TOPIP + @"\receiver /USER:GenusDS G3nu5DS");` Note the @ sign... | same for strCmdText of course... – LocEngineer Sep 28 '18 at 12:09

2 Answers2

0

I normally do something similar to below:

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

And this question shows how to run multiple commands in one process.

Hope that helps.

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);
Binku
  • 43
  • 1
  • 11