Creating DNS Entries with Powershell

Posted: February 10, 2011 in DNS, Scripts

Just some more Powershell. In this case, I had to delete a bunch of stub-zones and then recreate the records from the stubs in the main zone. Reads a CSV that has 3 columns, Name, Type and Address. Type can be either A or CNAME. Could be extended to include other record types, like MX, but didn’t need that at the time.

# Setup the global variables
$server = “”
$zone = “”
$class = 1
$ttl = 3600

# Import list of names
$records = Import-CSV c:\Scripts\Internal.csv

# Go through and remove the existing domains
foreach ($DNSRecord in $records)
 $name = $DNSRecord.Name
 $address = $DNSRecord.Address
    $c=Get-WMIObject -Class “MicrosoftDNS_Zone” -Namespace “root\MicrosoftDNS” -ComputerName $server | where-object{$_.Name -eq “$name.$zone”}
    Write-Host “Going to remove $c”
    $c | Remove-WMIObject  

# Re-read the list
$records = Import-CSV c:\Scripts\Internal.csv

# Create the new records
foreach ($DNSRecord in $records)
 $name = $DNSRecord.Name
    $type = $DNSRecord.Type
 $address = $DNSRecord.Address
    if ($type -eq “A”){
        $A = [wmiclass]”\\$server\root\microsoftDNS:MicrosoftDNS_AType
        $A.CreateInstanceFromPropertydata($server, $zone, “$name.$zone”, $class, $ttl, $address)
    elseif ($type -eq “CNAME”){
        $CNAME = [wmiclass]”\\$server\root\microsoftDNS:MicrosoftDNS_cNameType
        $CNAME.createInstanceFromPropertydata($server, $zone, “$name.$zone”, $class, $ttl, $address) 


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s