none
Powershell output wrong order RRS feed

  • Question

  • This is a snippet of a larger piece of code.  It is not what I am using in my real script.  This is about the 5th test re-write.  The issue is simply that the second functions output comes out before the first.  In my actual script, the output from the first function is written to the console halfway through the output from the second.

    Clear-Host
    Write-Host ""
    $Script:ServerName = Read-Host "On which server would you like to clean-up the C:\?"
    Write-Host ""

    Write-Host "Querying C:\ on $ServerName BEFORE cleanup" -ForegroundColor Yellow
     
      function test{
      $DiskInfo = ""  
      do{
       $Disk = Get-WmiObject win32_logicaldisk -ComputerName $ServerName -Filter "DeviceID='C:'"
       $DiskInfo=[ordered]@{
       "Device ID" = $Disk.DeviceId
       "Drive Size" = $Disk.Size
       "Free Space" = $Disk.FreeSpace
       "Percentage Free" = ($Disk.FreeSize) / ($Disk.Size)
       }
       new-object psobject -Property $DiskInfo
       Write-Output $DiskInfo
       }
       while ($DiskInfo -eq "")

       Write-Output $DiskInfo `n
       }

       function test2{
       Write-Host "END"
       }
       test
       test2

    THE OUTPUT IS:

    On which server would you like to clean-up the C:\?: Server

    Querying C:\ on Server BEFORE cleanup

    END
    Device ID  Drive Size  Free Space Percentage Free
    ---------  ----------  ---------- ---------------
    C:        80528543744 40505835520               0

    Monday, February 22, 2016 8:53 PM

Answers

  • Adding | ft to the output fixed the issue.  I didn't try to do that before.  It was already formatted as a table without having to specify.

    Write-Output $Disk | ft

    • Marked as answer by TechGuy65 Thursday, February 25, 2016 1:30 PM
    Wednesday, February 24, 2016 7:09 PM

All replies

  • Write-host and write-output will never match.  Write-Output send output to the pipeline.  Write-Host writes directly to the console.  It will always win.

    \_(ツ)_/

    • Proposed as answer by bferg317 Tuesday, February 19, 2019 7:51 PM
    Monday, February 22, 2016 8:57 PM
  • OK, thank you for that. 

    However, I took the write-host out and changed the script (just as a POC).  The 10 second sleep that is in the "Test" function actually runs before the output of the "Query-DiskBefore" function is written to the console.

    Code:

    Clear-Host
    Write-Host ""
    $Script:ServerName = Read-Host "On which server would you like to clean-up the C:\?"
    Write-Host ""

    function Query-DiskBefore{
        Write-Host "Querying C:\ on $ServerName BEFORE cleanup" -ForegroundColor Yellow
        
        $Disk = Get-WmiObject win32_logicaldisk -ComputerName $ServerName -Filter "DeviceID='C:'" |
        Select-Object DeviceId, `
                      @{l="Drive Size (GB)";e={"{0:N2}" -f ($_.size/1GB)}}, `
                      @{l="Free Space (GB)"; e={"{0:N2}" -f ($_.freespace/1GB)}}, `
                      @{l="Percentage Free(%)"; e={"{0:P2}" -f ($_.FreeSpace / $_.Size)}}

        Write-Output $Disk `n
    }    
        
    function Test{
    Start-Sleep 10
    Write-Output "END"
    }

    Query-DiskBefore
    Test 

    Output:

    On which server would you like to clean-up the C:\?: smhprt01

    Querying C:\ on MyServer BEFORE cleanup

    DeviceId Drive Size (GB) Free Space (GB) Percentage Free(%)
    -------- --------------- --------------- ------------------
    C:       39.90           7.30            18.30 %           


    END



    PS C:\>

    Wednesday, February 24, 2016 2:55 PM
  • Adding | ft to the output fixed the issue.  I didn't try to do that before.  It was already formatted as a table without having to specify.

    Write-Output $Disk | ft

    • Marked as answer by TechGuy65 Thursday, February 25, 2016 1:30 PM
    Wednesday, February 24, 2016 7:09 PM