简体   繁体   English

远程机器上的 iisreset (C#)

[英]iisreset on remote machine (C#)

Process myProcess = new Process();
ProcessStartInfo remoteAdmin =
            new ProcessStartInfo(Environment.GetFolderPath(Environment.SpecialFolder.System) + @"\iisreset.exe /restart");

remoteAdmin.UserName = username;
remoteAdmin.Password = pwd;
remoteAdmin.Domain = domain;
myProcess.StartInfo = remoteAdmin;
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.RedirectStandardOutput = true;

myProcess.Start();   --- ERROR HERE

Can not find the file specified.找不到指定的文件。

But when I try to run iisreset on the local machine by cmd it's working.但是当我尝试通过 cmd 在本地机器上运行 iisreset 时,它正在工作。

Unless I'm missing something, (Environment.GetFolderPath(Environment.SpecialFolder.System) will get back the local machine (Where the code is running) special folder. So it's expecting the file C:\Windows\System\iisreset.exe to be located on your machine. The only method I could see to get around this, is to drop the C:\ and instead add in the device's name \\DeviceName\C$\ and then the filepath. This is assuming the special folder system is located in the same place on your machine and the remote machine.除非我遗漏了什么, (Environment.GetFolderPath(Environment.SpecialFolder.System)将取回本地机器(代码运行的地方)特殊文件夹。所以它期待文件C:\Windows\System\iisreset.exe到位于您的机器上。我能看到解决此问题的唯一方法是删除C:\并添加设备名称\\DeviceName\C$\然后文件路径。这是假设特殊文件夹系统位于您的机器和远程机器上的同一位置。

The only other method, to get the remote machines system directory is to get it via WMI or via a reg entry reading.获取远程机器系统目录的唯一其他方法是通过 WMI 或通过读取 reg 条目来获取它。

So if using WMI:因此,如果使用 WMI:

"SELECT * FROM Win32_OperatingSystem"

Once done, you would then need to build the folder string yourself from that.完成后,您需要自己从中构建文件夹字符串。

There is no file called C:\Windows\System\iisreset.exe /restart (assuming that Environment.GetFolderPath(Environment.SpecialFolder.System) returns C:\Windows\System\没有名为C:\Windows\System\iisreset.exe /restart的文件(假设Environment.GetFolderPath(Environment.SpecialFolder.System)返回C:\Windows\System\

So you would want所以你会想要

ProcessStartInfo remoteAdmin = 
     new ProcessStartInfo(Environment.GetFolderPath(Environment.SpecialFolder.System) + "iisreset.exe");
remoteAdmin.Arguments = "/restart";

But Environment.GetFolderPath(Environment.SpecialFolder.System) probably returns something like C:\Windows\System (note no trailing slash), and there is definitely no file called c:\windows\systemiisreset.exe但是Environment.GetFolderPath(Environment.SpecialFolder.System)可能会返回类似于C:\Windows\System东西(注意没有斜杠),并且肯定没有名为c:\windows\systemiisreset.exe的文件

So you would actually want所以你实际上想要

ProcessStartInfo remoteAdmin = 
    new ProcessStartInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "iisreset.exe"));
remoteAdmin.Arguments = "/restart";

iisreset.exe supports remote calls, so instead of using WMI to get remote directory you can actually just do: iisreset.exe 支持远程调用,因此您实际上可以执行以下操作,而不是使用 WMI 来获取远程目录:

iisreset {servername}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM