Set-DnsServerResourceRecord and OldInputObject Not Found
While trying to help a co-worker today, I stumbled across a documentation error on Microsoft’s TechNet in relation to Set-DnsServerResourceRecord. The example uses multiple variable initialization, which unfortunately ends up making pointers.
Okay, so the example seems pretty simple. They use the variable pass through to assign the return of Get-DnsServerResourceRecord to 2 variables at the same time. This should save some time, and avoid executing the same command twice. However, this actually causes an issue in this case, and here’s why.
What I’ve done here is grab the ‘jatest’ host record, outputted the two values, then updated the $newobj to have a new IP address of 22.214.171.124. However, as you can see here, there is an issue, the $oldObj value also updated the IP address. This has happened because $newObj is a pointer to $oldObj, meaning changes to one will apply to the other. Why does this matter? Well, the Set-DnsServerResourceRecord uses the old record information to find the record to update, and then updates it. This is important to understand because you could potentially have multiple IP records for ‘A’ records, or multiple NS records, or multiple MX records, etc. This means the data you are using to find the update must match the record you want to update, otherwise you could update a lot of records incorrectly. If it doesn’t, this happens instead: