How can I return a simple "Access Denied" message during error catching instead of the whole error stack?


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.)


$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 = "" #placeholder value 
$newDNS2 = "" #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

        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
            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
            Write-Host "Secondary DNS correct" -ForegroundColor Green
}#END ForEach                   
asked on Stack Overflow Apr 9, 2018 by Shawn • edited Sep 25, 2020 by TylerH

1 Answer


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

answered on Stack Overflow Apr 10, 2018 by wp78de

User contributions licensed under CC BY-SA 3.0