如何从 windows 7 客户机重新启动远程 windows 服务器上的 IIS?

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

我正在尝试从我的本地计算机 (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'。但是还是不行。

你能请任何人帮我解决这个问题吗?

提前致谢。

我希望你需要向它提供域管理员凭据。

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

我发现像这样使用 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();

IISreset 需要提升到 work.So 的权限,您必须将 -h 开关与 psexec

一起使用

-h If the target system is Vista or higher, has the process run with the account's elevated 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();

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

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

参考: