问题
when i run the following code
ConnectionOptions options = new ConnectionOptions();
options.EnablePrivileges = true;
options.Impersonation = ImpersonationLevel.Impersonate;
options.Authentication = AuthenticationLevel.Packet;
options.Authority = "ntdlmdomain:InsTIL.com";
options.Username = "instil" + @"\" + "admin";
options.Password = "Pwd";
ManagementScope scope= new ManagementScope(string.Format(@"\\172.16.2.171\root\cimv2"),options);
scope.Connect();
if (scope.IsConnected == true)
{
Console.WriteLine("Connection Succeeded");
}
else
{
Console.WriteLine("Connection Failed");
}
ObjectQuery query = new ObjectQuery("Select * from win32_operatingsystem");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection querycollection = searcher.Get();
foreach (ManagementObject mobj in querycollection)
{
Console.WriteLine("Computer name: {0}", mobj["csname"]);
Console.WriteLine("Windows Directory : {0}", mobj["WindowsDirectory"]);
Console.WriteLine("Operating System: {0}", mobj["Caption"]);
Console.WriteLine("Version: {0}", mobj["Version"]);
Console.WriteLine("Manufacturer : {0}", mobj["Manufacturer"]);
}
i get following error
Unhandled Exception: System.Management.ManagementException: Invalid parameter
at System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus errorCode)
at System.Management.ManagementScope.InitializeGuts(Object o)
at System.Management.ManagementScope.Initialize()
at remote_wmi.Program.Main(String[] args) in E:\.net prep\.net examples\remote wmi\remote wmi\Program.cs:line 21
please help me to know why its happening.? what's wrong in this code? is we need to do any thing before executing this code? if yes, please specify it .
Thanks in advance.
回答1:
I did the following changes then i got an answer.
1) i haven't specified the domain name with user name.
options.Username = "admin";
instead of options.Username = "instil" + @"\" + "admin";
2)
options.Authority = "ntlmdomain:InsTIL.com";
instead of options.Authority = "ntdlmdomain:InsTIL.com";
来源:https://stackoverflow.com/questions/15496561/remote-wmi-connection-c-sharp-invalid-parameter-error