Home > Software design >  Ping script not exporting results
Ping script not exporting results

Time:07-01

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"
     

}

}

CodePudding user response:

  • In your first, attempt, your Out-File call receives no input, so the resulting file is empty.

    • To put it differently: the Write-Output calls aren't connected to your Out-File call, so their output prints to the host (display) by default, and Out-File creates an empty file.

    • To connect a cmdlet's output stream to the input stream of another, use |, the pipeline operator

  • In your second attempt (... > Out-File ...) you're mistakenly using both > and Out-File; instead, use one or the other - such as by simply replacing > with | in your command. However, since you're trying to append to the file, use either >> or Out-File -Append

    • In fact, > / >>, the redirection operators, are effectively aliases for Out-File / Out-File -Append; use Out-File if you need more control over how the file is saved, notably with respect to character encoding, via the -Encoding parameter.

The best solution in your case is to use a single pipeline that uses a single Out-File call at the end:

Get-Content "C:\Users\username\Desktop\New folder\hnames.txt" |
  ForEach-Object {
    if(Test-Connection $_ -Count 1 -ErrorAction SilentlyContinue) {
        Write-Output "Alive ip - $_"
    } else {
        Write-Output "Dead ip - $_" 
    }
  } |     
  Out-File -FilePath "C:\Users\username\Desktop\New folder\results.log"

Note the use of the automatic $_ variable that refers to the current pipeline input object.

This has two advantages over using >> inside the ForEach-Object script block, as shown in Przetczak's answer:

  • Saving is much more efficient, because you needn't open and close the output file for every use of >>, which happens for each IP.

  • You needn't worry about first making sure that the output file doesn't exist or is empty, so as not to accidentally append the new data to old data.

CodePudding user response:

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   
    }
}
  • Related