Here is a sample script (sanitized) that i'm running - not important to this question, but what it does is sync local AD users with MS Online. It runs a code block on a remote server "UTILITY01" where the necessary dependencies exist. Everything works properly except for the output of the remotely executed code is delayed till the end of the script (or after the script exits??)
How can I make the output appear, and then wait? For reference, this is called from a windows shortcut. The output appears immediately before the window closes. The user has about 2ms to read it before it all disappears.
Thanks for any insight. :o\
$cred = Get-Credential -UserName "domain\myname" -Message "Enter Credentials:"
$s = New-PSSession -computerName UTILITY01 -Credential $cred
$Script_Block = {
Start-ADSyncSyncCycle -PolicyType Delta
Write-Host "finished"
# Output from this script block is delayed until the end of the whole script.
# ... or maybe until after the script exits?
}
# This is where that script block actually runs in that remote PS session.
Invoke-Command -Session $s -Scriptblock $Script_Block
# Session is closed
Remove-PSSession $s
# Delay to keep window open to read the output above (but there is no output)
Write-Host "Will exit in 10 sec..."
Start-Sleep 10
# How can I make the output from the remote script-block write to the terminal
# prior to the sleep, prior to the end of this script?