cameracode
cameracode

Reputation: 97

Not showing Table of Objects until after Pause

Have tried searching the interwebs, but majority of results only show when people accidentally hit a key when the PowerShell Console is active.

The issue I'm facing is that I store a Hyper-V object in a variable and call that variable.

The following snippet is from my script.

$ListDisks = Get-VMHardDiskDrive -VMName $VMName -ControllerType SCSI

"The following table will show which disks have been attached to the VM!" | Write-Host

$ListDisks
Pause

So when the script executes it runs and shows all the output like it's supposed to. Issue occurs when I hit the Pause portion of the script. The Get-VMHardDiskDrive output doesn't appear until after I hit "Press Enter to continue..."

Output:

The following table will show which disks have been attached to the VM!

Press Enter to continue...: 
VMName  ControllerType ControllerNumber ControllerLocation DiskNumber Path                                
------  -------------- ---------------- ------------------ ---------- ----                                
SOME-VM SCSI           0                0                  15         Disk 15 1.00 GB Bus 0 Lun 0 Target 0
SOME-VM SCSI           1                0                  28         Disk 28 1.00 GB Bus 0 Lun 0 Target 0
SOME-VM SCSI           2                0                  30         Disk 30 1.00 GB Bus 0 Lun 0 Target 0
SOME-VM SCSI           3                0                  16         Disk 16 1.00 GB Bus 0 Lun 1 Target 0

It's not a super big issue, I'm just wondering why this behavior only occurs with Pause. If I remove the Pause the issue doesn't occur. Why is this occurring?

**Edit - Forgot to add I was utilizing the PowerShell ISE in Windows Server 2012 R2 with Hyper-V Cmdlets. I was running the script with F5 and manually invoking the script in the ISE Console. Issue does not occur when utilizing a regular PowerShell Console.

Upvotes: 2

Views: 1908

Answers (2)

howdoicode
howdoicode

Reputation: 993

I had the same issue, but using Get-DnsServerResourceRecord. Instead of using Write-Host, just pipe it to Format-Table -AutoSize. This would allow Format-Table to collect the data and properly format it, allowing the script to handle the stream of data in a controlled manner.

I found the solution here: PowerShell Write-Host not synchronous

Upvotes: 0

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200463

This seems to be an issue with the ISE. I can reproduce the behavior you describe if I run your code in ISE, whereas it works fine when run in a regular PowerShell. ISE and regular PowerShell are known for behaving slightly different from each other since they're different host environments.

You can work around the issue by enforcing host output, e.g. like this:

$ListDisks | Out-Host
pause

However, normally you should avoid writing to the host unless it's for presenting data to the user, because host output by design and definition doesn't go to any of PowerShell's output streams and thus can't be further processed.

If your script is intended for being run in a regular PowerShell console, I wouldn't bother working around this ISE quirk.

Upvotes: 4

Related Questions