1. Using two PowerShell console to be able to fulfill
Consider the following scenario: we want to use PowerShell for the following task- create a new Office 365 user and assign to the user additional email address (Alias).
Pay attention to the fact that this “task” requires access to two environments:
- User management (create a new user) is implemented by creating a remote PowerShell session to Office 365
- Email address management (add email alias), is implemented by creating a remote PowerShell session to Exchange Online.
The standard way to we use to execute this task, include few disadvantages:
- Using two PowerShell console to be able to fulfill this task we usually use two PowerShell consoles:
A. Creating a remote PowerShell session to Office 365 by using the shortcut “Microsoft Online Services Module for Windows PowerShell” (that includes a command that imports the Office 365 cmdlets to the PowerShell console).
B. Creating a remote PowerShell session to Exchange Online by using the built-in Windows PowerShell console.
- Dealing with “long” set of PowerShell Commands The command that we use for creating the remote session to Office 365 is quite simple (Connect-MsolService) but for creating the remote PowerShell session to Exchange Online, we need to use a “block” or a set of PowerShell commands. Most of the time, we save the PowerShell set of command in a text file, and each time that we need to create the remote session, we copy and paste the required “block” of the PowerShell commands.
I can mention additional “obstacles” such as: spelling or syntax mistake when trying to enter the PowerShell commands manually and more, but I think you get the idea.
Using a PowerShell script
In general, using a PowerShell script includes many advantages that can make our life easier. In this article, we will demonstrate how to use a PowerShell script for creating Remote PowerShell connection. In addition, the site includes articles that deal with other daily management tasks using PowerShell scripts.
1: Download and install required components
Before we can start the remote PowerShell session to office 365, we need to download the required cmdlets. The additional pre-requirement is to install the: Office 365 sign in assistant. You can find the required software component using the following link: Manage Azure AD using Windows PowerShell
2: Using the PowerShell console
3: Change the default PowerShell Execution Policy
Running the remote connection script
1. Create “script” folder
It’s recommended to create a dedicated folder that will hold the scripts that we use. For example- on drive C: created a folder named: scripts
2. Download the script
Download the Conenct0365.ps1 script, and save it in the scripts folder.
3. Open the PowerShell console
Open the PowerShell console (Start menu > All Programs > Accessories > Windows PowerShell> Windows PowerShell ).
4. Go to Script folder
Type the command: cd c:\scripts
5. Running a PowerShell script
For running a PowerShell script we first need to type “ .\ “ Charters. By using these characters, we are “telling” to the PowerShell that we want to run a script. You can manually type the “full” script name, or other option is to use the autocomplete PowerShell feature, that enable us to type the first characters of the script file name, and hit the TAB key (The “full name” of the script will be completed automatically).
6. Using script menu options
The script displays a menu with 3 options. To choose one of the options type the menu number.
Menu 1 – Login
Login in using your Administrator credentials: choosing this option, will activate set of PowerShell commands that create a remote session to Office 365 and Exchange Online.
The script includes a “sample string” that populate the username filed in the authentication window ([email protected]<Your Domain>.onmicrosoft.com). You can edit the script and replace the sample string by using your credentials. Enter your credentials and…
Walla!, you are now connected.
Menu 3 – Exit
After the remote connection was created, you need to “exit” the script menu, and start to use the required PowerShell commands. If you want to verify that you are connecting to booth of the environments you can use the following options:
Test the connection to Exchange Online by using the command: Get-Mailbox
Test the connection to Office 365 by using the command: Get-MsolUser
Menu 2 – Disconnect the remote PowerShell session
Best practice is to disconnect a remote PowerShell session after we have finished using the required management tasks. For disconnecting the remote PowerShell session choose “2” in the menu
Script PowerShell command description
If you want to read more information about the PowerShell commands that we use in the script, read the following section.
$user = “[email protected]
The use for this command is to simplify that part in which we need to enter our user name credentials. The variable named: $user, will display a predefined user name credentials. You can customize this value by editing the script, and enter your specific user name credentials.
$cred = Get-Credential -Credential $user
In this command we create a variable named: $cred that “contain” the command:
- Get-Credential – this PowerShell command will create a pop out windows that enable us to type our credentials
- We use the variable $user for pre populates the “user name section” in the authentication dialogue box. When we finish the part of the “password entry”, the information will be saved in the $cred variable, and will be used in next PowerShell commands.
The “Microsoft Online Services Module for Windows PowerShell” shortcut includes a command to import Office 365 cmdlets to the PowerShell console. In case that we don’t want to use this option, for example when we using the standard windows PowerShell console, the command will import Office 365 cmdlets to the PowerShell console.
Connect-MsolService -Credential $cred
The command that we use for creating the remote PowerShell session to office 365. We use the $cred variable for providing the required credential.
$ExchangeURL = “https://ps.outlook.com/powershell/
Create a variable that will store the URL address of the target Exchange Online server. After our credentials were verified and approved, the request will be redirected to the specific Exchange Online server that hosts the organization mailboxes.
$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $ExchangeURL -Credential $cred -Authentication Basic –AllowRedirection
The PowerShell command set that creates the remote connection to Exchange Online.
In this article we review
- The required first time configuration that we need to implement (download the required software + Change the Default PowerShell console Execution policy).
- How to run a script from the PowerShell console.
- How to use the connect-o365.ps1 script, to create a remote PowerShell session.
We really want to know what you think about the article