Test-NetConnection "Cannot connect to CIM server. Access denied "

I have a script that includes “Test-NetConnection” to see if a server is responding. For some users I’m getting the error:

PS>TerminatingError(Find-NetRoute): "Cannot connect to CIM server. Access denied "

When this occurs, the result returned from “Test-NetConnection” is of the form:

ComputerName           : ***
RemoteAddress          : xxx.xxx.xxx.xxx
InterfaceAlias         :
SourceAddress          :
PingSucceeded          : True
PingReplyDetails (RTT) : 3 ms

Note that the “InterfaceAlias” and “SourceAddress” are empty. When the “Test-NetConnection” is successful, these are populated:

ComputerName           : ***
RemoteAddress          : xxx.xxx.xxx.xxx
InterfaceAlias         : Ethernet
SourceAddress          : xxx.xxx.xxx.xxx
PingSucceeded          : True
PingReplyDetails (RTT) : 3 ms

Any ideas on this error or how can I suppress it, given that I’m only interested in the “PingSucceeded” value? I’ve tried:

-ErrorAction SilentlyContinue -WarningAction SilentlyContinue

as well as including the call in a “try{} catch{}”. Neither of these work and the error still gets displayed.

Thanks.

Can you provide your code or at least the failing code? Make sure to format with the ‘</>’ button

It’s pretty much as per the extracts above, but specifically:

$up = Test-NetConnection -ComputerName *** -ErrorAction SilentlyContinue -WarningAction SilentlyContinue

If you are only looking for ICMP (ping) response, you might consider using test-connection instead of test-netconnection.

1 Like

Since the error shows Find-NetRoute, it’s probably not being thrown by Test-NetConnection. I’d set the $ErrorActionPreference for the session to Stop and put it in a try/catch.

$ErrorActionPreference = 'Stop'

try{
    $up = Test-NetConnection -ComputerName ***
}
catch{
    # Write-Warning $_.Exception.Message
}

I gave this a try and now I get:

At script.ps1:251 char:8
+     $up = Test-Connection -ComputerName $controllers -Quiet
+           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Test-Connection], ManagementException
    + FullyQualifiedErrorId : TestConnectionException,Microsoft.PowerShell.Commands.TestConnectionCommand

The “controllers” variable is defined as:

[string[]]$controllers = @("xxx")

Works fine when I run it but doesn’t work for the account that’s running the script.

Gave this a try and still get the same error.

Sounds like you have some security control in place that prevents execution on the other account. Can the other account just run the command from a prompt but not a script?

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.