-1

here's my Code, the output should show up every 5 seconds but now i can't see anything when the script is running, after code in while{},the output will show up at the same time, you can see the picture attached

but when i change this $ShowStatus|ft -AutoSize to write-host $showstatus i can see the output in this format every 5 seconds @{ResourceGroup=mxytest; VMName=AD-BJ; OS=Windows; static=Running; Time=2017/5/31 17:33:27}

if ($VMJobIDs -ne $null)
{
    $NotCompleted = $true
    while ($NotCompleted)
    {
        $NotCompleted = $false
        [pscustomobject[]]$ShowStatus = $null
        foreach ($VMJobID in $VMJobIDs)
        {
            #Write-Host "$(Get-Date)" -ForegroundColor Yellow
            $VMJob = Get-Job -Id $VMJobID
            if ($VMJob.State -eq "Completed")
            {
                if ($VMJob.HasMoreData)
                {
                    $FinalResult = $null
                    $FinalResult = New-Object -TypeName psobject
                    $Result = Receive-Job -Id $VMJob.id
                    $FinalResult | Add-Member -MemberType NoteProperty -Name ResourceGroup -Value $ResourceGroupName
                    $FinalResult | Add-Member -MemberType NoteProperty -Name VMName -Value $($VMJob.Name)
                    $FinalResult | Add-Member -MemberType NoteProperty -Name OS -Value $VMOSInfo.($VMJob.Name)
                    $FinalResult | Add-Member -MemberType NoteProperty -Name Succeed -Value $($Result.IsSuccessStatusCode)
                    $FinalResult | Add-Member -MemberType NoteProperty -Name Time -Value $(Get-Date)
                    $FinalResults += $FinalResult
                }
            }
            else
            {
                $NotCompleted = $true
            }
            $Show = $null
            $Show = New-Object -TypeName psobject
            $Show | Add-Member -MemberType NoteProperty -Name ResourceGroup -Value $ResourceGroupName
            $Show | Add-Member -MemberType NoteProperty -Name VMName -Value $($VMJob.Name)
            $Show | Add-Member -MemberType NoteProperty -Name OS -Value $($VM.StorageProfile.OsDisk.OsType.Tostring())
            $Show | Add-Member -MemberType NoteProperty -Name static -Value $($VMJob.State)
            $Show | Add-Member -MemberType NoteProperty -Name Time -Value $(Get-Date)
            $ShowStatus += $Show
        }
        #Write-Host ("$(Get-Date) * Trying to install the extension...Please wait")  -ForegroundColor Yellow
        $ShowStatus|ft -AutoSize
        #Write-Host ("$(Get-Date) * Trying to install the extension...Please wait")  -ForegroundColor Yellow
        Start-Sleep 5

    }
}
if ($FinalResults -ne $null)
{
    return $FinalResults
}

enter image description here

mxy
  • 37
  • 1
  • 8

1 Answers1

0

Change these lines:

    $ShowStatus += $Show
}

$ShowStatus|ft -AutoSize
Start-Sleep 5

To this:

    $ShowStatus += $Show
    $ShowStatus|ft -AutoSize
    Start-Sleep 5
}

Edit - above did not work for OP

From the comments, it sounds like Write-Host is giving you the behaviour you want. So keep using it:

Write-Host $ShowStatus.ResourceGroup $ShowStatus.VMName $ShowStatus.OS $ShowStatus.static $ShowStatus.Time

This wouldn't necessarily give you a nice table but it looks like your data is the same length for each field, for each line.

You can use tabs like so:

Write-Host "$($ShowStatus.ResourceGroup)`t$($ShowStatus.ResourceGroup)`t etc"

You can display aligned text line so, but assume this will give you the same issue as the previous attempts to get the output every 5s:

# "-10" means 10 characters for this field, left aligned. "10" = right aligned
"{0,-10} {1,-10} ..." -f $ShowStatus.ResourceGroup $ShowStatus.VMName ...
G42
  • 9,791
  • 2
  • 19
  • 34
  • sorry,but it's not useful..i don't why i can't see the output – mxy May 31 '17 at 09:25
  • I thought it was due to the output being outside of the `foreach` loop. If this is not the case, this may be expected behaviour when dealing with jobs (I'm not familiar with them). – G42 May 31 '17 at 09:28
  • it should not be some logic issue, outside the foreach loop, there is still a while loop, i just found if i change $ShowStatus|ft -AutoSize to write-host $showstatus i can see the output in this format every 5 seconds @{ResourceGroup=mxytest; VMName=AD-BJ; OS=Windows; static=Running; Time=2017/5/31 17:33:27}, really don't know why – mxy May 31 '17 at 09:37
  • Switch from `Write-Host` to `Write-Output` – G42 May 31 '17 at 09:41
  • just tried..write-output is same as $ShowStatus|ft -AutoSize – mxy May 31 '17 at 09:53
  • Thanks a lot!. it did work, but is there any better solution for this? do you know what is the root cause? – mxy Jun 01 '17 at 02:33
  • @mxy Great! As this has answered the question, please consider accepting the answer, and up-voting if you think it useful. I don't know a better solution. Regarding root cause, it's the difference between `Write-Out` and `Write-Host` (`Write-Output` is used by default when you don't specify anything like in your original code). See [this StackOverflow q/a](https://stackoverflow.com/questions/8755497/which-should-i-use-write-host-write-output-or-consolewriteline) and [this one](https://stackoverflow.com/questions/19754069/powershell-difference-between-write-host-and-write-output). – G42 Jun 01 '17 at 09:50