Automating Windows Azure Active Directory Module for Powershell Connections - Office 365

I have been working on a PowerShell script to automated the Windows Azure Active Directory Module for Powershell Connection and login. 

I started by exporting my password to a file so that powershell could load that from my script.

 (get-credential).password|convertFrom-SecureString|set-content c:\cred.txt  


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 "superadmin@yourcompany.onmicrosoft.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()  

Comments

Popular Posts