下面是我在远程计算机中使用WMI读取IIS应用程序的代码。
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"]);
}
失败并出现以下异常:
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:
我试过的补救措施:
如果您的计算机是域的一部分,请连接到目标计算机 使用本地Administrators组中的域帐户 远程计算机。然后UAC访问令牌过滤不会影响 本地Administrators组中的域帐户。不要使用 远程计算机上的本地非域帐户,即使该帐户也是如此 在管理员组中。
答案 0 :(得分:2)
通过将ConnectionOptions的Authentication属性设置为'AuthenticationLevel.PacketPrivacy'来解决