One of the biggest challenges I faced while automating mailbox creation and migration within a hybrid Exchange/O365 environment was figuring out when Dirsync had finished running.
By default, Dirsync runs x hours after the time the last cycle completes, which means that Dirsync never really runs on a fixed schedule. But, how does Dirsync know when the cycle completed?
Turns out, there’s a value in Get-MsolCompanyInformation called LastDirsyncTime. Get that value, store it in a variable, run Dirsync, then use a while loop to compare the value in your variable with the current value of LastDirSyncTime until the current value exceeds your variable value.
Here’s the code snippet, and the full function I use for O365 to On-Prem migration is below the cut.
$DirSyncTimeBefore = Get-MsolCompanyInformation | select LastDirSyncTime
$dstimeb = $DirSyncTimeBefore.LastDirSyncTime
Write-Output "Time before is $dstimeb"
$ScriptBlock = {
Invoke-Command -ComputerName dirsyncserver.domain.com {C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe `
-psconsolefile "C:\Program Files\Windows Azure Active Directory Sync\DirSyncConfigShell.psc1" `
-command "Start-OnlineCoexistenceSync"}
}
while ($timetest  -ne $True){
  $DirsyncTimeNow = Get-MsolCompanyInformation | select LastDirSyncTime
  $dstimen = $DirsyncTimeNow.LastDirSyncTime
  $timetest = $DirsyncTimeNow.LastDirSyncTime –gt $DirSyncTimeBefore.LastDirSyncTime
  Write-Output "Time now is $dstimen"
  Start-Sleep(20)
  Function Invoke-MailboxMigration
  {
  <#
  .SYNOPSIS
  Migrates a mailbox from O365 to On-prem.
  .DESCRIPTION
  Cmdlet prepares mailbox for migration, starts dirsync, then finishes migration after dirsync has finished.
  .EXAMPLE
  Invoke-MailboxMigration -Username meowth -GUID "0x000x00-y0yy-000y-0000-00000z0z00zz"
  #>
  [CmdletBinding(SupportsShouldProcess=$True,ConfirmImpact='Low')]
  param
   (
  [Parameter(Mandatory=$True,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True,Position=0,HelpMessage='Please enter a username')] [string]$Username,
  [Parameter(Mandatory=$True,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True,Position=1,HelpMessage='Enter a GUID')]
   [string]$GUID,  [Parameter(Mandatory=$True,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True,Position=2,HelpMessage='Enter a database')]
   [string]$Database
   )
  #region setup
  $EmailAddress = [string]::Concat($($Username),"@domain.mail.onmicrosoft.com") Â
  Enable-Remotemailbox $Username -remoteroutingaddress $EmailAddress
  Set-Remotemailbox $Username -ExchangeGUID $GUID
  Write-Output "Began migration process for $Username with remote routing address $EmailAddress and GUID $GUID"
  #endregion
  #region Dirsync
  try{Get-MsolAccountSku -ErrorAction Stop }
  catch [Microsoft.Online.Administration.Automation.MicrosoftOnlineException]
  {"Not connected to MSOnline. Now connecting."
  $O365pw = ConvertTo-SecureString -AsPlainText -Force -String s3cretP@ssw0rd!
  $O365Cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "[email protected]",$O365pw
  Connect-MsolService -Credential $O365Cred
  }
  $DirSyncTimeBefore = Get-MsolCompanyInformation | select LastDirSyncTime
  $dstimeb = $DirSyncTimeBefore.LastDirSyncTime
  Write-Output "Time before is $dstimeb"
  $ScriptBlock = {
  Invoke-Command -ComputerName dirsyncserver.domain.com {C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe `
  -psconsolefile "C:\Program Files\Windows Azure Active Directory Sync\DirSyncConfigShell.psc1" `
  -command "Start-OnlineCoexistenceSync"}
  }
  while ($timetest  -ne $True){
   $DirsyncTimeNow = Get-MsolCompanyInformation | select LastDirSyncTime
   $dstimen = $DirsyncTimeNow.LastDirSyncTime
   $timetest = $DirsyncTimeNow.LastDirSyncTime –gt $DirSyncTimeBefore.LastDirSyncTime
   Write-Output "Time now is $dstimen"
   Start-Sleep(20) #sleep for 20 seconds
  }
  #endregion
  #region mailboxMove
  $ExPW = ConvertTo-SecureString -AsPlainText -Force -String s3cretP@ssw0rd!
  $ExCred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "domain\exchangeAdmin",$ExPW
  New-MoveRequest -Outbound -RemoteTargetDatabase $Database -RemoteHostName mailserver.domain.com -RemoteCredential $ExCred -TargetDeliveryDomain domain.com -Identity [email protected]
  #endregion
  }
Anya is live and ready to show you everything. Watch her strip, dance, and perform exclusive shows just for you. Interact in real-time and make your fantasies come true.
✓ Live Streaming✓ Interactive Chat✓ Private Shows✓ HD Quality✓ Free Actions
Free to watch • No registration required • HD streaming