Below is my code to read IIS applications in a remote machine which in in domain using WMI.
var connOptions = new ConnectionOptions
{
Username = "USERNAME",
Password = "PASSWORD",
Authority = "ntlmdomain:DOMAINNAME",
EnablePrivileges = true,
Impersonation = ImpersonationLevel.Impersonate
};
var scope = new ManagementScope(@"\\SERVERNAME\root\WebAdministration", connOptions);
var WMIQuery = new ObjectQuery("SELECT * FROM Application");
var searcher = new ManagementObjectSearcher(scope, WMIQuery);
//Connect to WMI
scope.Connect();
//Write the list of Applications info
foreach (ManagementObject queryObj in searcher.Get())
{
Console.WriteLine("SiteName: {0} - {1}", queryObj["SiteName"], queryObj["Path"]);
}
It fails with following exception:
System.Management.ManagementException was caught
Message=Access denied
Source=System.Management
StackTrace:
at System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus errorCode)
at System.Management.ManagementObjectSearcher.Get()
at Program.Main(String[] args)
InnerException:
Remedies I tried:
- I followed the suggestion given in Handling Remote Connections Under UAC but with no success. Quoting it:
If your computer is part of a domain, connect to the target computer using a domain account that is in the local Administrators group of the remote computer. Then UAC access token filtering will not affect the domain accounts in the local Administrators group. Do not use a local, nondomain account on the remote computer, even if the account is in the Administrators group.
- Added and assigned full control to the user in Computer Management -> Services and Applications -> WMI control -> View properties -> Navigate to Root/WebAdministration -> Security.
Solved by setting Authentication property of ConnectionOptions to 'AuthenticationLevel.PacketPrivacy'
Details at : Access Denied for WMI