PowerShell SDK 7.2.x unable to connect to Exchange remotes
See original GitHub issuename: Bug report🐛 about: Unable to connect to exchange powershell with PowerShell.net 7.1x title: “Unable to connect to exchange powershell with PowerShell.net 7.1x” labels: Needs-Triage assignees: ‘’
Steps to reproduce
//Set Credential
SecureString s = new SecureString();
Config.GetValue<string>(pass).ToCharArray().ToList().ForEach(p => s.AppendChar(p));
//Use a public client app to get a OAuth token for CUBOT
var publicClientApplication = PublicClientApplicationBuilder.Create(appid).WithTenantId(Config.GetSection("AzureAd").GetValue<string>("tenantId")).Build();
var tokenworker = publicClientApplication.AcquireTokenByUsernamePassword(new string[] { "https://outlook.office365.com/.default" }, Config.GetValue<string>(user), s).ExecuteAsync();
tokenworker.Wait();
Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace();
PowerShell powershell = PowerShell.Create();
PSCommand command = new PSCommand();
s = new SecureString();
//convert the OAuth Token into a SecureString
$"Bearer {tokenworker.Result.AccessToken}".ToCharArray().ToList().ForEach(p => s.AppendChar(p));
//Create a Powershell Credential using the CUBot User and the OAuth SecureString
PSCred = new PSCredential(Config.GetValue<string>("poshuser"), s);
command.AddCommand("New-PSSession");
command.AddParameter("ConfigurationName", "Microsoft.Exchange");
//convert basic auth to modern with the query string
command.AddParameter("ConnectionUri", new Uri("https://outlook.office365.com/powershell-liveid/?BasicAuthToOAuthConversion=true"));
command.AddParameter("Credential", PSCred);
//Yes still basic auth but we are passing the OAuth token not a password
command.AddParameter("Authentication", "Basic");
command.AddParameter("AllowRedirection");
powershell.Commands = command;
runspace.Open();
powershell.Runspace = runspace;
Result = powershell.Invoke();
Expected behavior
successfully connects to exchange online
Actual behavior
unable to connect
Environment data
Name Value
---- -----
PSVersion 7.1.3
PSEdition Core
GitCommitId 7.1.3
OS Microsoft Windows 10.0.21387
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
C# .net core 5
Windows 10/Azure App Service
Issue is in all 7.1.x builds of the SDK System.Management.Automation namespaces. This script works in 7.0.x but does not in 7.1.x
The error you get is to unable to connect to remote resource/connection closed. Again this code works in 7.0.x (inc 7.0.6) but does not in 7.1.3
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:8 (1 by maintainers)
Top Results From Across the Web
Unable to connect to Exchange Online PowerShell Module
Unable to connect to Exchange Online PowerShell Module · Open PowerShell: Look for PowerShell in the start menu and open it. · Load...
Read more >Unable to Connect to Exchange via Powershell remote
I'm trying to connec to an on premise Exchange 2016 server via remote powershell using the below 3 commands. Powershell. $LiveCred = Get- ......
Read more >how to get winrm to use powershell 7 for remote sessions ...
When remoting, default to running PowerShell Core v7.x on the # the target machines: $PSSessionConfigurationName = 'PowerShell.7'.
Read more >How to Install Azure AD module in PowerShell
Open PowerShell with Elevated permissions. – Right-click on your Start menu (or press Windows key + X) · Install Azure AD Module. Type...
Read more >PowerShell – Kemp Support
To enable it using the Web User Interface (WUI), go to Certificates & Security > Remote Access and select Enable API Interface. You...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Any update on this issue? Testing in .net 6 and the latest preview (7.2.0) of the SDK.
Any updates? Can’t PS remote from Azure Function (C#, net6) even when EnableUnsafeBinaryFormatterSerialization is enabled in project