Powershell: Powershell stops outputting strings after running While command

Created on 18 Oct 2019  路  4Comments  路  Source: PowerShell/PowerShell

Support Question

When trying to run this script (This is just an extract of the end), the final output of
"The disk size is $Size GB"
"There is $freespace GB of space free($percentagefree%)"
does not show up. It simply skips to read host and exists the script.
Apologies for my janky code but I'm completely stumped as to why this would happen?

invoke-command -Session $s -scriptblock {
gwmi win32_logicaldisk | FT DeviceId, MediaType, `
@{n="Size";e={[math]::Round($_.Size/1GB,2)}},@{n="FreeSpace";e={[math]::Round($_.FreeSpace/1GB,2)}}
}

$Continue = read-host -prompt "Continue? Y/N"
if (!($Continue = 'y')) {exit}

If(-$installed -eq $false) {
copy-item "\pronto.com.au\IT\IT-Software\Diagnostics\patchcleaner.msi" -destination "\$computername\c$";
"Installer copied"
invoke-command -Session $s -scriptblock {
Start-Process msiexec.exe -ArgumentList "/i C:\PatchCleaner.msi /q /norestart" 鈥揥ait
"Patch Cleaner has been successfully installed"
}
}
else{
"Patch Cleaner is already installed"
}

invoke-command -Session $s -scriptblock {
Start-Process -FilePath "C:\Program Files (x86)\HomeDev\PatchCleaner\PatchCleaner.exe" -argumentlist "/D"
$date = Get-date
"Running..."
While ($event -eq $null)
{
$event = get-eventlog -logname Application -source PatchCleaner -after $date
start-sleep -s 1
"."
start-sleep -s 1
"."
start-sleep -s 1
"."
$event
}

$disk = gwmi win32_logicaldisk
$Size = ($disk.Size/1GB)
$Size = [math]::round($Size,2)
$FreeSpace = ($disk.FreeSpace/1GB)
$Freespace = [math]::round($FreeSpace,2)
$PercentageFree = (100/$Size)*$FreeSpace
$PercentageFree = [math]::round($percentageFree,1)
"The disk size is $Size GB"
"There is $freespace GB of space free($percentagefree%)"
}

read-host -prompt "Press enter to exit"

Official support

PowerShell Support Lifecycle

Community Resources

Slack and Discord Community Chat - Interactive chat with other PowerShell enthusiasts. Both Slack and Discord are bridged via a bot and can seamlessly talk to each other.

StackOverflow.com and PowerShell.org Forum - Search or post new general PowerShell usage questions

Issue-Question Resolution-Answered

Most helpful comment

I believe it's the same as this UserVoice issue for PSv5 and is caused by you sending some output through the object pipeline and some directly to the host, and they take different paths, and end up arriving on screen at different times - the read-host call grabs the screen before the pipeline output arrives there.

All 4 comments

I believe it's the same as this UserVoice issue for PSv5 and is caused by you sending some output through the object pipeline and some directly to the host, and they take different paths, and end up arriving on screen at different times - the read-host call grabs the screen before the pipeline output arrives there.

Yep. Success output has a delayed display to the screen (~300ms if I recall correctly) and will be preempted by things like Read-Host which halt execution until they are responded to.

You can send output directly to the screen instead by using Write-Host or piping into Out-Host. 馃檪

This Stack Overflow answer has a concise summary of the problem (with links to more information).

This issue has been marked as answered and has not had any activity for 1 day. It has been closed for housekeeping purposes.

Was this page helpful?
0 / 5 - 0 ratings