0

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>
  • Check `ps.Streams.Error` after the invocation, most likely there is an error reported there. – Anatoli Beliaev Feb 04 '22 at 22:10
  • Have you gone through this Guide https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-powershell?tabs=portal – SaiSakethGuduru Feb 05 '22 at 15:58
  • thank you both, looks like there are some commands that are not supported in azure functions – Luis Leon Gonzalez Feb 07 '22 at 14:37
  • Could you please check this [thread](https://stackoverflow.com/questions/62895226/run-powershell-script-cli-command-from-c-sharp-azure-function-with-linux-system) once! –  Feb 07 '22 at 14:55
  • Any update to the issue? –  Feb 09 '22 at 17:14
  • Negative, no luck with the particular issue, when I manually print information, it shows in the console, but the powershell scripts like this one `Get-AdminPowerAppEnvironment` it doesn't bring any information. – Luis Leon Gonzalez Feb 10 '22 at 23:46

1 Answers1

0

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

BrettMiller
  • 946
  • 2
  • 5
  • 12