【问题标题】:How to restart IIS on remote windows server from windows 7 client machine?如何从 Windows 7 客户端计算机重新启动远程 Windows 服务器上的 IIS?
【发布时间】:2017-07-31 06:56:15
【问题描述】:

我正在尝试从本地计算机 (Windows 7) 远程重启 iis (Windows Servr 2012)。命令行中的以下命令无法重新启动 IIS;

    iisreset servername /restart

但是当我在命令行中尝试时,下面的命令可以正常工作。

    psexec iisreset \\servername /restart

现在的问题是当我在 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();

如果我在上面的代码中给出任何其他参数,比如“ipconfig”,它会给我预期的输出。但是当我尝试使用 psexec 时,它会给出空输出。但是在命令提示符下尝试时效果很好。

我还尝试在文件名中使用“psexec.exe”并在参数中删除“\C psexec”。但仍然没有运气。

请有人帮我解决这个问题吗?

提前致谢。

【问题讨论】:

标签: c# psexec iisreset


【解决方案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();

【讨论】:

  • 感谢杰森的回复。但我仍然得到“系统找不到指定的文件”输出,但不是预期的。
  • 我将 servername 更改为第一个参数,您可能需要远程服务器上 iisreset 的完整路径。另请参阅:stackoverflow.com/questions/21027559/…
【解决方案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;
       }
 }

【讨论】:

    【解决方案3】:

    IISreset 需要提升的权限才能工作。因此您必须将 -h 开关与 psexec 一起使用

    -h 如果目标系统是 Vista 或更高版本,则使用帐户的提升令牌运行进程(如果可用)。

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

    【讨论】:

      【解决方案4】:

      感谢大家的宝贵回复。它适用于以下代码:)

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-09
        • 2013-01-16
        • 1970-01-01
        • 2017-12-10
        相关资源
        最近更新 更多