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.
#Managing a PowerShell session #Capture administrative credential for future connections. $credential = get-credential #Creates an Exchange Online session. $ExchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $credential -Authentication Basic -AllowRedirection #Import session commands. 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. Disconnect-PSSession $session #And finally, remove your session. Remove-PSSession $session #Additional session management for PowerShell details. #Ending a session for Skype for Business is identical to the process used in this script. #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.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ""Software""), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.