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?
1 Answer 1
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()
}
}
*>$nulldidn't work it means they're likely usingConsole.WriteLinemeaning that you won't find a way around that... Great design by Microsoft!! As usual.Start-JobandReceive-Job -Wait -AutoRemoveas yourwhilecondition. 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.Write-Host "$($esc)[1F" -NoNewlinebefore theWrite-Verbose. 2. Move the waiting to the background with aStart-JoborStart-ThreadJob, which would also allow you to define a timeout and kill the job.