Using of remoting with PowerShell SDK is not possible in .net6 Azure Functions
Related to https://github.com/PowerShell/PowerShell/issues/14054
We do have simple Azure Function:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
<EnableUnsafeBinaryFormatterSerialization>true</EnableUnsafeBinaryFormatterSerialization>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.20.0" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.1.0" />
<PackageReference Include="Microsoft.PowerShell.SDK" Version="7.2.2" />
<PackageReference Include="Microsoft.Store.PartnerCenter" Version="3.0.0" />
<PackageReference Include="System.Management.Automation" Version="7.2.2" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
</Project>
and corresponding code to call the PowerShell SDK:
var cca = ConfidentialClientApplicationBuilder
.Create(clientId)
.WithClientSecret(clientSecret)
.WithTenantId(tenantId)
.Build();
var ewsScopes = new string[] { "https://outlook.office365.com/.default" };
var authResult = await cca.AcquireTokenForClient(ewsScopes).ExecuteAsync();
var runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace();
var powershell = System.Management.Automation.PowerShell.Create();
var command = new System.Management.Automation.PSCommand();
var secureToken = new NetworkCredential("", $"Bearer {authResult.AccessToken}").SecurePassword;
var credentials = new System.Management.Automation.PSCredential($"OAuthUser@{tenantId}", secureToken);
// call $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://outlook.office365.com/PowerShell-LiveId?BasicAuthToOAuthConversion=true" -Credential $Ctoken -Authentication Basic -AllowRedirection -Verbose
log.LogInformation("Connecting to Exchange Online");
command.AddCommand("New-PSSession");
command.AddParameter("ConfigurationName", "Microsoft.Exchange");
command.AddParameter("ConnectionUri", new Uri(connectionUri));
command.AddParameter("Credential", credentials);
command.AddParameter("Authentication", "Basic");
powershell.Commands = command;
runspace.Open();
powershell.Runspace = runspace;
Collection<System.Management.Automation.PSObject> result = powershell.Invoke();
This works locally, but as soon as it published to the Azure the error occurred:
2022-04-20T13:15:45.704 [Error] One or more errors occurred. (An error has occurred which PowerShell cannot handle. A remote session might have ended.)BinaryFormatter serialization and deserialization are disabled within this application. See https://aka.ms/binaryformatter for more information.
Hi @antonGritsenko Thank you for the update, we will discuss this internally and let you know about the findings soon! Thanks.
Hi @Francisco-Gamino, could you please help with this issue?
Tagging @AnatoliB @michaelpeng36 and @MadhuraBharadwaj-MSFT
Hello @antonGritsenko -- Could you please describe the scenario you are trying to enable? Thank you.
@Francisco-Gamino I thought it quite clear :) I want to use Exchange PowerShell module in Azure Functions. Exch cmdlets can be used only via remoting.
@antonGritsenko -- To retrieve the token from Exchange, have you considered using a PowerShell function? For example, you could create an http trigger to return the token from Exchange. Alternatively, if need to implement you solution in .Net, then I will tag someone from the PowerShell team to provide you further guidance.
This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.