Bulk Update Microsoft DNS (Multi Zone, Multi Data)

Hi All,


What am I doing wrong with the code below?

write-host $data
$data = Import-CSV "C:\scripts\Data.csv" -header ZoneName,Name,DescriptiveText
ForEach-Object { 
Add-DnsServerResourceRecord -ZoneName $data.ZoneName -Txt -Name $data.Name -DescriptiveText $data.DescriptiveText

CSV Example:


I get the following error:

Add-DnsServerResourceRecord : Cannot process argument transformation on parameter 'ZoneName'. Cannot convert value to type System.String.

Tried playing around with [string] arguments but I’m doing something wrong.
I’m not great with PS so any help is welcome!

Thank you

I think you’ll want to provide an InputObject for the foreach, and then use the inbuilt $_/$PSItem variable to operate on each item rather than passing the entire $Data object each time.

$Data = Import-CSV "C:\scripts\Data.csv" -Header ZoneName,Name,DescriptiveText
Write-Host "Data contains '$($Data.Count)' items"

$Data | ForEach-Object {
    Add-DnsServerResourceRecord -ZoneName $_.ZoneName -Name $_.Name -DescriptiveText $_.DescriptiveText