Link to home
Start Free TrialLog in
Avatar of jskfan
jskfanFlag for Cyprus

asked on

Add DNS records zones to an existing zone.


I have a dns primary zone  Test.com in domain1 that has about 50 records there is another dns zone from anothe domain (domain2), the dns zone has also the same name TEST.com and it has about 80 records.

I would like to add the records from the TEST.com zone in domain2 to the TEST.com zone in domain1

I looked at the DNSCMD command and didn't see a switch that can do that.

Any help?

Thanks
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image


Heh no, there wouldn't be a switch to do that. You'd have to script it because it's way outside normal DNS functionality.

If the zones are supposed to be the same, why isn't one of them Secondary?

Chris
Avatar of jskfan

ASKER

is it possible if they have different names ?

It's not "test.com" in both cases? DNS isn't case sensitive if that's the only difference.

Chris
Avatar of jskfan

ASKER

I mean if you have record in a zone named AA.com and you want to add those records to a zone name BB.com without overwriting the existing records

Then we definitely need some scripting, nothing else will do it.

Do you care which language it's in? If you don't I'll probably make it PowerShell :)

Chris
Avatar of jskfan

ASKER

that's english.

Okay, well I'll get on with that first thing in the morning unless something blows up here :)

Chris
ASKER CERTIFIED SOLUTION
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jskfan

ASKER



I pasted your code to a notepad and created myscript.vbs file.
I am getting this error:

Script C:\document and settings\administrator.mydomainname\desktop\myscript.vbs
line:1
char:1
Error:Invalid Charachter
code:800A0408
Source:Microsoft VBScript compilation error

PowerShell rather than VbScript, you'll need this to be able to run it :)

http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx

It'll paste into the command prompt.

I can rewrite it in VbScript if you want, it's just PowerShell is more fun / less code ;)

Chris

> It'll paste into the command prompt.

Er this isn't a very clear statement. I mean you can paste it into the PowerShell prompt, it'll (hopefully) become clear if you install it :)

Chris
Avatar of jskfan

ASKER

That's what I get when I run it

Windows PowerShell
Copyright (C) 2006 Microsoft Corporation. All rights reserved.

PS C:\Documents and Settings\administrator.mycompany> $SrcServer = "myDNSserver.mycompany.com"
PS C:\Documents and Settings\administrator.mycompany> $SrcCredential = "mydomain\myuseraccount"
PS C:\Documents and Settings\administrator.mycompany> $SrcZone = "mycompanyemailservices.net"
PS C:\Documents and Settings\administrator.mycompany>
PS C:\Documents and Settings\administrator.mycompany> $DstServer = "myDNSserver"
PS C:\Documents and Settings\administrator.mycompany> $DstZone = "mycompany.com"
PS C:\Documents and Settings\administrator.mycompany>
PS C:\Documents and Settings\administrator.mycompany> $Classes = @("MicrosoftDNS_AType", "MicrosoftDNS_CNAMEType")
PS C:\Documents and Settings\administrator.mycompany>
PS C:\Documents and Settings\administrator.mycompany> $NewRRClass = [WMIClass]"\\$DstServer\root\MicrosoftDNS:Microsoft
ResourceRecord"
PS C:\Documents and Settings\administrator.mycompany>
PS C:\Documents and Settings\administrator.mycompany> ForEach ($Class in $Classes) {
>>
>>   $SrcRecords = Get-WMIObject -Namespace "root\MicrosoftDNS" -Class $Class `
>>     -Computer $SrcServer -Filter "ContainerName='$SrcZone'" `
>>     -Credential $SrcCredential | Select-Object TextRepresentation
>>
>>   $DstRecords = Get-WMIObject -Namespace "root\MicrosoftDNS" -Class $Class `
>>     -Computer $DstServer -Filter "ContainerName='$DstZone'" `
>>    | Select-Object TextRepresentation
>>
>>   ForEach ($Record in $SrcRecords) {
>>     $DstTextRepresentation = ($Record.TextRepresentation).Replace($SrcZone, $DstZone)
>>
>>     If (!($DstRecords | ?{ $_.TextRepresentation -eq $DstTextRepresentation })) {
>>       $NewRR = $NewRRClass.CreateInstanceFromTextRepresentation( `
>>         $DstServer, $DstZone, $DstTextRepresentation)
>>     }
>>   }
>> }
>>
Get-WmiObject : User credentials cannot be used for local connections
At line:2 char:30
+   $SrcRecords = Get-WMIObject  <<<< -Namespace "root\MicrosoftDNS" -Class $Class `
You cannot call a method on a null-valued expression.
At line:9 char:66
+     $DstTextRepresentation = ($Record.TextRepresentation).Replace( <<<< $SrcZone, $DstZone)
Exception calling "CreateInstanceFromTextRepresentation" : "Invalid parameter "
At line:11 char:64
+       $NewRR = $NewRRClass.CreateInstanceFromTextRepresentation( <<<<  `
Get-WmiObject : User credentials cannot be used for local connections
At line:2 char:30
+   $SrcRecords = Get-WMIObject  <<<< -Namespace "root\MicrosoftDNS" -Class $Class `
You cannot call a method on a null-valued expression.
At line:9 char:66
+     $DstTextRepresentation = ($Record.TextRepresentation).Replace( <<<< $SrcZone, $DstZone)
Exception calling "CreateInstanceFromTextRepresentation" : "Invalid parameter "
At line:11 char:64
+       $NewRR = $NewRRClass.CreateInstanceFromTextRepresentation( <<<<  `
PS C:\Documents and Settings\administrator.mycompany>

Where are you running it from? It's only configured to run from the Destination Server at the moment. I'll see if I can fix the reason for that.

Chris
Avatar of jskfan

ASKER

all the zones are in one DNS server. I just want to add records from one zone to another without overwriting the records in the zone.

Okay, got it. Give me a moment, I'll make it flexible.

Chris

Oh really? Thought they were separate servers.

Well that makes life a lot easier :)

Here we go. No more credential prompts, and no more server settings, it picks up the current server name from the environmental variables, you don't need to change that one.

Chris
$SrcZone = "testsrc.com"
$DstZone = "testdst.com"
 
$Server = $Env:ComputerName
$Classes = @("MicrosoftDNS_AType", "MicrosoftDNS_CNAMEType")
 
$NewRRClass = [WMIClass]"\\$Server\root\MicrosoftDNS:MicrosoftDNS_ResourceRecord"
 
ForEach ($Class in $Classes) {
  $SrcRecords = Get-WMIObject -Namespace "root\MicrosoftDNS" -Class $Class `
    -Filter "ContainerName='$SrcZone'" | Select-Object TextRepresentation
 
  $DstRecords = Get-WMIObject -Namespace "root\MicrosoftDNS" -Class $Class `
    -Filter "ContainerName='$DstZone'" | Select-Object TextRepresentation 
 
  ForEach ($Record in $SrcRecords) {
    $DstTextRepresentation = ($Record.TextRepresentation).Replace($SrcZone, $DstZone)
    If (!($DstRecords | ?{ $_.TextRepresentation -eq $DstTextRepresentation })) {
      $NewRR = $NewRRClass.CreateInstanceFromTextRepresentation( `
        $Server, $DstZone, $DstTextRepresentation)
    }
  }
}

Open in new window

Avatar of jskfan

ASKER

`I got this :

You cannot call a method on a null-valued expression.
At line:7 char:66
+     $DstTextRepresentation = ($Record.TextRepresentation).Replace( <<<< $SrcZone, $DstZone)
You cannot call a method on a null-valued expression.
At line:9 char:64
+       $NewRR = $NewRRClass.CreateInstanceFromTextRepresentation( <<<<  `
You cannot call a method on a null-valued expression.
At line:7 char:66
+     $DstTextRepresentation = ($Record.TextRepresentation).Replace( <<<< $SrcZone, $DstZone)
You cannot call a method on a null-valued expression.
At line:9 char:64
+       $NewRR = $NewRRClass.CreateInstanceFromTextRepresentation( <<<<  `on( <<<<  `

Run this on its own?

Get-WMIObject -Namespace "root\MicrosoftDNS" -Class $Class `
    -Filter "ContainerName='$SrcZone'" | Select-Object TextRepresentation

That should return the records in the source zone... but it would seem it's not here (from the above message).

Chris

Oops, that won't work too well, $Class isn't defined. Give these a try instead please:


Get-WMIObject -Namespace "root\MicrosoftDNS" -Class "MicrosoftDNS_AType" `
    -Filter "ContainerName='$SrcZone'" | Select-Object TextRepresentation

Get-WMIObject -Namespace "root\MicrosoftDNS" -Class "MicrosoftDNS_CNAMEType" `
    -Filter "ContainerName='$SrcZone'" | Select-Object TextRepresentation

Chris
Avatar of jskfan

ASKER

I tried it by itself, it doesn't do anything.
Avatar of jskfan

ASKER


I found out that this command works fine and doesn't overwrite the records in the zone.
dnscmd /recordadd destinationzone @ A sourcezone.

the only thing that it can't do is to copy records like these ones:
abc      Mail Exchanger (MX)      [10]  10.230.15.20
(same as parent folder)      Mail Exchanger (MX)      [10]  10.230.15.20.

> I found out that this command works fine and doesn't overwrite the records in the zone.

Really?

Just makes an error if you run that on mine, rightly so the syntax passed with the command is rubbish ;)

> I tried it by itself, it doesn't do anything.

What value was assigned to $SrcZone? I should have noted that the value for that is still required.

Chris
Avatar of jskfan

ASKER

I assigned $SrcZone the source zone name I am adding to the destination zone

Hmmm something wrong there then, it should have told us about the records in the zone. Lets go with just this:


$SrcZone = "source.com"

Get-WMIObject -Namespace "root\MicrosoftDNS" -Class "MicrosoftDNS_AType" `
    -Filter "ContainerName='$SrcZone'"

Get-WMIObject -Namespace "root\MicrosoftDNS" -Class "MicrosoftDNS_CNAMEType" `
    -Filter "ContainerName='$SrcZone'"

Anything returned is better than nothing.

Chris
Avatar of jskfan

ASKER


I got this:

PS C:\Documents and Settings\administrator.mydomain> $SrcZone = "sourcezonename"
PS C:\Documents and Settings\administrator.mydomain> Get-WMIObject -Namespace "root\MicrosoftDNS" -Class "MicrosoftDNS_ATy
pe" `
>>     -Filter "ContainerName='$SrcZone'"
>> Get-WMIObject -Namespace "root\MicrosoftDNS" -Class "MicrosoftDNS_CNAMEType" `
>>     -Filter "ContainerName='$SrcZone'"
>>

Lets make the search less specific, if you could run:

Get-WMIObject -Namespace "root\MicrosoftDNS" -Class "MicrosoftDNS_AType"

That should return all A records from all zones.

If that works, we need to confirm the zone name, because it must be wrong. If it doesn't, something is wrong with WMI which is rather more annoying :)

Chris
Avatar of jskfan

ASKER

Get-WMIObject -Namespace "root\MicrosoftDNS" -Class "MicrosoftDNS_AType"

yes this worked

Oops forgot to reply.

If the above worked it means the zone name you're entering doesn't match the DNS servers view of it.

You can see all the possible names we can use for $SrcZone with the below.

Chris
Get-WMIObject -Namespace "root\MicrosoftDNS" -Class "MicrosoftDNS_Zone" `
  | Select-Object ContainerName

Open in new window

Avatar of jskfan

ASKER

sorry... I am  a bit busy. I will get back with you later.

No worries, no rush :)

Chris