Search code examples
c#psexeciisreset

How to restart IIS on remote windows server from windows 7 client machine?


I am trying to restart iis remotely (Windows Servr 2012) from my local machine (Windows 7). The below command in command line doesn't work to restart IIS;

    iisreset servername /restart

but the below command works fine when I tried in command line.

    psexec iisreset \\servername /restart

Now the issue is when I try with below code in C#,

    Process process = new Process();
    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.WindowStyle = ProcessWindowStyle.Hidden;
    startInfo.FileName = "cmd.exe";
    startInfo.Arguments = "\C psexec iisreset \\servername /restart";
    startInfo.RedirectStandardOutput = true;
    startInfo.UseShellExecute = false;
    process.StartInfo = startInfo;
    process.Start();
    // capture what is generated in command prompt
    var output = process.StandardOutput.ReadToEnd();

If I give any other arguments in the above code like 'ipconfig', it gives me the expected output. But when I try with psexec, it gives empty output. But it works well when tried in command prompt.

I have also tried by using 'psexec.exe' in the filename and by removing '\C psexec' in the arguments. But still no luck.

Could you please anyone help me to resolve this?

Thanks in Advance.


Solution

  • Thank you all for your valuable response. It works fine with below code :)

        startInfo.FileName = @"C:\Windows\Sysnative\PsExec.exe";
        startInfo.Arguments = "iisreset \\servername /restart";
    

    Reference: Process.Start in C# The system cannot find the file specified error