开发者

Trying to run psexec to remote to server and recycle app pool

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 does开发者_开发问答n'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);
}


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?


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

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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜