New to Powershell 2021


I would like to run a powershell script where I can loop a command until it comes back false.

tnc -ComputerName [Address] -port [port]
Start-Sleep -s 5

The script is run every 5 seconds

ComputerName : [Address]
RemoteAddress : IP
RemotePort : Port
InterfaceAlias : WiFi
SourceAddress : [Address]
TcpTestSucceeded : True

I need this to run every 5 seconds but only show on false then extract to a doc and continue running.

I don’t know if this is possible, but any help

Welcome to the forum.

Please when you post code or console output format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the buutton and paste your code.
Besides that please do not use aliasses in scripts and especially not in forums as it makes your code harder to read.

Thanks in advance.

So you actually need to run this endlessly, right?

Assumed tnc stands for Test-NetConnection you can create a loop and check the output with a condition. A simple version would be something like this:

while ($true) {
    $Result = Test-NetConnection -ComputerName 'whatever' -Port 'dunno'
    If (-not $Result.TcpTestSucceeded){
        $Result | Export-Csv -Path 'log.csv' -NoTypeInformation
    Start-Sleep -Seconds 5

Hi Olaf,

I ended up using:

$log = do # Do this
    # Store the result of TNC in this variable to be tested until(...) is False
    $tnc = Test-NetConnection -ComputerName [Address]
] -Port [port] |
    Select-Object @{
        Name = 'TimeStamp'
        Expression = { [datetime]::Now.ToString('u') }
    }, RemoteAddress, RemotePort, TcpTestSuceeded

    # Return this variable so it's captured in $log Variable to export the log later
    Start-Sleep -Seconds 5
until ($tnc.TcpTestSuceeded) # Until this property is $False 

$log | Export-Csv path/to/logfile.csv -NoTypeInformation

But thank you very much for taking the time to come back to me.Preformatted text

Please format your code as code!!!

Fixed, sorry Olaf :frowning: