无法在Azure Functions v4中运行PowerShell脚本,但可在控制台应用程序中运行



我正试图在azure函数中执行PowerShell脚本,但它只是忽略了它,没有抛出任何错误,而且我知道这些行并没有真正执行,因为它只是即时的,从ConsoleApp(相同的代码行(运行时,大约需要5到10秒,然后就能很好地提供信息。

我研究了一下,发现我必须手动将dll文件插入到构建的输出中。问题是,我试过了,但也没有运气。

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
}
}

应该带来信息的线路是这样的:

Get-AdminPowerAppEnvironment

它在控制台应用程序上运行良好,但在Azure功能中不起作用。

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

我没有使用过这个模块,只是在PowerShell库上检查了模块的内容,这表明它使用WinForms进行身份验证

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

如果是这种情况,那么Azure函数中的PowerShell 7不支持WinForms。你可以创建一个PowerShell FunctionApp,并从C#函数调用它,看看你是否可以通过导入带有的PowerApps模块来解决Winforms问题

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

不确定这是否可行,但大多数O365类型的模块在PowerShell 7中没有得到适当的支持,需要绕过限制进行一些黑客攻击才能使它们工作。

下面是我为导入AzureAD模块创建的一个示例。

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

最新更新