Out-File only list last entry in file - Why

Welcome Forums General PowerShell Q&A Out-File only list last entry in file - Why

This topic contains 6 replies, has 4 voices, and was last updated by

 
Participant
4 years, 7 months ago.

  • Author
    Posts
  • #14313

    Participant
    Points: 0
    Rank: Member

    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

  • #14315

    Participant
    Points: 0
    Rank: Member

    John,

    Thanks for the help. It works the way I excepted now.

    Rob

  • #14338

    Participant
    Points: 0
    Rank: Member

    Richard

    Richard,

    Thanks for the reply. With regards to best practice,which method is preferred? What is the best practice?

    Thank you,
    Rob

  • #14342

    Participant
    Points: 0
    Rank: Member

    It really depends on what you want to do. I tend to mix and match output methods depending on what I'm trying to achieve. If the file is one I'll keep coming back to I tend to use the *Content cmdlets. if its a one off dump of data then out-file is.

    If you don't use a format cmdlet you end up with the default format for the object as defined to PowerShell in the format files. This is just dumped to out-file, effectively as string data. If you want output that's re-usable then you need to use the content cmldtes (good for strings), export-csv (can be read in as objects), export-clixml (preserves object as XML)

  • #14314

    Participant
    Points: 21
    Rank: Member

    So when your script runs, if the server is available it clears the content of the text file and then writes to it. When the script reaches the non-pingable server it doesn't clear the file first, I am kinda surprised that you aren't getting the last pingable and non-pingable server in the list. I think that this is more along the lines of what you are looking for

    `
    $servers = get-content -path 'c:\rcj\scripts\powershell\servers.txt'
    $outfile = 'c:\rcj\scrpts\powershell\servershutdownresults.txt'
    foreach($server in $servers)
    {
    if(test-connection $server -count 1 -quiet)
    {
    write-output "$server is pingable" | out-file -append -filepath $outfile
    }
    else
    {
    write-output "$server is down" | out-file -append -filepath $outfile
    }
    }

  • #14326

    Participant
    Points: 0
    Rank: Member

    Check out Add-Content – you could simplify

    
    $servers = get-content -path 'c:\rcj\scripts\powershell\servers.txt'
    $outfile = 'c:\rcj\scrpts\powershell\servershutdownresults.txt'
    foreach($server in $servers)
     {
       if(test-connection $server -count 1 -quiet)
       {
          $message = “$server is pingable” 
       }
       else
       {
        $message = “$server is down” 
       }
       Add-Content -Value $message -Path $outfile
     }
    
  • #14340

    Member
    Points: 0
    Rank: Member

    If you're working with strings, you won't see any difference between Out-File -Append and Add-Content. The main difference is that Out-File works with PowerShell's Format-* cmdlets, even if you don't call one explicitly, so it produces the type of table or list output you normally see at the console.

    If you send a non-string object to Add-Content, the output is usually different (and often rather useless.)

The topic ‘Out-File only list last entry in file - Why’ is closed to new replies.