Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Function Connect-ExchangeOnline(){
- param(
- [Switch]$CheckStatus
- )
- If($CheckStatus){
- #Check that connection to Exchange Online is present
- $Sessions = Get-PSSession
- $prereqtest = $false
- ForEach($Session in $Sessions){
- If($Session.ConfigurationName -like "Microsoft.Exchange" -and $prereqtest -eq $false){
- $prereqtest = $true
- }
- }
- If($prereqtest -eq $false){
- #Write-Host "Exchange Online connection must be present to run this cmdlet."
- [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
- $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)
- If($var -like "Yes"){
- Connect-ExchangeOnline
- Return
- }
- Else{
- Break
- }
- }
- Else{
- Return
- }
- }#End check for Exchange Online connection
- $UserCredential = Get-Credential
- $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
- Import-PSSession $Session
- }
Advertisement
Add Comment
Please, Sign In to add comment