Modifying DNS records with WMI

Using WMI it is possible to modify any existing record hosted on a Microsoft DNS Server. The method used varies slightly depending on which record type we want to change.

References for each version of the Modify method can be found in the DNS WMI Provider Reference. These are the most common:

The examples below show modification of Host or A records, but it is possible to extend the example to use any of the types above.

Using VbScript to modify records

This example demonstrates the use of WMI in VbScript to modify a record. The first value for Modify is the TTL, by using objItem.TTL we just reinsert the existing value, only changing the IP address.

' Connect to the WMI Service Set objWMIService = GetObject("winmgmts:\dns01rootMicrosoftDNS") ' Run a query to get the record we want to change Set colItems = objWMIService.ExecQuery("SELECT * FROM MicrosoftDNS_AType" & _ " WHERE ContainerName='domain.example' AND OwnerName='test.domain.example', , 48) ' Loop through the results For Each objItem in colItems ' Modify the record objItem.Modify objItem.TTL, "1.2.3.4" Next

If the TTL must be changed it is important to exclude the IP Address parameter, failure to do so will result in the record being deleted. Any attempt to use Modify without making any changes will result in a “SWbemObjectEx: Generic failure” exception.

Using PowerShell to modify records

The same failure reasons and restrictions apply with PowerShell (including the obscure deletion). Otherwise changing the record can be performed like this.

$ServerName = "dns01" $ContainerName = "domain.example" $RecordName = "test.domain.example" $Record = Get-WMIObject -Computer $ServerName -Namespace "rootMicrosoftDNS" -Class "MicrosoftDNS_AType" -Filter "ContainerName='$ContainerName' AND OwnerName='$RecordName'" # Using the existing TTL value (in seconds) $ModifiedRecord = $Record.Modify($Record.TTL, "2.3.4.5")

In both VbScript and PowerShell the method call returns an object representing the updated record if further processing is required.