4

我正在尝试从我的本地计算机(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”。但仍然没有运气。

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

提前致谢。

4

4 回答 4

2

我发现当像这样使用 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();
于 2017-07-31T07:09:04.200 回答
1

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

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;
       }
 }
于 2017-07-31T07:09:04.023 回答
1

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();
于 2017-07-31T08:06:36.947 回答
1

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

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

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

于 2017-08-03T09:43:49.113 回答