Wednesday, June 25, 2014

Scripting Office 365 and DirSync Replication

If you are running the new version of DirSync, you probably noticed that the "DirSyncConfigShell.psc1" file is missing. They have also changed the way your start a sync. The new way (documented here: Click Me) you have to import the module, then run the command. Well, I attempted to run this remotely on my desktop and it just hung every time. Here is what I was running:
Invoke-Command -ComputerName SERVER -ScriptBlock { import-module dirsync; Start-OnlineCoexistenceSync }
Then, with the help of a friend at he reminded me to turn on -verbose so that I could see exactly what the PowerShell was doing. I found that when importing the module, it was attempting to dot-source a file named .\importmodules.ps1 put there by DirSync. Ok, now what is wrong with that file? So I cracked it open and ran each command, one at a time, in a ISE environment and found that it was attempting to run with .net 4.0 and since I had .net 2.0 on my PowerShell window (by default), it was hanging.
Whew… ok now let's find a way to change the PowerShell window to get it to run .net 4.0 for this command. Here is what I came up with:

reg add hklm\software\microsoft\.netframework /v OnlyUseLatestCLR /t REG_DWORD /d 1
reg add hklm\software\wow6432node\microsoft\.netframework /v OnlyUseLatestCLR /t REG_DWORD /d 1
Invoke-Command -ComputerName SERVER -ScriptBlock { import-module dirsync -verbose; Start-OnlineCoexistenceSync }
reg delete hklm\software\microsoft\.netframework /v OnlyUseLatestCLR /f
reg delete hklm\software\wow6432node\microsoft\.netframework /v OnlyUseLatestCLR /f

So I through that into my script and BAM it started working again. Thanks to all the people online (twitter, reddit, etc…) for your help.