Last active
June 25, 2019 22:31
-
-
Save 45413/9c9a19541d372b82a05acb406386d645 to your computer and use it in GitHub Desktop.
Powershell: Office365 Remote Powershell Session Management
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Connect-Office365 | |
{ | |
[CmdletBinding()] | |
[Alias()] | |
[OutputType([void])] | |
Param | |
( | |
# Param1 help description | |
[Parameter(Position=0)] | |
[string]$username="", | |
[Parameter()] | |
[ValidateNotNull()] | |
[System.Management.Automation.PSCredential] | |
[System.Management.Automation.Credential()] | |
$Credential = [System.Management.Automation.PSCredential]::Empty | |
) | |
if (($username -eq "") -and ($Credential -eq [System.Management.Automation.PSCredential]::Empty)) | |
{ | |
Write-Error "No username or Credential object provided" -ErrorAction Stop | |
} | |
elseif ($Credential -ne [System.Management.Automation.PSCredential]::Empty) { | |
write-verbose "Credential Passed. Skipping request for password" | |
} | |
Else { | |
Write-Verbose "Username provided: $username" | |
$SecurePassword = Read-Host -AsSecureString -Prompt "Password for $username" | |
$Credential = New-Object System.Management.Automation.PSCredential -ArgumentList $username, $SecurePassword | |
} | |
if(Test-Office365SessionState) { | |
Write-Verbose "Session already connected" | |
return | |
} else { | |
#Connect to Office 365 | |
Write-Verbose "Connecting to Office 365 as $($Credential.UserName)..." | |
#$session = New-PSSession -Name "Office365" -ConfigurationName Microsoft.Exchange -ConnectionUri "https://ps.outlook.com/powershell/" -Credential $Credential -Authentication Basic -AllowRedirection | |
$session = New-PSSession -Name "Office365" -ConfigurationName Microsoft.Exchange -ConnectionUri "https://outlook.office365.com/powershell-liveid/" -Credential $Credential -Authentication Basic -AllowRedirection | |
$PSCmdlet.SessionState.PSVariable.Set("_o365SessionName", "Office365") | |
Import-Module (Import-PSSession $session -DisableNameChecking -AllowClobber -WarningAction SilentlyContinue) -WarningAction SilentlyContinue -Global | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Disconnect-Office365 { | |
[CmdletBinding()] | |
param ( | |
) | |
if(Test-Office365SessionState) { | |
$sessionName = $PSCmdlet.SessionState.PSVariable.Get("_o365SessionName").Value | |
Remove-PSSession -name $sessionName | |
$PSCmdlet.SessionState.PSVariable.Remove("_o365SessionName") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Test-Office365SessionState { | |
[CmdletBinding()] | |
param ( | |
) | |
$sessionName = $PSCmdlet.SessionState.PSVariable.Get("_o365SessionName").Value | |
if ($sessionName.length -gt 0) { | |
$session = $(Get-PSSession -name $sessionName -ErrorAction SilentlyContinue) | |
if( $session -and ($session.State -eq "Opened") ) { | |
return $true | |
} else { | |
return $false | |
} | |
} else { | |
return $false | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment