After Microsoft disabled the RPS for exchange my code is throwing 'System.Management.Automation.PSNotImplementedException'

Abhijit Nagle 0 Reputation points
2023-08-15T19:51:30.26+00:00

Below is my code where I am creating WSManConnectionInfo and then trying to create runspace with WSManConnectionInfo.

HttpClient Client = new HttpClient();
    var tenantId = ((dynamic)JsonConvert.DeserializeObject(Client.GetAsync(ConnectionConst.wsManTokenIssuer + pSCredential.UserName.Split('@')[1] + "/v2.0/.well-known/openid-configuration").Result.Content.ReadAsStringAsync().Result)).authorization_endpoint.ToString().Split('/')[3];
    PublicClientApplicationBuilder pcaBuilder = PublicClientApplicationBuilder.Create(ClientId);
    pcaBuilder.WithTenantId(tenantId);
    var tokenResult = pcaBuilder.Build().AcquireTokenByUsernamePassword(new[] { scope }, pSCredential.UserName, pSCredential.Password).ExecuteAsync().Result;
    System.Security.SecureString secureStringToken = new System.Security.SecureString();
    foreach (char c in ("bearer " + tokenResult.AccessToken))
    {
        secureStringToken.AppendChar(c);
    }

    PSCredential _shellCredential = new PSCredential(domainInfo.UserName, secureStringToken);();
    String wsManURIConnectionString = "https://outlook.office365.com/powershell-liveid?DelegatedOrg=" + domainInfo.Domain + "&BasicAuthToOAuthConversion=true";
    WSManConnectionInfo _wsManConnectionInfo = new WSManConnectionInfo(new Uri(wsManURIConnectionString), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", _shellCredential);
    _wsManConnectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
    _wsManConnectionInfo.MaximumConnectionRedirectionCount = 4;
    Runspace _runspace = RunspaceFactory.CreateRunspace(_wsManConnectionInfo);
    _runspace.Open();

At line Runspace _runspace = RunspaceFactory.CreateRunspace(_wsManConnectionInfo); I am getting 'System.Management.Automation.PSNotImplementedException' Exception.

And the above code was working fine till RPS was enabled for the tenant.

Any help will be appreciated.

Exchange Server Development
Exchange Server Development
Exchange Server: A family of Microsoft client/server messaging and collaboration software.Development: The process of researching, productizing, and refining new or existing technologies.
539 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,438 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Limitless Technology 44,201 Reputation points
    2023-08-16T10:50:32.9966667+00:00

    Hello there,

    This exception typically occurs when you're trying to use a feature or functionality that is not implemented or available in the current environment.

    Given that Microsoft disabled the RPS for Exchange, it's possible that the specific cmdlets, features, or functionalities you were using in your code were reliant on the RPS. When you attempt to use these features now, you might encounter this exception since the underlying infrastructure required for those features is no longer available.

    To address this issue, you would need to update your code to use alternative methods or cmdlets that are still supported and available in the environment where you're running your code.

    Hope this resolves your Query !!

    --If the reply is helpful, please Upvote and Accept it as an answer--

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.