This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Forums Archives Forums Archives 5 years, 4 months ago.

  • Author
  • #5298

    by energy99 at 2012-11-21 13:25:02

    I would like to cleanup the final report by having a blank line between each servers results. I have been using the following code:

    get-content “c:\servers.txt” | %{$_ >> c:\Summary.txt; get-content "\\$_\c$\results.txt" >>c:\Summary.txt}

    The above works great but when I view the Summary.txt file it lists all the necessary data but I would lke to change the formatting to have a blank line between the results from each of the servers listed from the E:\servers.txt file. For example:

    Right now:


    I would like it to read:




    by DonJ at 2012-11-21 13:29:19

    Write a "`n" (backtick-N in double quotes) for a blank line.

    by energy99 at 2012-11-21 14:14:04

    Any idea where in the line of code provided I would place the "`n" ? I've tried in numerous spots but still does not create a blank line between the data.

    by DonJ at 2012-11-21 14:26:29

    Yeah, so the underlying problem is that you're asking PowerShell to do something it doesn't terribly like, which is deal with plain text. It likes objects. It isn't really meant to produce textual reports like this, so you have to fiddle with it a lot sometimes.

    You're also using a lot of shortcuts, so just in the interest of making this more readable to someone else who might come along and read this in the future...

    $servers = Get-Content c:\servers.txt
    foreach ($server in $servers) {
    $server | out-file results.txt -append
    get-content "\\$_\c$\results.txt" | out-file results.txt -append
    "`n`n" | out-file results.txt -append

    Is what I might try if I had PowerShell right in front of me right now – which sadly, I don't, so you'll have to let me know how it works out. I added a couple of blank lines there just to kind of exaggerate the effect. You can probably re-construct that into a one-liner if that's what you're after. Basically just another semicolon in your ForEach-Object script block.

    by energy99 at 2012-11-22 07:45:37

    not having much luck with the above... what it does is open the servers.txt file in notepad and show me the contents of servers.txt.

    by DexterPOSH at 2012-11-22 14:13:20

    Hi Energy99,

    I tried this using a simple approach. Let's say there is a file Servers.txt with three server names in it and there is a result file on these servers with some result.
    So what I did is prefixed the $server with a "`n" character, the only downside with this is that the first server will also have a space:

    $servers = Get-Content .\servers.txt
    foreach ($server in $servers) {
    "`n$server" | out-file -Append .\summary.txt
    Get-Content "\\$Server\C$\results.txt" | Out-File .\summary.txt -Append

    So this gives me output something like this
    PS C:\TEMP> gc .\summary.txt

    Result 1

    Result 2

    Result 3

    Hope this helps

    by energy99 at 2012-11-23 07:51:06

    Perfect, thank you!

    by nohandle at 2012-11-23 12:43:08

    If you specify the read count paremeter on the get-content cmdlet it serves two lines at a time (as array of strings). so the only thing you has to do is to add blank (not `n) after each pair. And capture it to the output of course.
    Get-Content -Path "C:\temp\txt.txt" -ReadCount 2 | ForEach-Object{$_;""} | Out-File -Append -FilePath 'C]

    Still one extra line on the end, though.

    by nohandle at 2012-11-23 13:34:30

    And if anybody wonders how to do it without adding blank line after the last line. And IMHO more readable
    function AddBlank ([string]$Path)
    $lines = Get-Content -Path $Path
    for ($index=0; $index -lt $lines.Length; ++$index)
    #if index is odd and the line is not the last add blank
    #-1 because indexing from zero
    if (($index%2) -and ($index -ne ($lines.Length-1)))
    Write-Output ""
    AddBlank -path 'C:\temp\txt.txt' |out-file -Append -FilePath 'C]

    I know the name is not according the powershell verb-noun best practice.

You must be logged in to reply to this topic.