More Functionality

Managing your PowerShell sessions

Scenario description

In PowerShell, a session is the equivalent to having a console open on a remote server. For Office 365, these sessions enable you to administer your online services.

Each Office 365 service has a different way to create a session. In this script we have used Exchange Online as an example.

Importing a PowerShell session makes the commands in the remote session available for use within your active PowerShell window.
Note: All imported commands are executed on the remote service you are connect to. Executing any command imported from your session will not affect your local machine. 

Once you have finished with your session, it is best practice to disconnect and remove it. This enables your session to end gracefully, and frees up resources both locally and within your Office 365 tenant.

Ending a session for Skype for Business is identical to the process used in this scenario.
However, for a SharePoint online session, we simply replace the Disconnect-PSSession and Remove-PSSession commands with ‘Disconnect-SPOService’

No additional arguments are needed to perform this command.

Managing your PowerShell sessions

Managing a PowerShell session - Exchange Online.

Capture administrative credential for future connections. Creates an Exchange Online session. Import session commands.

$credential = get-credential
$ExchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $credential -Authentication Basic -AllowRedirection
Import-PSSession $ExchangeSession

The commands in this session are now available for you to use as required. In this example, we will simply use a command that retrieves information.

Get-Mailbox

Once finished, it is important to close your session.

Remove-PSSession $ExchangeSession

Managing a PowerShell session - Skype for Business.
As we are working with Skype for Business, we must first import the related PowerShell module.

Import-Module LyncOnlineConnector

Capture administrative credential for future connections. Creates a Skype for Business session. Import session commands.

$credential = get-credential
$Session = New-CSOnlineSession -Credential $credential 
Import-PSSession $Session

The commands in this session are now available for you to use as required. In this example, we will simply use a command that retrieves information.

Get-CSOnlineUser

Once finished, it is important to close your session.

Remove-PSSession $session

Managing a PowerShell session - SharePoint online.
As we are working with SharePoint Online, we must first import the related PowerShell module

Import-Module Microsoft.Online.SharePoint.PowerShell

Capture administrative credential for future connections. Creates a SharePoint online session, and imports it for use.
For SharePoint online, This command additionally does the work of Import-PSSession

$credential = get-credential
Connect-MsolService -Credential $Credential

This connects you to your SharePoint Online services. Substitute the ‘contoso’ portion of the URL with the name of your SharePoint Online tenant.

Connect-SPOService -url https://contoso-admin.sharepoint.com -Credential $credential

The commands in this session are now available for you to use as required.
In this example, we will simply use a command that retrieves information.

Get-SPOSite

Once finished, it is important to close and remove your session.

Disconnect-SPOService

Viewing active PowerShell Sessions.

Running the following allows you to view all PowerShell sessions and the current state.

Get-PSSession

If you wanted to close all of these, you can use the following command.

Get-PSSession | Remove-PSSession