尝试运行 psexec 远程连接到服务器并回收应用程序池

发布于 2024-10-08 09:39:57 字数 922 浏览 11 评论 0原文

如果我从命令提示符运行它,它工作正常。

psexec \ServerName cscript.exe iisapp.vbs /a AppName /r

我正在尝试使用 C# 控制台应用程序执行相同的操作。我正在使用下面的代码,但大多数时候应用程序都会挂起并且无法完成,并且有几次它会抛出错误代码。我这样做错了吗?有谁知道在哪里可以查找错误或错误代码?

static void RecycleAppPool(string sServer)
{
    Console.Clear();
    ProcessStartInfo p = new ProcessStartInfo("psexec.exe", "\\\\" + sServer + " cscript.exe iisapp.vbs /a <AppName> /r");
    p.RedirectStandardInput = true;
    p.UseShellExecute = false;
    Process.Start(p);
}

当它完成时出现错误,看起来像这样

“cscript.exe 退出,错误代码 -2147024664”

编辑

下面的代码运行良好

static void RecycleAppPool(string sServer)
{
    Console.Clear();
    ProcessStartInfo p = new ProcessStartInfo("psexec.exe");
    p.Arguments = @"\\" + sServer + @" cscript.exe iisapp.vbs /a AppName /r";
    p.UseShellExecute = false;
    Process.Start(p);
}

If I run this from my command prompt it works fine.

psexec \ServerName cscript.exe iisapp.vbs /a AppName /r

I'm trying to do the same thing with C# console app. I'm using the below code but most of the time the application hangs and doesn't complete, and the few times it does it throws an error code. Am I doing this wrong? Does anyone know where I can look up the error or error code?

static void RecycleAppPool(string sServer)
{
    Console.Clear();
    ProcessStartInfo p = new ProcessStartInfo("psexec.exe", "\\\\" + sServer + " cscript.exe iisapp.vbs /a <AppName> /r");
    p.RedirectStandardInput = true;
    p.UseShellExecute = false;
    Process.Start(p);
}

When it completes with an error, looks like this

"cscript.exe exited with error code -2147024664"

EDIT

Below code working well

static void RecycleAppPool(string sServer)
{
    Console.Clear();
    ProcessStartInfo p = new ProcessStartInfo("psexec.exe");
    p.Arguments = @"\\" + sServer + @" cscript.exe iisapp.vbs /a AppName /r";
    p.UseShellExecute = false;
    Process.Start(p);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

淡水深流 2024-10-15 09:39:57

VS2003/8/10:工具->错误查找。粘贴十六进制的错误代码。 800700E8。这是“管道正在关闭”。不是很有帮助 - 我猜重定向存在一些问题。

您是否真的有 ProcessStartInfo 参数,或者该参数用于替换您的实际应用程序名称?

VS2003/8/10: Tools->Error Lookup. Paste in the error code in hex. 800700E8. It's "The pipe is being closed." Not very helpful - some issue with redirection i guess.

Do you really have in the ProcessStartInfo parameter, or is that being used to replace what your actual app name is?

梦言归人 2024-10-15 09:39:57

您是否尝试过使用 appcmd 而不是 iisapp 进行回收.vbs?

并且,在此 线程 中,他们使用 WMI 回收了远程应用程序池。

Have you tried recycling using appcmd instead of iisapp.vbs?

And, in this thread they recycled a remote application pool using WMI.

红衣飘飘貌似仙 2024-10-15 09:39:57

如果是 IIS7,那么您可以从 C# 访问 Web 管理命名空间:

using System;
using System.Xml.Serialization;
using Microsoft.Web.Administration;
using System.Linq;
using System.Runtime.InteropServices;

///...

var serverManager = ServerManager.OpenRemote(@"\\myiisserver");
var appPool = serverManager.ApplicationPools["my app pool name"];
appPool.Recycle();

您可以了解有关 此处的 Web 管理命名空间。到目前为止,它对我们来说效果很好。但必须安装在客户端和远程计算机上。

If it's IIS7 then you can you the web admin namespace from C#:

using System;
using System.Xml.Serialization;
using Microsoft.Web.Administration;
using System.Linq;
using System.Runtime.InteropServices;

///...

var serverManager = ServerManager.OpenRemote(@"\\myiisserver");
var appPool = serverManager.ApplicationPools["my app pool name"];
appPool.Recycle();

You can learn more about the Web Admin Namespace here. So far it has worked very well for us. BUT must be installed on the client and remote machines.

绻影浮沉 2024-10-15 09:39:57

在过去的两天里,我在这个问题上苦苦挣扎,尝试了我在网上找到的每一个解决方案。我正在尝试回收不同域中远程计算机上的应用程序池。我尝试使用 PsExec 的第一种方法返回了错误 3。我尝试了 DirectoryEntry,但权限也失败,然后尝试使用 ServerManager 但出现同样的问题。
最后,我转向 WMI 并且它起作用了:

public static void RecycleIis4(string user, string password, string serverName = "LOCALHOST", string appPoolName = "DefaultAppPool")
{
    var processToRun = new[] { @"c:\Windows\system32\inetsrv\appcmd recycle APPPOOL " + appPoolName };
    var connection = new ConnectionOptions { Username = user, Password = password };
    var wmiScope = new ManagementScope(string.Format(@"\\{0}\root\cimv2", serverName), connection);
    var wmiProcess = new ManagementClass(wmiScope, new ManagementPath("Win32_Process"), new ObjectGetOptions());
    wmiProcess.InvokeMethod("Create", processToRun);
}

希望这会有所帮助。

I struggled with this a lot for the last 2 days trying every solution I found online. I'm trying to recycle an application pool on remote machines on a different domain. The first method I tried with PsExec returned error 3. I tried DirectoryEntry and failed on permissions as well and then tried using ServerManager but the same issue.
Finally, I moved to WMI and it worked:

public static void RecycleIis4(string user, string password, string serverName = "LOCALHOST", string appPoolName = "DefaultAppPool")
{
    var processToRun = new[] { @"c:\Windows\system32\inetsrv\appcmd recycle APPPOOL " + appPoolName };
    var connection = new ConnectionOptions { Username = user, Password = password };
    var wmiScope = new ManagementScope(string.Format(@"\\{0}\root\cimv2", serverName), connection);
    var wmiProcess = new ManagementClass(wmiScope, new ManagementPath("Win32_Process"), new ObjectGetOptions());
    wmiProcess.InvokeMethod("Create", processToRun);
}

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文