So I am trying to update all IP addresses in a database for the Microsoft DNS server and here is my code
internal bool UpdateIPFromDns(string oldIP, string newIP)
{
try
{
string Query = ("SELECT * FROM MicrosoftDNS_AType WHERE RecordData = '" + oldIP + "'");
ManagementObjectSearcher s = new ManagementObjectSearcher(_scope, new ObjectQuery(Query));
ManagementObjectCollection col = s.Get();
foreach (ManagementObject wmiZone in col)
{
Console.WriteLine("Updating: " + (String)wmiZone.Properties["OwnerName"].Value + " To -> " + newIP);
ManagementBaseObject mgmtParams = null;
mgmtParams = wmiZone.GetMethodParameters("Modify");
mgmtParams["IPAddress"] = newIP;
wmiZone.InvokeMethod("Modify", mgmtParams, null);
}
return true;
}
catch (Exception ex)
{
Console.WriteLine("error: " + ex);
return false;
}
}
for sometimes it seems to work fine and other times it print out that it's updated the item (like it is supposed to), but when I refresh the database itself it has actually removed all those items and i'm not sure why it is doing this.
EDIT:
Ok so I have worked out when its deleting the items
So in the database we start with
Example.com->
example.com 1.1.1.1
t1.example.com 1.1.1.1
t2.example.com 2.3.4.5
test.com->
test.com 1.1.1.1
t1.test.com 2.3.4.5
t2.test.com 1.1.1.1
If I run the update on 1.1.1.1 and update it to 2.3.4.5 it updates so now the database looks like this
Example.com->
example.com 2.3.4.5
t1.example.com 2.3.4.5
t2.example.com 2.3.4.5
test.com->
test.com 2.3.4.5
t1.test.com 2.3.4.5
t2.test.com 2.3.4.5
Now if I was to run it again updating 2.3.4.5 to 2.3.4.5 it then removes all items with the IP 2.3.4.5
But I am not sure why its doing this
P.S I am not very experienced with WMI, so if I am doing something wrong there, let me know
So the implementation, per Microsoft, does exactly what they programmed it to do: https://support.microsoft.com/en-us/kb/979609. (I had a feeling the server was doing something weird and that you had the WMI right). So you probably already built this in by now, but you need to skip the update if the old address equals the new address. Given that WMI is pretty expensive, that's a good idea anyway.