Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 8583

Out-File only list last entry in file – Why

$
0
0

All,

 

I am trying to get the output file to list all the computers I am running queries against.  The script below works but when run against multiple servers only the last server queried is listed in the out-file.  

What am I doing wrong?

Output I would like to achieve

av3022 is pingable 

av3021 is pingable 

av3023 is pingable 

av3024 is down

 

Currently only one entry (last one queried) is in the file.

 

$servers = Get-Content -Path C:\RCJ\Scripts\POWERSHELL\Servers.txt

ForEach ($server in $servers) {

if

(Test-Connection $server -count 1 -Quiet)

{Clear-Content C:\RCJ\Scripts\POWERSHELL\ServerShutdownResults.txt

write-output ""$server" is pingable "| Out-File -append -FilePath C:\RCJ\Scripts\POWERSHELL\ServerShutdownResults.txt}

else 

{write-output ""$server" is down " | Out-File  -append -FilePath C:\RCJ\Scripts\POWERSHELL\ServerShutdownResults.txt}

}

 

Thanks,

Rob 


Viewing all articles
Browse latest Browse all 8583

Trending Articles