Calling Exchange Online PowerShell Cmdlets from C# With Modern Authentication

%3CLINGO-SUB%20id%3D%22lingo-sub-424282%22%20slang%3D%22en-US%22%3ECalling%20Exchange%20Online%20PowerShell%20Cmdlets%20from%20C%23%20With%20Modern%20Authentication%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-424282%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20Currently%20We%20can%20calling%20Exchange%20Online%20Commandlets%20With%20the%20below%20code%20%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3CSPAN%3EWSManConnectionInfo%20connectionInfo%20%3D%20new%20WSManConnectionInfo(new%20Uri(%22%3CA%20href%3D%22https%3A%2F%2Foutlook.office365.com%2FPowerShell-LiveID%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Foutlook.office365.com%2FPowerShell-LiveID%3C%2FA%3E%22)%2C%20exchangeSchema%2C%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BPowershellCommonUtilities.GetPSCredential(connectInfo.UserName%2C%20connectInfo.Password))%3B%26nbsp%3BconnectionInfo.AuthenticationMechanism%20%3D%20AuthenticationMechanism.Basic%3B%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20connectionInfo.IdleTimeout%20%3D%20idleTimeout%3B%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20connectionInfo.OpenTimeout%20%3D%20connectionTimeout%3B%3C%2FSPAN%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20exchangerunspacepool%20%3D%20RunspaceFactory.CreateRunspacePool(minRunSpace%2C%20maxRunspace%2C%20connectionInfo)%3B%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWe%20need%20to%20move%20this%20basic%20Authentication%20to%20Modern%20Authentication.%20Please%20let%20me%20know%20if%20anyone%20has%20achieved%20it.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-424282%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExchange%20Online%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EOffice%20365%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1224118%22%20slang%3D%22en-US%22%3ERe%3A%20Calling%20Exchange%20Online%20PowerShell%20Cmdlets%20from%20C%23%20With%20Modern%20Authentication%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1224118%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20Vibhor%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EJust%20wanted%20to%20check%20if%20you%20found%20any%20working%20solution%20for%20this.%26nbsp%3B%3C%2FP%3E%3CP%3EI.e%20trying%20to%20invoke%20exchange%20power%20shell%20commands%20from%20any%20service%20using%20oauth%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1224123%22%20slang%3D%22en-US%22%3ERe%3A%20Calling%20Exchange%20Online%20PowerShell%20Cmdlets%20from%20C%23%20With%20Modern%20Authentication%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1224123%22%20slang%3D%22en-US%22%3E%3CP%3EHello%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F579988%22%20target%3D%22_blank%22%3E%40Us9898%3C%2FA%3E%26nbsp%3B%20%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWe%20are%20using%26nbsp%3B%3CSPAN%3EExchange%20Online%20Remote%20PowerShell%20Module%3C%2FSPAN%3E%20%2C%20to%20invoke%20cmdlets%20using%20modern%20authentication.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CFONT%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fexchange%2Fexchange-online%2Fconnect-to-exchange-online-powershell%2Fmfa-connect-to-exchange-online-powershell%3Fview%3Dexchange-ps%23connect-to-exchange-online-powershell-by-using-mfa%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fexchange%2Fexchange-online%2Fconnect-to-exchange-online-powershell%2Fmfa-connect-to-exchange-online-powershell%3Fview%3Dexchange-ps%23connect-to-exchange-online-powershell-by-using-mfa%3C%2FA%3E%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1224310%22%20slang%3D%22en-US%22%3ERe%3A%20Calling%20Exchange%20Online%20PowerShell%20Cmdlets%20from%20C%23%20With%20Modern%20Authentication%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1224310%22%20slang%3D%22en-US%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F318313%22%20target%3D%22_blank%22%3E%40VibhorM%3C%2FA%3E%26nbsp%3B%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ethanks%20for%20the%20quick%20reply%2C%20I%20am%20trying%20to%20execute%20this%20via%20backed-services.%3C%2FP%3E%3CP%3Ewhich%20will%20not%20need%20user%20interaction.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESo%20i%20can%20get%20the%20token%20but%20after%20that%20i%20cannot%20connect%20to%20the%20API%2C%20via%20power%20shell.%26nbsp%3B%3C%2FP%3E%3CP%3EWondering%20if%20you%20got%20it%20working%20.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ethank%20you%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
New Contributor

Hi Currently We can calling Exchange Online Commandlets With the below code :

 WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("https://outlook.office365.com/PowerShell-LiveID"), exchangeSchema,
                   PowershellCommonUtilities.GetPSCredential(connectInfo.UserName, connectInfo.Password)); connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
                connectionInfo.IdleTimeout = idleTimeout;
                connectionInfo.OpenTimeout = connectionTimeout;

                exchangerunspacepool = RunspaceFactory.CreateRunspacePool(minRunSpace, maxRunspace, connectionInfo);

 

We need to move this basic Authentication to Modern Authentication. Please let me know if anyone has achieved it.

3 Replies
Highlighted

Hi Vibhor,

 

Just wanted to check if you found any working solution for this. 

I.e trying to invoke exchange power shell commands from any service using oauth

Highlighted

Hello @Us9898  ,

 

We are using Exchange Online Remote PowerShell Module , to invoke cmdlets using modern authentication.

 

https://docs.microsoft.com/en-us/powershell/exchange/exchange-online/connect-to-exchange-online-powe...

 

Highlighted

Hi @VibhorM ,

 

thanks for the quick reply, I am trying to execute this via backed-services.

which will not need user interaction. 

 

So i can get the token but after that i cannot connect to the API, via power shell. 

Wondering if you got it working .

 

thank you