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!

Tanya
  • 31
  • 1
  • 3
  • You are supposed to use the ServerManager's constructor that takes a server name as parameter, not this one who asks for a file path. – Lex Li Nov 15 '15 at 13:44
  • Yes, but the problem is that the ServerManager.OpenRemote(serverName) function that's supposed to take the server name as a parameter doesn't take credentials, so I can't really use it. I have to connect to a computer in another domain, so I have to use authentication. – Tanya Nov 17 '15 at 06:48
  • MWA relies on the related Interfaces to manage remote machines and you will have to adapt to its limitation. Your approach only exposes the config file to it which is useless as MWA in that case assumes this file is "local", not remote, which leads to the issue you observed. You might try to see if the IIS PowerShell cmdlets + PowerShell remoting is a better option. – Lex Li Nov 17 '15 at 07:50
  • After a lot of research I got to the same conclusion. The config file workaround is fine for configuration data only, that's why I can't read the pool's status that way. The OpenRemote function is limited for inside domain (no authentication), so eventually I understood I just have no other choice. – Tanya Nov 18 '15 at 11:18

1 Answers1

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

hsimah
  • 1,265
  • 2
  • 20
  • 37
  • This would be nice if it worked. Care to offer the full solution? I don't see any reference to SimpleImpersonation here in your code snippet. – user5855178 Jan 19 '19 at 03:35
  • SimpleImpersonation simply wraps this code. You'll have to read their documentation to see how to do it as it's changed since I used it with the above code. – hsimah Jan 19 '19 at 04:11