Get-Ryan

[PowerShell] Connect-ExchangeOnline

Oct 7th, 2015
756
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function Connect-ExchangeOnline(){
  2.  
  3.     param(
  4.         [Switch]$CheckStatus
  5.         )
  6.  
  7.     If($CheckStatus){
  8.         #Check that connection to Exchange Online is present
  9.         $Sessions = Get-PSSession
  10.         $prereqtest = $false
  11.    
  12.         ForEach($Session in $Sessions){
  13.             If($Session.ConfigurationName -like "Microsoft.Exchange" -and $prereqtest -eq $false){
  14.                 $prereqtest = $true
  15.             }
  16.         }
  17.  
  18.         If($prereqtest -eq $false){
  19.             #Write-Host "Exchange Online connection must be present to run this cmdlet."
  20.             [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
  21.             $var = [System.Windows.Forms.MessageBox]::Show("You must be connected to Exchange Online to proceed. Would you like to connect to Exchange Online now?","Not Connected",4)
  22.             If($var -like "Yes"){
  23.                 Connect-ExchangeOnline
  24.                 Return
  25.             }
  26.             Else{
  27.                 Break
  28.             }
  29.         }
  30.         Else{
  31.             Return
  32.         }
  33.        
  34.     }#End check for Exchange Online connection
  35.  
  36.     $UserCredential = Get-Credential
  37.     $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
  38.     Import-PSSession $Session
  39.  
  40. }
Advertisement
Add Comment
Please, Sign In to add comment