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

I am trying to restart iis remotely (Windows Servr 2012) from my local machine (Windows 7). The following command on the command line does not work to restart IIS;

    iisreset servername /restart

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

    psexec iisreset \\servername /restart

Now the problem is that I'm trying to use the code below 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, for example, "ipconfig", it gives me the expected result. But when I try to use psexec, it gives an empty output. But it works fine when trying on the command line.

I also tried using 'psexec.exe' in the file name and removing '\ C psexec' in the arguments. But still no luck.

Could you help me solve this problem?

Advance.

+4
4

. :)

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

: Process.Start #

+1

, PSexec, , CMD.exe, , psexec. , exe.

//Assume that psexec.exe is in same location as application
//Get directory of running applications
String AppPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).Replace("file:\\","");

//Set up start infor details
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.UseShellExecute = false;

//Combine path of running app
startInfo.FileName = Path.Combine(AppPath, "psexec.exe");
startInfo.Arguments = "\\servername c:\path\to\iisreset  /restart";

//Execute
Process nProc = Process.Start(startInfo);
nProc.Start();
+2

, .

private int ExcecuteCommand(string command, string fileName, bool getResult, string timeout = null)
{
   try
     {
        var secure = new SecureString();
        foreach (char c in txtAdminPassword.Text)
        {
          secure.AppendChar(c);
        }
        System.Diagnostics.Process pProcess = new System.Diagnostics.Process();
        pProcess.StartInfo.Domain = txtDomainName.Text;
        pProcess.StartInfo.UserName = txtUser.Text;
        pProcess.StartInfo.Password = secure;
        pProcess.StartInfo.FileName = fileName;// AppDomain.CurrentDomain.BaseDirectory + @"PSTools\PsExec.exe"; ;
        //pProcess.StartInfo.Arguments = string.Format(@"\\{0} -i -s -accepteula  ipconfig /all", ipAddress);
        //pProcess.StartInfo.Arguments = string.Format(@"\\{0} -accepteula netstat -ano",ipAddress);
        //pProcess.StartInfo.Arguments = string.Format(@"\\{0} -accepteula -i CheckURLConnectivity", ipAddress);
        //pProcess.StartInfo.Arguments = string.Format(@"\\{0} -accepteula  ping {2}", ipAddress, AppDomain.CurrentDomain.BaseDirectory + @"Installer\CheckURLConnectivity.exe","10.10.10.35");
          //pProcess.StartInfo.Arguments = string.Format(@"\\{0} -accepteula cmd /c type C:\ServiceLog.txt", ipAddress);
         pProcess.StartInfo.Arguments = command;//string.Format(@"\\{0} -accepteula -c -f {1}", compName, AppDomain.CurrentDomain.BaseDirectory + @"Installer\CheckURLConnectivity.exe");
        pProcess.StartInfo.UseShellExecute = false;
        Process.StartInfo.RedirectStandardInput = true;
        pProcess.StartInfo.RedirectStandardOutput = true;
        pProcess.StartInfo.RedirectStandardError = true;
        pProcess.StartInfo.CreateNoWindow = true;
        logger.log("Query " + command);
        pProcess.Start();
        if (timeout == null)
           pProcess.WaitForExit();
        else
           pProcess.WaitForExit(Convert.ToInt32(timeout));

         string strOutput = string.Empty;

          if (pProcess.HasExited == true && pProcess.ExitCode != 0)
            {
              string _errorMessage = GetWin32ErrorMessage(pProcess.ExitCode);
                pProcess.Kill();
               return pProcess.ExitCode;
            }
            else
              return 0;
      }
      catch (Exception)
       {
         return -1;
       }
 }
+1

IISreset . -h psexec

-h If the target system is Vista or higher, the process runs with the increased account token, if available.

    Process process = new Process();
    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.WindowStyle = ProcessWindowStyle.Hidden;
    startInfo.FileName = "psexec.exe";
    startInfo.Arguments = "-h 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();
+1
source

Source: https://habr.com/ru/post/1682643/


All Articles