Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get remote application pool state using Microsoft.Web.Administration

Tags:

iis

dns

pool

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!

like image 701
Tanya Avatar asked Feb 24 '26 01:02

Tanya


1 Answers

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).

like image 84
hsimah Avatar answered Feb 25 '26 18:02

hsimah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!