Basic DNS Record A Creation Script

This topic contains 3 replies, has 3 voices, and was last updated by  bob 6 days ago.

  • Author
    Posts
  • #104311

    Jaroslav Useche
    Participant

    Hi,

    Since we are continously creating DNS records for some of our Linux-Based Server we came up with the idea idea of doing it a little bit more automatize so we though using powershell for it and this was the result:

    #Import CSV File
    $Data = import-csv -Delimiter ";" -Path .\DNS_List.csv
    
    #Creation of the Session with the DC
    $Session = New-PSSession -ComputerName CWVEP009
    
    #Creation of DNS Records according to the CSV File
    $Data | ForEach-Object {
    
        $Name = $_.name
        $ZoneName = $_.zonename
        $Ip = [IPAddress]$_.ip
     
     
        Write-Output $Name
        Write-Output $ZoneName
        Write-Output $Ip
                 
        Invoke-Command -Session $Session -ScriptBlock {Add-DnsServerResourceRecordA -Name '$Name' -ZoneName '$ZoneName' -AllowUpdateAny -IPv4Address '$Ip.IPAddressToString' -TimeToLive 01:00:00}
       
    }
    

    Unfortunately, we were not succesfull with our goal yet because we are getting this error over a over again:

    Cannot process argument transformation on parameter 'IPv4Address'. Cannot convert value "$Ip.IPAddressToString" to type "System.Net.IPAddress[]". Error:
    "Cannot convert value "$Ip.IPAddressToString" to type "System.Net.IPAddress". Error: "An invalid IP address was specified.""
    + CategoryInfo : InvalidData: (:) [Add-DnsServerResourceRecordA], ParameterBindin...mationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Add-DnsServerResourceRecordA
    + PSComputerName : DC01Test

    I ran out of ideas... have you faced something similar??

  • #104320

    Don Jones
    Keymaster

    So, you're doing a couple of odd things. For one, this:

    '$Ip.IPAddressToString'
    

    Won't work. Single quotes don't have the magic variable stuff, and even in double quotes you can't call methods and properties all naked like that.

    "$($Ip.IPAddressToString)"
    

    Is what you likely mean. But I don't understand why you're doing this; you've cast $_.ip as [ipaddress], but then you're just returning a string. Why not just use $_.ip as-is, instead of casting it back and forth?

  • #104321

    Don Jones
    Keymaster

    (and those Write-Output commands should technically be Write-Verbose, as what you're currently outputting is not the intended "result" of your script... but this is a nit-pick, not a functional problem)

  • #104324

    bob
    Participant

    Why are you converting $Ip back to a string?

        Invoke-Command -Session $Session -ScriptBlock {Add-DnsServerResourceRecordA -Name '$Name' -ZoneName '$ZoneName' -AllowUpdateAny -IPv4Address '$Ip.IPAddressToString' -TimeToLive 01:00:00}
    
    

    Call it like this

        Invoke-Command -Session $Session -ScriptBlock {Add-DnsServerResourceRecordA -Name '$Name' -ZoneName '$ZoneName' -AllowUpdateAny -IPv4Address $Ip -TimeToLive 01:00:00}
    
    

    https://docs.microsoft.com/en-us/powershell/module/dnsserver/add-dnsserverresourcerecord?view=win10-ps

You must be logged in to reply to this topic.