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

Viewing 3 reply threads
  • Author
    Posts
    • #104311
      Participant
      Topics: 1
      Replies: 2
      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
      Topics: 13
      Replies: 4872
      Points: 1,896
      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
        Topics: 1
        Replies: 2
        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
      Topics: 13
      Replies: 4872
      Points: 1,896
      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
      Topics: 2
      Replies: 6
      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
        Topics: 1
        Replies: 2
        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.”

         

Viewing 3 reply threads
  • The topic ‘Basic DNS Record A Creation Script’ is closed to new replies.