How to validate deleting a DNS entry with powershell
<p>I have a (at least what I think) tricky problem with a Server 2008 R2 domain. I wrote a cleanup script for AD computer accounts. Beside the account in AD I want to delete SCCM and DNS accounts. But with DNS I have a problem. I need to log everything as my script will run timed as a job each day. 
With the normal AD module cmdlets all work great by using something like this:</p>

<pre><code>Remove-ADComputer -Identity $account -confirm:$false

if($?){
    Write-Log -LogContent "Delete-OldADaccount: successfully deleted account `"$($account.name)`" with LastLogondate `"$($account.LastLogondate)`", full path `"$($account.distinguishedname)`"" -LogPath $logFile
} else {
    Write-Log -LogContent "Delete-OldADaccount: failed to delete account `"$($account.name)`": $($error[0].Exception.Message)" -LogPath $logFile -Level 'Warn'
}
</code></pre>

<p>For deleting old DNS entries I found two solutions for Server 2008 R2 (I can't use those cool new Server 2012 DNS modules for ps):</p>

<pre><code>dnscmd $DNSServer /RecordDelete $ZoneName $Computer A /f
</code></pre>

<p>and</p>

<pre><code>Get-WmiObject -ComputerName $dnsserver -Namespace 'Root\MicrosoftDNS' -class MicrosoftDNS_AType  -Filter "domainname = '$computer'" | Remove-WmiObject
</code></pre>

<p>But both commands (dnscmd and Remove-WmiObject) always return true, even if there were no records in DNS matching my computer account's name. So I cant use a similar construct as above.</p>

<p>So I tried something like this:</p>

<pre><code>try{
    [System.Net.DNS]::GetHostEntry($computer)
    Get-WmiObject -ComputerName $dns -Namespace 'Root\MicrosoftDNS' -class MicrosoftDNS_AType  -Filter "domainname = '$computer'" | Remove-WmiObject -whatif
    Get-WmiObject -ComputerName $dns -Namespace 'Root\MicrosoftDNS' -class MicrosoftDNS_AAAAType  -Filter "domainname = '$computer'" | Remove-WmiObject -whatif
    Write-Log -LogContent "Delete-OldADaccount: successfully deleted DNS entry for `"$($computer)`"" -LogPath $logFile
}
catch {
    Write-Log -LogContent "Delete-OldADaccount: failed to delete DNS entry for `"$($computer)`": $($error[0].Exception.Message)" -LogPath $logFile -Level 'Warn'
}
</code></pre>

<p>With the static function <code>[System.Net.DNS]::GetHostEntry($computer)</code> I test if there is at least an ipv4 entry (as ipv6 is deactivated on my system I would get an exception if there is only an ipv6 entry. If both ipv4 and ipv6 exist it works). If there is an entry it proceeds with the Remove-WmiObject cmdlet for ipv4 and ipv6. 
If there is no such entry in DNS I get an exception and directly jump into the catch-block where I log the error.
But even with this method I have no clue later if the Remove-WmiObject was successful. I would have to do a <code>ipconfig /flushdns</code> and re-run the command <code>[System.Net.DNS]::GetHostEntry($computer)</code> to see if it now fails and interpret this as "entries deleted". </p>

<p>Please, is there another cmdlet or way for Server 2008 R2 to delete an entry from DNS and validate if the deletion was successful? Help ;)</p>