Advertisement
AutomateMyStuff

Powershell - O365 Connect and Disconnect Functions

Jan 14th, 2016
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function Connect-<TENANT>
  2.     {
  3.         Param(
  4.             $cred_<TENANT> = (Get-Credential -Message "<TENANT> Credentials" -UserName "admin@<TENANT>")
  5.         )
  6.         Try{
  7.             #CONNECT TO OFFICE365
  8.             Connect-MsolService -Credential $cred_<TENANT>
  9.  
  10.             #CONNECT TO SHAREPOINT ONLINE
  11.             Connect-SPOService -Url https://<TENANT>-admin.sharepoint.com -Credential $cred_<TENANT>
  12.  
  13.             #CONNECT TO SKYPE FOR BUSINESS ONLINE
  14.             $sfboSession_<TENANT> = New-CsOnlineSession -Credential $cred_<TENANT>
  15.             Import-PSSession $sfboSession_<TENANT>
  16.  
  17.             #CONNECT TO EXCHANGE ONLINE
  18.             $exchangeSession_<TENANT> = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://outlook.office365.com/powershell-liveid/" -Credential $cred_<TENANT> -Authentication Basic -AllowRedirection
  19.             Import-PSSession $exchangeSession_<TENANT> -DisableNameChecking
  20.  
  21.             #CONNECT TO COMPLIANCE CENTER
  22.             $ccSession_<TENANT> = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://ps.compliance.protection.outlook.com/powershell-liveid/" -Credential $cred_<TENANT> -Authentication Basic -AllowRedirection
  23.             Import-PSSession $ccSession_<TENANT> -Prefix cc
  24.  
  25.             Clear-Host
  26.  
  27.             Write-Host 'You are now managing the <TENANT> domain'
  28.             (Get-Host).UI.RawUI.WindowTitle = ($originalUIRawUIWindowTitle + " - You are now managing the <TENANT> domain")
  29.         }
  30.         Catch{
  31.             $_.Exception.Message
  32.         }
  33.     }
  34.  
  35.     Function Disconnect-<TENANT>
  36.     {
  37.         Param()
  38.         #Remove-PSSession $sfboSession_<TENANT>
  39.         #Remove-PSSession $exchangeSession_<TENANT>
  40.         #Remove-PSSession $ccSession_<TENANT>
  41.         Get-PSSession | Remove-PSSession
  42.         Disconnect-SPOService
  43.         Write-Host 'You are no longer managing the <TENANT> domain'
  44.         (Get-Host).UI.RawUI.WindowTitle = ($originalUIRawUIWindowTitle + " - You are no longer managing the <TENANT> domain")
  45.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement