1

I would like to execute below command continuously until a key is pressed. As an example:

Test-NetConnection -ComputerName google.com -Port 80

How can I do this?

2 Answers 2

1

You can use a do{...}until() with [console]::KeyAvailable

do
{
    test-Connection -ComputerName google.com -Count 1
    Start-Sleep -Seconds 1
}
until([Console]::KeyAvailable)
Sign up to request clarification or add additional context in comments.

3 Comments

but how to specify the port 42424? I need to specify it and parameter -TcpPort does not work. Using its successor cmdlet, TestNet-Connection I can specify a port using -Port parameter. How could I do this using TestNet-Connection cmdlet instead?
I'm not sure what that has to do with the question you asked about. Perhaps you can find an existing question/answer on that topic, otherwise create a new question about it.
Well, in the post I detailed to perfom a TestNet-Connection cmdlet on a machine and port.
0

I tweaked Doug Maurer's answer and it works for me. It runs the TEST-NETCONNECTION command continuously until you click any key on your keyboard.

do
{
    TEST-NETCONNECTION google.com -port 443
    Start-Sleep -Seconds 1
}
until([Console]::KeyAvailable)

Comments

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.