first time posting on stackoverflow!
I need to be able to change the static configuration of NICs in my Domain, so i'm using WMI to do it. I have most stuff figured and my code works great on local PC's just not on Remote PC's. I manage to set DNS addresses IP and Subnet Mask just fine using the proper "EnableStatic" and "SetDNSServerSearchOrder" methods from the Win32_NetworkAdapterConfiguration namespace, but the "SetGateways" method never works.
With some debugging i was able to find that code execution stops right after invoking the "EnableStatus" code. Thinking about this, then it kind of made sense, the IP changes so my connection is lost, and the "SetGateways" method never executes. Making the "SetGateways" method run first won't work if the NIC was previously set to DHCP.
So i tried to do the following, making another WMI connection but this time passing the newIP that was set. It still does not work because the stupid code stops executing, and no exceptions are thrown.
Here's the code:
try
{
ConnectionOptions connection = new()
{
Authority = "ntlmdomain:mydomain",
Impersonation = ImpersonationLevel.Impersonate
};
ManagementScope scope = new(
"\\\\" + pc + "\\root\\CIMV2", connection);
scope.Connect();
ObjectQuery NETquery = new(
"SELECT * FROM Win32_NetworkAdapterConfiguration");
ManagementObjectSearcher NETsearcher =
new(scope, NETquery);
InvokeMethodOptions methodOptions = new(null, TimeSpan.FromSeconds(60));
foreach (ManagementObject mo in NETsearcher.Get().Cast<ManagementObject>())
{
if (mo["Description"].ToString().Equals(nicName))
{
//MessageBox.Show(this, mo["Description"].ToString());
//MessageBox.Show(this, nicName);
ManagementBaseObject newIP =
mo.GetMethodParameters("EnableStatic");
ManagementBaseObject newDNS =
mo.GetMethodParameters("SetDNSServerSearchOrder");
newDNS["DNSServerSearchOrder"] = DnsSearchOrder.Split(',');
ManagementBaseObject setDNS = mo.InvokeMethod(
"SetDNSServerSearchOrder", newDNS, null);
DNS = setDNS["ReturnValue"].ToString();
newIP["IPAddress"] = IpAddresses.Split(',');
newIP["SubnetMask"] = new string[] { SubnetMask };
NO CODE IS EXECUTED AFTER<- ManagementBaseObject setIP = mo.InvokeMethod(
"EnableStatic", newIP, methodOptions); -> NO CODE IS EXECUTED AFTER
DNS = setDNS["ReturnValue"].ToString();
break;
}
}
ConnectionOptions connection2 = new()
{
Authority = "ntlmdomain:mydomain",
Impersonation = ImpersonationLevel.Impersonate
};
ManagementScope scope2 = new(
"\\\\" + IpAddresses + "\\root\\CIMV2", connection);
scope.Connect();
connection2.Timeout = TimeSpan.FromSeconds(60);
ObjectQuery NETquery2 = new(
"SELECT * FROM Win32_NetworkAdapterConfiguration");
ManagementObjectSearcher NETsearcher2 =
new(scope2, NETquery2);
foreach (ManagementObject mo2 in NETsearcher2.Get().Cast<ManagementObject>())
{
if (mo2["Description"].ToString().Equals(nicName))
{
ManagementBaseObject newGate =
mo2.GetMethodParameters("SetGateways");
newGate["DefaultIPGateway"] = new string[] { Gateway };
newGate["GatewayCostMetric"] = new int[] { 1 };
ManagementBaseObject setGateways = mo2.InvokeMethod(
"SetGateways", newGate, methodOptions);
GATE = setGateways["ReturnValue"].ToString();
ShowBalloon("Info", $"Set IP: {WMIReturnValue(IP)}\nSet Gateway: {WMIReturnValue(GATE)}\nSet DNS: {WMIReturnValue(DNS)}");
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
I'm at a loss. How can i solve this?