I have a script that goes though a list of servers and checks to see if the DNS entries are set to a specific value.
I am set an environment variable when I make the WMI call to catch any errors and both write them to screen and to a text file. It is writing a truncated version of the error (which is what I want) on everything except "access denied". On that error it dumps the entire stack.
My question is, how do I get the "access denied" error stack down to just reporting something like Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)).
What is strange is that when I run the script then just type $err and hit enter I get the following:
PS C:\myScripts> $err Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
When I try to Write-Host $err or Add-Content $err I get the whole error stack. (I can post it if you want...just trying to save space.)
script:
$ErrorActionPreference = "SilentlyContinue"
Clear-Content C:\myScripts\iofiles\dns.txt
$servers = Get-Content "C:\myScripts\iofiles\hosts.txt"
#Set the DNS values to check for
$newDNS1 = "1.1.1.1" #placeholder value
$newDNS2 = "1.1.1.2" #placeholder value
ForEach($server in $servers){
Write-Host ""
Write-Host "Checking" $server -ForegroundColor Cyan
$wmi = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -ComputerName $server -filter "ipenabled='true'" -ea "SilentlyContinue" -ev err
$primaryDNS = $wmi.DNSServerSearchOrder[0]
$secondaryDNS = $wmi.DNSServerSearchOrder[1]
if ($err.count -gt 0) {
Add-Content C:\myScripts\iofiles\dns.txt "$server - $err"
Write-Host "Error on $name $err" -ForegroundColor Red
}else{
if ($primaryDNS -ne $newDNS1) {
Add-Content C:\myScripts\iofiles\dns.txt "$server - Primary DNS incorrectly set - $primaryDNS"
Write-Host "Primary DNS incorrectly set: " -ForegroundColor Yellow -NoNewline; Write-Host $primaryDNS -ForegroundColor Red
}else{
Write-Host "Primary DNS Correct" -ForegroundColor Green
}if ($secondaryDNS -ne $newDNS2) {
Add-Content C:\myScripts\iofiles\dns.txt "$server - Secondary DNS incorrectly set - $secondaryDNS"
Write-Host "Secondary incorrectly set: " -ForegroundColor Yellow -NoNewline; Write-host $secondaryDNS -ForegroundColor Red
}else{
Write-Host "Secondary DNS correct" -ForegroundColor Green
}
}
}#END ForEach
Probably you suffer from a DCOM related error:
0x80070005 – E_ACCESS_DENIED: Access denied by DCOM security.
The user does not have remote access to the computer through DCOM. Typically, DCOM errors occur when connecting to a remote computer with a different operating system version.
Give the user Remote Launch and Remote Activation permissions in dcomcnfg. Right-click My Computer-> Properties Under COM Security, click "Edit Limits" for both sections. Give the user you want remote access, remote launch, and remote activation. Then go to DCOM Config, find "Windows Management Instrumentation", and give the user you want Remote Launch and Remote Activation. For more information, see Connecting Between Different Operating Systems
User contributions licensed under CC BY-SA 3.0