1

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? 
mklement0
  • 382,024
  • 64
  • 607
  • 775
Nate
  • 226
  • 2
  • 6
  • Does `Start-ADSyncSyncCycle` output anything? If not, then no. Things can't happen _before_ they... well, happen :) – Mathias R. Jessen Nov 16 '21 at 22:19
  • 2
    If you're content with _display_ output only, try appending `| Out-Host` to the `Invoke-Command` call - see [this answer](https://stackoverflow.com/a/43691123/45375) for details. – mklement0 Nov 16 '21 at 22:25
  • Yes, both `Start-ADSyncSyncCycle` and `Write-Host "finished"` produce output, which I had expected would be output BEFORE the `Write-Host "Will exit in 10 sec..."` and end of script, but is happening after instead. Even with a much greater `Start-Sleep` delay that output is blocked apparently until THIS script ends. – Nate Nov 17 '21 at 13:15
  • 1
    Yes @mklement0 Yes that was it. Thanks so much. It's only most of a year later before I need this thing again. :o) Although, I have no idea how I didn't see your comment when I was leaving my other comment just above this. – Nate Aug 23 '22 at 19:37
  • Glad to hear it helped, @Nate. To guide future readers, I've closed your question as a duplicate of the post I linked to in my previous comment. – mklement0 Aug 23 '22 at 20:17

0 Answers0