我想改變本地網絡適配器的一些配置。我必須使用非管理用戶,但當我嘗試調用方法時,我只是得到返回值「91」,代表「拒絕訪問」。可以設置「EnableStatic」方法的權限,但「SetDNSServerSearchOrder」完全不起作用。它總是返回「訪問被拒絕」。我甚至無法禁用網絡適配器(在Win32_NetworkAdapter中)。一切與管理員用戶很好。由於我有與「EnableStatic」相同的問題,我敢肯定,必須有一種方法可以使用其他方法。我爲wmimgmt.msc中的所有命名空間設置了安全選項,我在dcomcnfg中設置了限制,我在註冊表中爲tcpip授予權限,將用戶添加到分佈式com-users,性能組,網絡配置操作員中,但是沒有辦法得到這個工作。我意識到,當我的工具正在運行時,沒有dns ip。因此,儘管該方法返回「拒絕訪問」,但有些內容會發生變化。有任何想法嗎?自從三週以來,即使微軟也無法幫助我。當我以管理員身份登錄時,該工具沒有任何問題地運行。這是我的C#代碼:(和我的書面英語的一些錯誤對不起;))Win32_NetworkAdapter配置與非管理用戶(訪問被拒絕)
private static ManagementScope CreateScope(string strScope)
{
ManagementScope scope = new ManagementScope(strScope);
scope.Options.Impersonation = ImpersonationLevel.Impersonate;
scope.Options.Authentication = AuthenticationLevel.Packet;
return scope;
}
public static void SetIP(String strScope, String strQuery, string IPAddress, string SubnetMask, string Gateway, string DNSServer1, string DNSServer2)
{
ManagementScope scope = CreateScope(strScope);
SelectQuery query = new SelectQuery(strQuery);
Collection<object> objCol = new Collection<object>();
scope.Connect();
ManagementObjectCollection mobjCol = null;
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query))
{
mobjCol = searcher.Get();
}
foreach (ManagementObject objMO in mobjCol)
{
try
{
ManagementBaseObject objNewIP = null;
ManagementBaseObject objSetIP = null;
ManagementBaseObject objNewGate = null;
ManagementBaseObject objNewDNS = null;
objNewIP = objMO.GetMethodParameters("EnableStatic");
objNewGate = objMO.GetMethodParameters("SetGateways");
objNewDNS = objMO.GetMethodParameters("SetDNSServerSearchOrder");
//Set DefaultGateway
objNewGate["DefaultIPGateway"] = new string[] { Gateway };
objNewGate["GatewayCostMetric"] = new int[] { 1 };
//Set IPAddress and Subnet Mask
objNewIP["IPAddress"] = new string[] { IPAddress };
objNewIP["SubnetMask"] = new string[] { SubnetMask };
//Set DNS servers
objNewDNS["DNSServerSearchOrder"] = new string[] { DNSServer1, DNSServer2 };
//Invoke all changes
objSetIP = objMO.InvokeMethod("EnableStatic", objNewIP, null);
MessageBox.Show("EnableStatic: " + objSetIP["ReturnValue"].ToString());
objSetIP = objMO.InvokeMethod("SetGateways", objNewGate, null);
MessageBox.Show("SetGateways: " + objSetIP["ReturnValue"].ToString());
objSetIP = objMO.InvokeMethod("SetDNSServerSearchOrder", objNewDNS,null);
MessageBox.Show("SetDNSServerSearchOrder: " + objSetIP["ReturnValue"].ToString());
}
catch (ManagementException ex)
{
MessageBox.Show("Unable to Set IP : " + ex.Message);
}
}
}