Automating Windows Azure Active Directory Module for Powershell Connections - Office 365
I started by exporting my password to a file so that powershell could load that from my script.
Then I created this powershell script to make the connection. At the bottom I added some powershell to tell us when the last DirSync was run too. This is set to utc by default, so I also convert that to Local Time.
$pass = get-content C:\cred.txt | convertto-securestring $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist "email@example.com",$pass $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $cred -Authentication:Basic -AllowRedirection Import-PSSession $Session | Out-String Connect-MsolService -Credential $cred | Out-String echo "Last Dir Sync Completed at:" $DATEutc = Get-MSOLCompanyInformation | select -expand LastDir* $DATEutc.ToLocalTime()