Get remote application pool state using Microsoft.Web.Administration

3

I'm trying to get the state of an IIS 7.5 application pool using Microsoft.Web.Administration API, but get an exception:

System.Runtime.InteropServices.COMException was unhandled
  ErrorCode=-2147020584
  HResult=-2147020584
  Message=The object identifier does not represent a valid object. (Exception from HRESULT: 0x800710D8)
  Source=Microsoft.Web.Administration

I connect to a remote machine in a different domain using the following code:

string appHostConfigFile = "\\\\" + serverName + "\\c$\\windows\\system32\\inetsrv\\config\\applicationHost.config";
UNCAccess unc = new UNCAccess(@"\\" + serverName + "\\c$\\windows\\system32\\inetsrv\\config", <USER_NAME>, <DOMAIN>, <PASSWORD>);
server = new ServerManager(appHostConfigFile);

and then try to iterate through all application pools:

ApplicationPoolCollection applicationPools = server.ApplicationPools;
foreach (ApplicationPool pool in applicationPools)
{
    Console.WriteLine(“Name: ” + pool.Name + “ State: “ + pool. State);
}

Now, the strange thing is that I do get the Name property correctly (and many other properties too) but the State property thoughts an exception. Only when I tried connecting to the local machine (127.0.0.1), everything worked as expected.

So, can anyone tell me what I’ve been missing here? Is there any other way to connect to a remote IIS server on a machine outside of mine domain?

Thanks a lot!

iis
dns
pool
asked on Stack Overflow Nov 15, 2015 by Tanya

1 Answer

1

I have successfully used Windows Impersonation to access a remote IIS instance using ServerManager.

using (ServerManager serverManager = new ServerManager($@"\\{computerName}\c$\windows\system32\inetsrv\config\applicationHost.config"))
{
    ApplicationPoolCollection appPools = serverManager.ApplicationPools
}

This works with IIS 8 and IIS 7.5. I have not tested with other versions. I am using SimpleImpersonation (available via Nuget).

answered on Stack Overflow Sep 26, 2016 by hsimah

User contributions licensed under CC BY-SA 3.0