c# - 如何从 Windows 7 客户端计算机在远程 Windows 服务器上重新启动 IIS?

标签 c# psexec iisreset

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

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

提前致谢。

最佳答案

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

关于c# - 如何从 Windows 7 客户端计算机在远程 Windows 服务器上重新启动 IIS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45409228/

相关文章:

asp.net - session 生命周期问题

c# - 将 C# 字符串与 '==' 进行比较是一种好习惯吗?

PsExec:Win7 到 Win7 访问被拒绝(psexesvc 仍然存在)

C# 和 psexec 进程输出重定向问题

powershell - 批处理文件、Powershell 脚本、PSExec 和系统用户

asp.net - 如何减少在 IIS 5.1 中开发 ASP.NET Web 应用程序时对 IISRESET 的需求

azure - Windows Azure 上的 Web 角色和 iisreset 副作用

c# - 公共(public)静态 Dictionary<String,GameObject> 中的游戏对象在 Unity 中的场景更改时被销毁

c# - 使用 Angular 6 将文件上传到 Web Api C#

c# - 使用 EPPlus 在没有科学记数法的情况下写入 Excel