The estimated reading time 2 minutes

Hi there,
if you found this article maybe you are struggling slowness of the microsoft cloud (or some other products) and your script fails because it does not find the newly created object.
Yeah that’s a big issue especially in the wonderful cloud, and if you want to automate tasks. So I invested some time to script this issue, which is not really difficult. It may help others during scripting.

Example 1 Wait until eternity

Let’s see my try to get a Sharepoint URL (with ExchangeOnline PowerShell) of newly created teams (via PowerShell). For this scenario you can use a “DO UNTIL” (maybe not the best solution because script never ends…)

$teaminfo = New-Team -DisplayName Testteam -Owner me@demo.it-koehler.com
$teamgroupid = ($teaminfo).GroupId
#setting counter $i to zero
$i = 0
    do{
      #do this cmdlet until it gives a result
      $teamspurl = (Get-UnifiedGroup |Where-Object {$_.ExternalDirectoryObjectId -eq "$teamgroupid"}) | Select-Object SharePointSiteUrl -ExpandProperty SharePointSiteUrl
      if(!$teamspurl){
        #wait 5 seconds for the next try
        Start-Sleep -Seconds 5
        $i++
        Write-host "waiting for exchange online..."
      }
    }
    Until ($teamspurl)
    #count the and calculate the consumed time
    $urltime = [int]$i *5
    Write-Host "Sharepoint Online took $urltime seconds to respond and find the URL."

Of course you can change the sample interval from 5 seconds to every other value. The time period between the tries is extended. Don’t forget to change the value for calculation of the time.

Example 2 wait until timeout ends

It is nearly the same script as example 1 but it has some special lines:
if($j -gt “20”) makes the difference

    $teaminfo = New-Team -DisplayName Testteam -Owner me@demo.it-koehler.com
    $teamgroupid = ($teaminfo).GroupId
    #setting counter $i to zero 
    $j = 0
    do{
    #do this cmdlet until it gives a result
      $teamspurl = (Get-UnifiedGroup |Where-Object {$_.ExternalDirectoryObjectId -eq "$teamgroupid"}) | Select-Object SharePointSiteUrl -ExpandProperty SharePointSiteUrl
      if(!$teamspurl){
        #wait 5 seconds for the next try
        Start-Sleep -Seconds 5
        $j++
        Write-Host "waiting for exchange online..."
        #if waited 5 second 20 times break the script
        if($j -gt "20"){
              break
            } 
      }
    }
    Until ($teamspurl)
    #count the and calculate the consumed time
    $urltime = [int]$i *5
    Write-Host "Sharepoint Online took $urltime seconds to respond and find the URL."

That’s it.
NOTE: these lines of code only work if you use Teams and Exchange Online Module and have already connected to the cloud. But you can use this loop with every other powershell CMDLET you want.

Hope this small piece of code helps to improve your script. If you liked this topic, click on helpful or leave me comment if you have any questions. Stay tuned.

Print Friendly, PDF & Email
  • Was this Helpful ?
  • yes   no