I am using the Teams PowerShell Module 7.3.1 on PowerShell Core 7.5.3 to pause a provisioning script until a newly created user becomes available in the Microsoft Teams PowerShell module, as follows:
while (-not (Get-CsOnlineUser -Identity <userid> -ErrorAction SilentlyContinue)) {
    Write-Verbose "Waiting for new user to be synced into Microsoft Teams..." -Verbose
    Start-Sleep 10
}
This normally works pretty well for other PowerShell modules like Microsoft Graph and Exchange Online, but when I use it with the Microsoft Teams module, it returns the following:
Correlation id for this request : <GUID> VERBOSE: Waiting for new Guest User to be synced into Microsoft Teams... Correlation id for this request : <GUID> VERBOSE: Waiting for new Guest User to be synced into Microsoft Teams...
How can I suppress the Correlation id for this request : <GUID> output? It seems to appear whenever I make a Get-CsOnlineUser request that returns an error.
When running the command individually, I have already tried:
Get-CsOnlineUser -Identity "123456" -ErrorAction SilentlyContinue | Out-Null
$null = Get-CsOnlineUser -Identity "123456" -ErrorAction SilentlyContinue
Get-CsOnlineUser -Identity "123456" -ErrorAction SilentlyContinue *>$null
[void](Get-CsOnlineUser -Identity "123456" -ErrorAction SilentlyContinue)
function global:Write-Host() {}; Get-CsOnlineUser -Identity "123456" -ErrorAction SilentlyContinue
function global:Write-Information() {}; Get-CsOnlineUser -Identity "123456" -ErrorAction SilentlyContinue
Any thoughts as to how I can suppress this output?
If *>$null didn't work for you, it most likely means the cmdlet is using Console.WriteLine, the output goes straight to your host and it cannot be captured or redirected.
Your easiest option is to invoke the cmdlet outside the current process (host), for that, the easiest way is via Start-Job; not very efficient as you'd be creating a new process per loop iteration, though given your 10 second sleep time it might not matter:
$command = { Get-CsOnlineUser -Identity 'john.galt' -ErrorAction SilentlyContinue }
while (-not (Start-Job $command | Receive-Job -Wait -AutoRemoveJob)) {
    Write-Verbose 'Waiting for new user to be synced into Microsoft Teams...' -Verbose
    Start-Sleep 10
}
Another way is to use an out of process runspace, this method is less known and needs a bit more work but is much more efficient since you're creating a single process and re-invoking the command using it as a proxy:
try {
    $command = { Get-CsOnlineUser -Identity 'john.galt' -ErrorAction SilentlyContinue }
    $ps = [powershell]::Create().AddScript($command)
    $ps.Runspace = [runspacefactory]::CreateOutOfProcessRunspace($null)
    $ps.Runspace.Open()
    while (-not $ps.Invoke()) {
        Write-Verbose 'Waiting for new user to be synced into Microsoft Teams...' -Verbose
        Start-Sleep 10
    }
}
finally {
    if ($ps.Runspace) {
        $ps.Runspace.Dispose()
        $ps.Dispose()
    }
}
                        According to the OP's comments, the issue is about the optics (I can relate to that).
In this case, ANSI sequences can be used to overwrite the direct console output of Get-CsOnlineUser.
What's required here is:
ESC [ <n> F (from Cursor Positioning) to move the cursor up one line (so <n> will be 1), andESC [ <n> K (from Text Modification) to erase the previous line (so <n> will be 2 to erase the complete line).The sequences could theoretically be embedded directly into the 'desired' output string, but not when using Write-Verbose, because that will also add VERBOSE: , which would be printed before the ANSI sequence, and be left standing by itself under the 'desired' output; so a separate call to Write-Host it is.
while (-not (Get-CsOnlineUser -Identity <userid> -ErrorAction SilentlyContinue)) {
    Write-Host "`e[1F`e[2K" -NoNewline
    Write-Verbose "Waiting for new user to be synced into Microsoft Teams..." -Verbose
    Start-Sleep 10
}
More on the ANSI terminal support in PowerShell 7
See also @mklement0's answer here: Colored text output in PowerShell console using ANSI / VT100 codes
ANSI terminal sequences are also basically supported and enabled by default in PowerShell 5.1 running on a current version of Windows 10 and later.
Some caveats:
`e is not available in 5.1; something like $esc = [char]27 will have to do."$([char]27)[0m" handy to reset the current sequence.More on the ANSI terminal support in Windows PowerShell 5
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With