Bulk Update Microsoft DNS (Multi Zone, Multi Data)

Welcome Forums General PowerShell Q&A Bulk Update Microsoft DNS (Multi Zone, Multi Data)

This topic contains 1 reply, has 2 voices, and was last updated by

 
Moderator
1 month, 1 week ago.

  • Author
    Posts
  • #170815

    Participant
    Topics: 1
    Replies: 0
    Points: 12
    Rank: Member

    Hi All,

     

    What am I doing wrong with the code below?

    $data=@()
    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:

    domain1.co.uk,cloudflare-verify,000-000
    domain2.co.uk,cloudflare-verify,000-001

    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

  • #170854

    Moderator
    Topics: 0
    Replies: 36
    Points: 331
    Team MemberHelping Hand
    Rank: Contributor

    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
    }
    

You must be logged in to reply to this topic.