Update multiple DNS records with CSV and foreach loop

Welcome Forums General PowerShell Q&A Update multiple DNS records with CSV and foreach loop

  • This topic has 2 replies, 2 voices, and was last updated 2 weeks ago by
    Participant
    .
Viewing 2 reply threads
  • Author
    Posts
    • #283765
      Participant
      Topics: 2
      Replies: 2
      Points: 28
      Rank: Member

      Hi,

      I am trying to update multiple DNS records using a CSV file and a foreach loop. Adding multiple records with CSV and foreach loop works fine.

      CSV file (file.csv):

      Working addition of records:

      I can update individual records as:

      However, trying to incorporate the update into a CSV and foreach loop ends up adding additional records (2 records for test1, test2, etc., one record with original IP and one record with new IP):

      CSV file:

      Non working code that creates additional records with new IP:

      Can anyone shed some light on my mistake here?

      Thanks,

      HB

    • #283783
      Participant
      Topics: 2
      Replies: 2
      Points: 28
      Rank: Member

      Well, it looks like the resolution was indeed simple.  Removing the single quotes from the ‘parse’ argument allows the loop to succeed:

      I’m open to any suggestions if there is a better way to do this..

      Thanks,

      HB

    • #283975
      Participant
      Topics: 5
      Replies: 249
      Points: 985
      Helping Hand
      Rank: Major Contributor

      Just to add some education to this.  Single quotes are verbatim strings (no interpolation), so in the ‘$_.ip’ is literally that string not the ip property of the current item in the foreach-object loop.  Recommend reviewing Get-Help about_quoting_rules

Viewing 2 reply threads
  • You must be logged in to reply to this topic.