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"}

Donnerstag, 23. Februar 2012

Rename your network adapter using Powershell and netsh

Often I have the problem that my network adapters are labeled wrong. So I want to rename them. The easiest way to rename your adapter is to use netsh. But if you want to you netsh you are limited to one adapter. What would you do if you want to do this dynamicly and for more than one adapter, doesn't matter how much NIC are installed. Yes, use powershell!!!!


In this example I will show you how to get the name of a network adapter with a specific ip address.
First you have to get all of your adapters by using WMI. You can find all installed adpaters in the win32_networkadapter and win32_networkadapterconfiguration classes.
We start to get all instances of the win32_networkadapterconfiguration:
Type gwmi win32_networkadapterconfiguration


you will get more than 1 result, also if you only have one network card.

The network interface I want to change is "Atheros AR5B97 Wireless Network Adapter".
What we need is the index of your network interface, so we have pipe our output into a where-object:
Type  gwmi win32_networkadapterconfiguration | ? {$_.ipaddress -like "*192.168*"}
Instead of my *192.168* you can type your own ip address.


we need this later again, so we put this command into a variable. and let us show the index:


If you have more than one interface with ip like "192.168*" this will fail, then you have to use a foreach loop.


The rest is easy now. You have to use the index of your interface to get the networkconnectionID from the win32_networkadapter class:
Type: 
 $networkadapterID = gwmi win32_networkadapter | ? { $_.index -eq ($networkadapter).index}
 ($networkadapterID).netconnectionID

Your Output will be the same as it is in your control center:
To use netsh you have to know how it is labeled.
The netsh command to rename your network inerface is:
netsh interface set interface "<your interface name>" newname="NewName"


How to include this into powershell?
This is also very simple:
First you have to put this command into a variable:
Type:   
$input = @"
interface set interface "$oldinterfacename" newname="NewName"
"@
Second and last step is to pipe this input into netsh:
Type: $input | netsh
 You are finished now.




To give you one more example, I have used this commands for more than one interface.


$adapter = gwmi win32_networkadapterconfiguration | ? { $_.IPaddress -like "*192.168*"}
$i = 1
foreach($element in $adapter){
$adapterindex = $element.index
$newname = "Adapter"+$i
$adapterID = gwmi win32_networkadapter | ?{$_.index -eq $adapterindex}
$adapter = ($adapterID).NetConnectionID
    $input = @"
interface set interface "$adapter" newname="$newname"
exit
"@
$input
$input | netsh
$i++
}

I hope this will help you, and please forgive my bad teaching skills. this is my first time :D