Can someone tell me what I am doing wrong?
Trying to have a ping script run a bunch of IPs that are in a text file. The write Alive or Dead and write it to a txt file with results.
I get it to run put does not input anything into the txt fil.
End goal is I Would like this to output in green or red in a table but haven’t figured out how to output the results to a txt.
Clear-Host
$ips = Get-Content "C:\Users\username\Desktop\New folder\hnames.txt"
foreach($ip in $ips) {
if(Test-Connection $ip -Count 1 -ErrorAction SilentlyContinue) {
Write-Output "Alive ip - $ip"
} else {
Write-Output "Dead ip - $ip"
Out-File -FilePath "C:\Users\username\Desktop\New folder\results.log"
I have also tried this. This one only write the last IP to the file. I figure im missing something in the loop.
Clear-Host
$ips = Get-Content "C:\Users\username\Desktop\New folder\hnames.txt"
foreach($ip in $ips) {
if(Test-Connection $ip -Count 1 -ErrorAction SilentlyContinue) {
Write-Output "Alive ip - $ip"
} else {
Write-Output "Dead ip - $ip" > Out-File -FilePath "C:\Users\username\Desktop\New folder\results.log"
}
}
>Solution :
Loop is ok, you need redirect just output with >>
Try this:
Clear-Host
$ips = Get-Content "C:\Users\username\Desktop\New folder\hnames.txt"
$outputfile = "C:\Users\username\Desktop\New folder\results.log"
foreach($ip in $ips) {
if(Test-Connection $ip -Count 1 -ErrorAction SilentlyContinue) {
Write-Output "Alive ip - $ip" >> $outputfile
} else {
Write-Output "Dead ip - $ip" >> $outputfile
}
}