Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Ping script not exporting results

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 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   
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading