Script to Change Permissions on DNS Records

SethRoberts-0739 31 Reputation points
2021-04-13T15:48:49.07+00:00

I've found myself in a situation where some computers in our environment are not able to update their records in DNS when their IP address changes. This is primarily due to DHCP servers creating the record on their behalf when the DHCP server issues or renews leases. Needless to say, PowerShell isn't my strength. I need to find a way to add an ACL for the computer object to have modify rights of its DNS record. Scripting this seems like the fastest and most reliable way to accomplish this. I would very much appreciate any help the PowerShell experts can provide.
Best,
Seth

Windows for business Windows Client for IT Pros Networking Network connectivity and file sharing
Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} vote

Accepted answer
  1. Anonymous
    2021-04-14T07:57:47.297+00:00

    Hi,

    Please see if this works for you. Set $ComputerNames to your actual computer names.

    $ComputerNames = "computer1","computer2","computer3"  
    foreach($ComputerName in $ComputerNames){  
        $DNSServer = (Get-ADDomain).PDCEmulator   
        $ZoneNames = Get-DnsServerZone -ComputerName $DNSServer  
        $DNSRecord = foreach($ZoneName in $ZoneNames ){  
            Get-DnsServerResourceRecord -ComputerName $DNSServer -ZoneName $ZoneName.ZoneName | Where-Object {$_.hostname -eq $ComputerName}  
        }  
        $ADcomputer = Get-ADComputer -Identity $ComputerName  
        $SID = New-Object System.Security.Principal.SecurityIdentifier $ADcomputer.SID.Value  
        Push-Location -Path AD:\  
        $ACL = Get-Acl -Path $DNSRecord.DistinguishedName  
        $ACE = New-Object System.DirectoryServices.ActiveDirectoryAccessRule $SID, "GenericAll", "Allow"  
        $ACL.AddAccessRule($ACE)  
        $ACL | Set-Acl -Path $DNSRecord.DistinguishedName  
        Pop-Location  
    }  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Mohamed SAKHO 126 Reputation points
    2021-10-11T14:42:15.4+00:00

    Hello,

    Thank you very much!This helped me IMMENSELY!
    But I just want to know if it's possible to log it. and show me the record that not exist.
    Thank you by advance

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.