Mittwoch, 21. März 2012

PowerShell: Create DNS Alias Entry

This Script will help you to automate your DNS-registration. If you have 2 NICs installed in your system, maybe you dont want both to register in the DNS. What you do is to create manually an entry for your NIC with an alias.
I created this script to detect the NIC with a special IP-address, prove whether it has an A-record and a PTR-record. If not it will create it:
I did some comments into the code, for any other questions just ask.

"Create DNS backup entry" > "c:\temp\DNSentry.txt"
get-date >> "c:\temp\DNSentry.txt"

## check DNS server contactable
  if (-not (Test-Connection -ComputerName <your DNS-Server> -quiet )){"DNS server not found" >> "c:\temp\DNSentry.txt"}
    else
{
"DNS Server is reachable"  >> "c:\temp\DNSentry.txt"
##get FQDN
$computersystem = get-WmiObject -Class Win32_computersystem
$systemname = ($computersystem).name + "<Suffix>" + "." + ($computersystem).Domain
"Systemname: " + $systemname  >> "c:\temp\DNSentry.txt"
##get the ip-address of your NIC
$NICadapter = @()
$NICadapter += gwmi win32_networkadapterconfiguration | ? { $_.IPaddress -like "*192.168*"}
##check if there is a backup NIC
##if not, skip everything
$checkNIC = gwmi win32_networkadapterconfiguration | ? { $_.IPaddress -like "192.168*"}
if($checkNIC){
foreach($element in $NICadapter){
##revert IP and trim
$IParray = $element.IPaddress -split "\."
$ipaddress = $element.IPaddress
break
}
$revertipaddress = $iparray[3] + "." +$iparray[2] + "." +$iparray[1]
#$revertipaddress
"NIC IP:" + $ipaddress  >> "c:\temp\DNSentry.txt"
##Check Entry if exists
$Arec = Get-WmiObject -ComputerName <your DNS-Server> -Namespace ‘root\MicrosoftDNS’ -Class MicrosoftDNS_ATYPE -filter 'ContainerName = "domain.net"' | ? {$_.OwnerName -like "$systemname" } # | select -first 1
$PTRrec = Get-WmiObject -ComputerName <your DNS-Server> -Namespace ‘root\MicrosoftDNS’ -Class MicrosoftDNS_PTRTYPE -filter 'ContainerName = "10.in-addr.arpa"' | ? {$_.OwnerName -like $revertipaddress +"*"} # | select -first 1
$DNScheck = 4

""  >> "c:\temp\DNSentry.txt"
"Checking A-Record..."  >> "c:\temp\DNSentry.txt"
""  >> "c:\temp\DNSentry.txt"
IF($Arec)
        {"There is an existing A-Record for " + $systemname >> "c:\temp\DNSentry.txt"
         "Aborting!" >> "c:\temp\DNSentry.txt"
         exit              
        }
      else
        {"there is no A-Record" >> "c:\temp\DNSentry.txt"
        }
""  >> "c:\temp\DNSentry.txt"       
"Checking PTR-Record..."  >> "c:\temp\DNSentry.txt"
        
IF($PTRrec)
        {"There is an existing PTR-Record for " + $systemname >> "c:\temp\DNSentry.txt"
         "Aborting!" >> "c:\temp\DNSentry.txt"
         exit  
        }
      else
        {"there is no PTR-Record" >> "c:\temp\DNSentry.txt"
        }
"Test"
##A-Record
##Create WMI-Class
$rec = [Wmiclass]'\\<your DNS-Server>\root\MicrosoftDNS:MicrosoftDNS_AType'
#set properties
$server = "<your DNS-Server>.domain.net"
$zone = "domain.net"
$name = "$systemname"
$class = 1
$TTL = 1200
$address = "$ipaddress"
##Create A-Record
$rec.CreateInstanceFromPropertyData($server,$zone,$name,$class,$TTL,$address)

##PTR-Record
##Create WMI-Class
$rec = [Wmiclass]'\\<your DNS-Server>\root\MicrosoftDNS:MicrosoftDNS_PTRType'
#$rec | gm
##set properties
$server = "<your DNS-Server>.domain.net"
$zone = "xy.in-addr.arpa"
$name = "$revertipaddress"
$class = 1
$TTL = 1200
$address = "$systemname"
##Create PTR-Record
$rec.CreateInstanceFromPropertyData($server,$zone,$name,$class,$TTL,$address)
}
else
{"There is no NIC" >> "c:\temp\DNSentry.txt"}
}

##Check Entry
$Arec = Get-WmiObject -ComputerName <your DNS-Server> -Namespace ‘root\MicrosoftDNS’ -Class MicrosoftDNS_ATYPE -filter 'ContainerName = "domain.net"' | ? {$_.OwnerName -like "$systemname" } # | select -first 1
$PTRrec = Get-WmiObject -ComputerName <your DNS-Server> -Namespace ‘root\MicrosoftDNS’ -Class MicrosoftDNS_PTRTYPE -filter 'ContainerName = "10.in-addr.arpa"' | ? {$_.OwnerName -like $revertipaddress +"*"} # | select -first 1
$DNScheck = 4

""  >> "c:\temp\DNSentry.txt"
"A-Record"  >> "c:\temp\DNSentry.txt"
""  >> "c:\temp\DNSentry.txt"
IF($Arec)
        {$Arec >> "c:\temp\DNSentry.txt"}
      else
        {"there is no A-Record" >> "c:\temp\DNSentry.txt"}
""  >> "c:\temp\DNSentry.txt"       
"PTR-Record"  >> "c:\temp\DNSentry.txt"
        
IF($PTRrec)
        {$PTRrec >> "c:\temp\DNSentry.txt"}
      else
        {"there is no PTR-Record" >> "c:\temp\DNSentry.txt"}