PowerShell: Invoke-Command to Add A Host Record on DNS Server

# DNS Host Record Information
$aRecord="superman"
$recordIP='192.168.0.256'
$zoneName='kimconnect.com'
$dnsServer='dc01.intranet.kimconnect.com'

# Admin Credential
$adminUsername='DoeManeAdmin'
$adminPassword='WhatPassword?'
$adminCredential=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $adminUsername,$(ConvertTo-securestring $adminPassword -AsPlainText -Force)
# Optional
# Autogen variable to get PDC and set it as $dnsServer variable
#if (!(get-module activedirectory)){Install-WindowsFeature RSAT-AD-PowerShell -Confirm:$false} # this only works on server-type OS
#$pdc=(Get-ADForest |Select-Object -ExpandProperty RootDomain |Get-ADDomain |Select-Object -Property PDCEmulator).PDCEmulator; 
# Optional: checking a host record remotely without making changes
function checkARecordRemoteServer{
    param(        
        $dnsServer,
        $adminCred,
        $record,
        $zone
    )
    #$resolve=Resolve-DnsName -Name $record -Server $dnsServer -zonename $zoneName -QuickTimeout -ea SilentlyContinue
    # Install-WindowsFeature DNS -IncludeManagementTools
    invoke-command -ComputerName $dnsServer -Credential $adminCred -ScriptBlock{
        param($aRecord,$zone)
        import-module dnsserver
        $resolve=Get-DnsServerResourceRecord -Name $aRecord -ZoneName $zone -ea SilentlyContinue
        if($resolve.HostName -ne $null){
            write-host "$env:computername has records of $aRecord in zone $zone as $($resolve.RecordData.IPv4Address.IPAddressToString)`: YES!`r`n"
            return $true
            }else{
                write-warning "$env:computername has records of $aRecord`: NO!`r`n";
                return $false
                }
        } -Args $record,$zone
}

# checkARecordRemoteServer -dnsServer $dnsServer -adminCred $adminCredential -record $aRecord -zone $zoneName;
# Add a Host Record onto remote DNS Server in a Specific Zone
function addARecordRemoteServer{
    param(        
        $dnsServer,
        $adminCred,
        $record,
        $ip,
        $zone
    )
    invoke-command -ComputerName $dnsServer -Credential $adminCred -ScriptBlock{
        param($aRecord,$ip,$zone)
        import-module dnsserver
        $resolve=Get-DnsServerResourceRecord -Name $aRecord -ZoneName $zone -ea SilentlyContinue
        if($resolve.HostName -ne $null){
            write-warning "$env:computername already has a record of $aRecord in zone $zone as $($resolve.RecordData.IPv4Address.IPAddressToString)!"
            }else{
                try{
                    Add-DnsServerResourceRecordA -Name $aRecord -IPv4Address $ip -ZoneName $zone -AllowUpdateAny -TimeToLive 01:00:00 -Confirm:$false -EA Stop
                    write-host "$aRecord $ip has been added to zone $zone on $env:computername"
                    }
                catch{
                    write-warning $Error
                    write-warning "Unable to add record $aRecord to DNS server $env:computername";
                    }
                }
        } -Args $record,$ip,$zone
}

addARecordRemoteServer -dnsServer $dnsServer -adminCred $adminCredential -record $aRecord -ip $recordIp -zone $zoneName;

Leave a Reply

Your email address will not be published. Required fields are marked *