christophwille / pscore-playground

Programmatically drive PS Core with C# (mostly Exchange Online Powershell 3 and later)
MIT License
2 stars 1 forks source link

Unable to connect exchange online through azure functions app #3

Closed antonylineesh1 closed 1 year ago

antonylineesh1 commented 1 year ago

Tried connecting app-only authentication to exchange online through azure functions HttpTrigger , but it is failing at the Invoke() step


InitialSessionState iss = InitialSessionState.CreateDefault();
iss.ExecutionPolicy = Microsoft.PowerShell.ExecutionPolicy.Unrestricted;

iss.ImportPSModule(new string[] { "ExchangeOnlineManagement" });
using (var exchangeRunspace = RunspaceFactory.CreateRunspace(iss)) // Using stmts gehen nur in Core... Dreck
{
    exchangeRunspace.Open();

    using (var pipeLine = exchangeRunspace.CreatePipeline())
    {
        var connectCmd = new Command("Connect-ExchangeOnline");
        connectCmd.Parameters.Add("AppId", AppId);
        connectCmd.Parameters.Add("Organization", Organization);
        connectCmd.Parameters.Add("Certificate", Certificate);

        pipeLine.Commands.Add(connectCmd);
        try
        {
                       //Exception is thrown here
            pipeLine.Invoke();
        }
        catch (Exception ex)
        {
                        //if running through functions app this Exception is thrown:
                         **A parameter cannot be found that matches the parameter name 'AppId'.**
            throw ex;
        }
        if (pipeLine.Error != null && pipeLine.Error.Count > 0)
        {
            // check error
        }
    }
}

Exception is thrown at the line pipeLine.Invoke(); exception is A parameter cannot be found that matches the parameter name 'AppId'.

Is there any way perform app only authentication to exchange online through azure functions app

christophwille commented 1 year ago

As you can see in my samples, I package everything via containers - because otherwise the ExO cmdlets are not available. (I know of a trick to xcopy deploy to App Service, but Azure Functions I never tried to add ExO PS)

christophwille commented 1 year ago

Also note: https://github.com/christophwille/pscore-playground/tree/master/ExOonNet6/ExOonNet6 that is the latest code.