4

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?

asked Oct 22 at 14:23
3
  • 2
    If *>$null didn't work it means they're likely using Console.WriteLine meaning that you won't find a way around that... Great design by Microsoft!! As usual. Commented Oct 22 at 14:30
  • 1
    The easiest workaround here would be to use Start-Job and Receive-Job -Wait -AutoRemove as your while condition. That way the console output isn't redirected and you'd only get the standard output from the cmdlet.... It's awful any way you look at it but works. Commented Oct 22 at 15:13
  • Two ideas come to mind, not sure if either of them is viable for you, so as a comment. 1. If it's just for the optics, you could use ANSI sequences to position the cursor and overwrite the Correlation ID output with your output, so something like Write-Host "$($esc)[1F" -NoNewline before the Write-Verbose. 2. Move the waiting to the background with a Start-Job or Start-ThreadJob, which would also allow you to define a timeout and kill the job. Commented Oct 22 at 15:14

1 Answer 1

3

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()
 }
}
answered Oct 22 at 15:33
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.