I would like to Stop the execution of when timeout string is found in a variable

Hi, quick question:

I need to use the traceroute function ‘tracert’ through Powershell but want to stop its execution when it gets ‘Request timed out’, in order to not wait for the 30 hops maximum for the command to finish.

In general, in Powershell, does a variable gets written ONLY at the end of a function execution or written as outputs comes to it ?

Here is what I tried:

Do {

tracert xxx.xxx.xxx.xxx | Tee-Object -Variable TracertResponse

} Until (($TracertResponse -like '*Request timed out*') -ne $null)

# Also, as a test, if $TracertResponse would already contain a traceroute output with some timeout in it, this would work:

if (($TracertResponse -like '*Request*') -ne $null) {Write-Host "We have a Timeout"}

Thanks

I do not have an answer, but am looking at the PowerShell Test-NetConnection command as this is shown to be equivalent to tracert

Yes, Test-NetConnection with -Tracert switch, it has a -Hops parameter to control number of hops as well. But it does not give you “request time out”, but 0.0.0.0 result.

But for your req, I don’t think it’s possible as the until condition hits only post the command/cmdlet execution(completion), by the time it would have already got the requset time out response till the last hop.

Simply put, that is not how TraceRT works, neither does the cmdlet for it in PowerShell there others are referring to. TraceRT is considered one of those blocking command, that requires manual user interaction to break out of it.