jeudi 21 septembre 2017

foreach loop doesn't execute the variable

I wrote a few lines of code to check if a port is open or not:

$ports = @(5353,5672,8080,4443,15672,9200)

foreach ($port in $ports)
    {   
    TNC -ComputerName localhost -Port $port -InformationLevel Quiet -WarningAction SilentlyContinue | Tee-Object -Variable CheckPortStatus > $null  
        if ($CheckPortStatus -eq "True") 
            {$status = Write-Host "Open" -ForegroundColor GREEN}
        else
            {$status = Write-Host "close" -ForegroundColor RED} 
    echo "the port is $status"

I don't get why the output is this:

enter image description here

The script actually works but it execute the variable when declared and then it doesn't use the variable after the if/else

Aucun commentaire:

Enregistrer un commentaire