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:\\dc01\root\MicrosoftDNS")
' Run a query to get the record we want to change
Set colItems = objWMIService.ExecQuery("SELECT * FROM MicrosoftDNS_AType" & _
  " WHERE ContainerName='thezone.net' AND OwnerName='test.thezone.net'",,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 = "somedomain.example"
$RecordName = "test.somedomain.example"

$Record = Get-WMIObject -Computer $ServerName `
  -Namespace "root\MicrosoftDNS" -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.

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

2 Responses to this post.

  1. Posted by Michael Schell on 23.10.08 at 12:40 pm

    With WS2003 DNS Servers at least, it appears using the Modify method to change only the TTL results in the Timestamp being dropped from the record, making it static.

  2. Posted by Justin McAlister on 23.10.08 at 12:40 pm

    Hi Chris,

    It seems like the modify method sets the timestamp to static regardless of what is passed for the TTL argument. Do you know of any ways to keep the timestamp value intact?

    Thank you,

    Justin

Respond to this post