3

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!

1 Answer 1

4

You can use the parameter ErrorAction like this:

If(Get-NetTCPConnection -LocalPort 5900 -State Established -ErrorAction SilentlyContinue) {Write-host "Established!"}

Sign up to request clarification or add additional context in comments.

2 Comments

That actually worked perfectly, the script runs as it should and no errors on receive-job, thank you!
I think that actually exist a best approach, but this is a simple way to solve the output

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.