Unable to Disconnect a powershell session connected to office365

Viewed 1245

I want to disconnect from a remote powershell connected to office365 and connect back later.

Tried the Disconnect-PSSession command but it throws error.

$UserCredential = Get-Credential

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.compliance.protection.outlook.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection

.
.
.
.

Disconnect-PSSession $Session

(It redirects to https://nam04b.ps.compliance.protection.outlook.com/powershell-liveid?PSVersion=4.0)

The output I am seeing is:

disconnect-pssession : Disconnect-PSSession operation failed for runspace Id = 613e8071-b691-4365-84dc-2d65c21ea2fa
for the following reason: The disconnection operation is not supported on the remote computer. To support
disconnecting, the remote computer must be running Windows PowerShell 3.0 or a later version of Windows PowerShell.
At line:1 char:15
+ get-pssession|disconnect-pssession
+               ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: ([PSSession]Session1:PSSession) [Disconnect-PSSession], RuntimeExcepti
   on
    + FullyQualifiedErrorId : PSSessionDisconnectFailed,Microsoft.PowerShell.Commands.DisconnectPSSessionCommand

Locally I have Powershell version 4 running. And Get-PSSessionConfiguration shows PSVersion 4 at server. Any help appreciated

1 Answers

As Theo pointed out, using Remove-PSSession works instead as it is supported in versions of PowerShell lower than 3 whereas Disconnect-PSSession was only introduced in PowerShell v3. Hence your error message.

Please see this link to Disconnect-PSSession where it stats:

This cmdlet is introduced in Windows PowerShell 3.0.

Related