1

I'm trying to get a script to test connectivity of a list of IP addresses and log the output to a file. I would like it to put a time/date stamp in the log when when I get no response. I'd like to execute the script over a 2-3 day period. I've got it to put the time/date stamp on the output of the console but not in the output file. Here's the script.

$names=Get-Content "C:\script\hname.txt"
foreach($name in $names){
    if(Test-Connection -ComputerName $name -Count 1 -ErrorAction SilentlyContinue){
        Write-Host "$name is up" -ForegroundColor Green
        $output+="$name is up,"
    }
    else{
        Write-Host "$name is down" -ForegroundColor Red
        Get-Date 
        $output+="$name is down,"
        " "
    }
}
$output | Out-File -FilePath "C:\script\results.txt"
Start-Sleep -s 3

this is the results on the ps console

PS C:\Windows\system32> C:\Users\admin\Documents\Test Connection-3sec.ps1
192.168.1.254 is up
192.168.1.74 is down

Tuesday, October 10, 2023 12:06:05 PM
 
192.168.1.239 is up
192.168.1.23 is down
Tuesday, October 10, 2023 12:06:07 PM
 
192.168.1.189 is up
192.168.1.78 is up
192.168.1.79 is up
192.168.1.81 is up
192.168.1.89 is down
Tuesday, October 10, 2023 12:06:08 PM
 
192.168.1.82 is up
192.168.1.85 is up
192.168.1.45 is down
Tuesday, October 10, 2023 12:06:10 PM

PS C:\Windows\system32> 

But this is what I get in the results.txt file.

192.168.1.254 is up,
192.168.1.74 is down,
192.168.1.239 is up,
192.168.1.23 is down,
192.168.1.189 is up,
192.168.1.78 is up,
192.168.1.79 is up,
192.168.1.81 is up,
192.168.1.89 is down,
192.168.1.82 is up,
192.168.1.85 is up,
192.168.1.45 is down,

as a side note the format doesn't have the new line feed which I understand I should be able to get if I add $output+="$name is up," +"`n`r"

I've tried putting the get-date at the end of the $output line, to no avail.

$output+="$name is down," Get-Date

How can I do this?

1 Answer 1

1

To match the console output:

$output+="$name is down,`n$(Get-date)`n"

But personally I'd simplify it and keep the date on the same line

$output+="$name is down. $(Get-date)"

And of course you could always add it to $output as its own entry:

$output+="$name is down, "
$output+= Get-date
9
  • all work well, thanks for the info. One more question, how do I make the script run in a loop for 3 days?
    – txjastx
    Commented Oct 10, 2023 at 21:05
  • 1
    @txjastx Don't do that. Long-running tasks can run into trouble like memory leaks, unexpected CPU use, or hanging and silently failing to continue. Instead, create a scheduled task to run periodically. This allows the process to end and return memory or other resources, and will keep waking up and running even if an individual instance has trouble. Commented Oct 10, 2023 at 21:07
  • I'll check into the scheduled task, I'm trying to catch an intermittent problem and figured it may take some time to do so. Thanks again for the info
    – txjastx
    Commented Oct 10, 2023 at 21:36
  • so the schedule task is not working well for me. I created one in the action tab I have start a program,
    – txjastx
    Commented Oct 19, 2023 at 18:10
  • so the schedule task is not working well for me. I created one in the action tab I have start a program, with powershell.exe in the Program/Script field. for the add arguments I have -File "C:\script\TestConn.ps1"
    – txjastx
    Commented Oct 19, 2023 at 18:16

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.