# Update multiple DNS records with CSV and foreach loop

**URL:** <https://forums.powershell.org/t/update-multiple-dns-records-with-csv-and-foreach-loop/15452>\
**Category:** PowerShell Help\
**Created:** [January 2, 2021, 9:40am UTC](https://forums.powershell.org/t/update-multiple-dns-records-with-csv-and-foreach-loop/15452 "2021-01-02T09:40:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hburnswell](https://avatars.discourse-cdn.com/v4/letter/h/9e8a1a/32.png) [@hburnswell](https://forums.powershell.org/u/hburnswell)\
**Post date:** [January 2, 2021, 9:40am UTC](https://forums.powershell.org/t/update-multiple-dns-records-with-csv-and-foreach-loop/15452/1 "2021-01-02T09:40:09Z")

</div>

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):

```
name,ip
test1,10.10.105.240
test2,10.10.105.241
test3,10.10.105.242
test4,10.10.105.243
```

Working addition of records:

```
Import-Csv .\file.csv | foreach {

	Add-DnsServerResourceRecordA `
	-Name $_.name `
	-ZoneName example.com `
	-IPv4Address $_.ip

	}
```

I can update individual records as:

```
$old = Get-DnsServerResourceRecord -ZoneName example.com -Name test1
$new = $old.Clone()
$new.RecordData.IPv4Address = [System.Net.IPAddress]::parse('10.10.106.35')
Set-DnsServerResourceRecord -NewInputObject $new -OldInputObject $old -ZoneName example.com
```

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:

```
name,ip
test1,10.10.106.240
test2,10.10.106.241
test3,10.10.106.242
test4,10.10.106.243
```

Non working code that creates additional records with new IP:

```
Import-Csv .\file.csv | foreach {

	$old = Get-DnsServerResourceRecord -ZoneName example.com -Name $_.name
	$new = $old.Clone()
	$new.RecordData.IPv4Address = [System.Net.IPAddress]::parse('$_.ip')
	Set-DnsServerResourceRecord -NewInputObject $new -OldInputObject $old -ZoneName example.com

	}
```

Can anyone shed some light on my mistake here?

Thanks,

HB

---

<div class="post-metadata">

**Author:** ![hburnswell](https://avatars.discourse-cdn.com/v4/letter/h/9e8a1a/32.png) [@hburnswell](https://forums.powershell.org/u/hburnswell)\
**Post date:** [January 2, 2021, 11:14am UTC](https://forums.powershell.org/t/update-multiple-dns-records-with-csv-and-foreach-loop/15452/2 "2021-01-02T11:14:26Z")

</div>

Well, it looks like the resolution was indeed simple. Removing the single quotes from the 'parse' argument allows the loop to succeed:
```
Import-Csv .\file.csv | foreach {

$old = Get-DnsServerResourceRecord -ZoneName [example.com](http://example.com) -Name $_.name  
$new = $old.Clone()  
$new.RecordData.IPv4Address = [System.Net.IPAddress]::parse($_.ip)  
Set-DnsServerResourceRecord -NewInputObject $new -OldInputObject $old -ZoneName [example.com](http://example.com)

}
```
  
I’m open to any suggestions if there is a better way to do this…

Thanks,

HB

---

<div class="post-metadata">

**Author:** ![ralphmwr](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ralphmwr/32/1064_2.png) [@ralphmwr](https://forums.powershell.org/u/ralphmwr)\
**Post date:** [January 4, 2021, 3:15am UTC](https://forums.powershell.org/t/update-multiple-dns-records-with-csv-and-foreach-loop/15452/3 "2021-01-04T03:15:46Z")

</div>

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](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules?view=powershell-5.1)

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:28pm UTC](https://forums.powershell.org/t/update-multiple-dns-records-with-csv-and-foreach-loop/15452/4 "2024-05-16T20:28:06Z")

</div>

Closed on May 16, 2024, 8:28pm UTC


