In this post I will go over how to connect automatically to Office 365 (Windows Azure Active Directory) and Exchange Online, without the need of typing long and complicated PowerShell commands each time! The added bonus that I would like to add to this is a method that will enable to complete these tasks and avoid having to provide our global administrator credentials, each time we run the PowerShell script.
If you are doing this for the first time then you will need a couple of components from Microsoft:
- Download and install the two Office 365 Powershell tools.
- Set the PowerShell execution policy to enable us to run a script
First we to enable our PowerShell console to run the script by running the PowerShell console as administrator and then issue this PowerShell command:
Set-ExecutionPolicy Unrestricted -force
Ok, you should now be all set to get stuck into the good stuff!
- Step 1#3 – Save the administrator password to a text file and encrypt the password using PowerShell.
- Step 2#3 – Write a PowerShell script, that will use the encrypted password. Test that we can create a remote PowerShell session to Windows Azure Active Directory + Exchange Online.
- Step 3#3 – Execution of the remote PowerShell script this will verify that the script is operating properly.
Step 1#3 – Save the administrator password to a text file and encrypt the password using PowerShell.
The complete Powershell syntax we will be using is as follows:
Read-Host -Prompt "Enter your tenant password" -AsSecureString | ConvertFrom-SecureString | Out-File "C:\Office365\Logins\cred.txt"
The screenshot below is an example of how to use this command. Note: Before running this command you will need to create a file called “cred.txt” alter the path to suit your needs.
After pressing enter you are them prompted for your password, supply your tenant admin password which will populate cred.txt.
Navigate to where you saved your cred.txt and you should see something like the below.
Step 2#3 – Write a PowerShell script, that will use the encrypted password.
This section of the script uses the UPN name of the EXO administrator followed by the encrypted password we created earlier.
$AdminName = "administrator@O365domain.com" $Pass = Get-Content "C:\Office365\Logins\cred.txt" | ConvertTo-SecureString $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $AdminName, $Pass
The top section connects to Azure Active Directory and the bottom section connects to Exchange Online.