Reputation: 117
I have an if statement that I am trying to figure out, basically it is
If(Get-NetTCPConnection -LocalPort 5900 -State Established) {}
The end goal is that if a connection exists on that port with the established state, do something. The issue I am running into is figuring out how to check to see if any connections with those parameters exist at all, since if none exist I get the "No Matching MSFT_NetTCPConnection objects found by CIM" error, it does not affect the execution of the script but it's an ugly error that I am trying to lint.
I have tried:
If($Null -ne (Get-NetTCPConnection -LocalPort 5900 -State Established)){}
If(-Not [String]IsNullorEmpty::((Get-NetTCPConnection -LocalPort 5900 -State Established))){}
Any help would be appreciated!
Upvotes: 3
Views: 2781
Reputation: 780
You can use the parameter ErrorAction like this:
If(Get-NetTCPConnection -LocalPort 5900 -State Established -ErrorAction SilentlyContinue) {Write-host "Established!"}
Upvotes: 4