Luis Leon Gonzalez
Luis Leon Gonzalez

Reputation: 91

Cannot Run PowerShell Scripts in Azure Functions v4 but works in console app

I am trying to execute PowerShell scripts in azure functions but it just ignores it, doesn't throw any errors, and I know the lines are not being really executed because it's just instant, and when running from ConsoleApp (same lines of code) it takes like 5 to 10 seconds and then brings the information well.

I looked into it and what I found is that I have to MANUALLY insert dll files to the output of the build. The thing is that I tried that and didn't have luck either.

        using (PowerShell ps = PowerShell.Create())
        {
            foreach (var line in query)
            {
                ps.AddScript(line);
                ps.AddParameters(prms);
                var pipelineObjects = await ps.InvokeAsync().ConfigureAwait(false);
                foreach (var item in pipelineObjects)
                {
                    if (item != null) { } // it's always null here
                }
         }

The line that should bring information is this one:

Get-AdminPowerAppEnvironment

It works on Console App well, but not in Azure Function.

<TargetFramework>net6.0</TargetFramework>
<AzureFunctionsVersion>v4</AzureFunctionsVersion>

Upvotes: 0

Views: 751

Answers (1)

BrettMiller
BrettMiller

Reputation: 1046

I've not used this module but just checking over the contents of the module on the PowerShell gallery it suggests that it users WinForms for authentication

https://www.powershellgallery.com/packages/Microsoft.PowerApps.Administration.PowerShell/2.0.27/Content/Microsoft.PowerApps.Administration.PowerShell.psd1

If this is the case then WinForms is not supported in PowerShell 7 within Azure Functions. You could create a PowerShell FunctionApp and call that from your C# function and see if you can workaround the Winforms issue by importing the PowerApps module with:

Import-Module Microsoft.PowerApps.Administration.PowerShell -UseWindowsPowerShell

Not sure if this would work but most of the O365 type modules aren't properly supported in PowerShell 7 and take some hacking around the limitations to get them to work.

Here is an example of one I created for importing the AzureAD module.

https://github.com/brettmillerb/azureadFunction/blob/main/addUser/run.ps1

Upvotes: 0

Related Questions