Do until loop

Welcome Forums General PowerShell Q&A Do until loop

Viewing 2 reply threads
  • Author
    Posts
    • #185384
      Participant
      Topics: 1
      Replies: 2
      Points: 18
      Rank: Member

      Hi,

      I'm working on a script for new starters which includes syncing the user to O365 via AD. As part of that, and because of the time the sync takes I've put a Start-Sleep -Seconds 60 command into the script to give the object enough time to sync.

      I've been reading about Do/while and Do/until loops but don't have much experience in using them. Is there a way I can incorporate one of these loops so the script waits until the object is visible in Azure AD/O365 and then continues the script?

      I also have the same issue when it comes to licence assignment and mailbox creation. I've had to put another sleep in of 60 seconds in order for the mailbox to show so I can perform further actions on it.

      Any help/guidance is appreciated.

      Thanks

    • #185447
      Participant
      Topics: 9
      Replies: 1277
      Points: 1,051
      Helping Hand
      Rank: Community Hero

      There are a lot of examples of loops out there. Assuming ADSync is set to 15 minutes, you may want to wait 30 minutes for AD replication. This loop checks if the user exists or if a timeout is met:

      'Build user locally'
      
      $t=1
      $timeout = 30
      
      do{
          'Waiting for AdSync {0}' -f $t
          #$user = Get-MsolUser ....
          #Start notepad to test break
          $user = Get-Process -Name notepad -ErrorAction SilentlyContinue
          Start-Sleep -Seconds 60
      
          $t++
      
      }
      until($user -or $t -eq $timeout)
      
      'Run cloud commands'
      
      • #185681
        Participant
        Topics: 1
        Replies: 2
        Points: 18
        Rank: Member

        Hi Rob,

        Thanks for the response. Sorry, I'm still learning and getting started with PowerShell so not sure I follow why you include Notepad?

        Thanks,

        James

    • #185552
      Participant
      Topics: 0
      Replies: 115
      Points: 433
      Helping Hand
      Rank: Contributor

      James,

      Any specific reason you are not using AzureAD (AAD) Connect to sync the mailbox from AD to Azure AD? In regards to your issue of having to wait a period of time, make sure to use the same Domain Controller. Each time you run the query it will pick an available Domain Controller and no guarantee it will be the same one. By going ahead and specify the domain controller you alleviate this and can continue with the assignment of licenses. I also recommend you to look at group based licensing instead of individual licensing. If you want to share the code we can help out a bit further. 🙂

      • #185684
        Participant
        Topics: 1
        Replies: 2
        Points: 18
        Rank: Member

        Hi Jason,

        Thanks for coming back to me. We do use AzureAD Connect. The problem is that I initiate a sync in my script but I have to wait 60 seconds or so for the sync to complete and for the object to appear in Azure AD. Again, I have the same thing when I assign a licence – I have to wait around 60 seconds until a mailbox is created after licence assignment.

        I was just wondering if there was anyway, instead of me using start-sleep -seconds 60, that I could just do a lookup of sorts that checks for the object in Azure AD and the script continues. Likewise, with the licence assignment, where it checks for the mailbox being live and then continues.

        Hope that makes sense.

        James

Viewing 2 reply threads
  • You must be logged in to reply to this topic.