Basic DNS Record A Creation Script

Welcome Forums General PowerShell Q&A Basic DNS Record A Creation Script

This topic contains 5 replies, has 3 voices, and was last updated by

 
Participant
3 months, 2 weeks ago.

  • Author
    Posts
  • #104311

    Participant
    Points: 0
    Rank: Member

    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

    Keymaster
    Points: 1,704
    Helping HandTeam Member
    Rank: Community Hero

    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?

    • #110092

      Participant
      Points: 0
      Rank: Member

      Hi Don,

      I get the following when I run the script with "$($Ip.IPAddressToString)" suggestion:

      Cannot process argument transformation on parameter 'IPv4Address'. Cannot convert value "" to type "System.Net.IPAddress[]". Error: "Cannot convert value "" to type

      "System.Net.IPAddress". Error: "An invalid IP address was specified.""

  • #104321

    Keymaster
    Points: 1,704
    Helping HandTeam Member
    Rank: Community Hero

    (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
    Points: 1
    Rank: Member

    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

    • #110090

      Participant
      Points: 0
      Rank: Member

      Hi bob,

      If I call it like that I get the following error:

      "Cannot validate argument on parameter 'IPv4Address'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again."

       

The topic ‘Basic DNS Record A Creation Script’ is closed to new replies.