从C#Azure函数应用程序调用PowerShell
我想在V2 C#Azure函数应用程序中运行PowerShell命令。我曾尝试从Visual Studio中的.NET Core 2.1项目运行PowerShell Core(据我所知,这是使用.NET Core访问PS的唯一方法),该项目运行得很好,但是,使用C#Azure Function App(使用.NET Core)项目时,我在尝试访问URL时遇到以下错误:从C#Azure函数应用程序调用PowerShell,c#,.net,azure,powershell,azure-functions,C#,.net,Azure,Powershell,Azure Functions,我想在V2 C#Azure函数应用程序中运行PowerShell命令。我曾尝试从Visual Studio中的.NET Core 2.1项目运行PowerShell Core(据我所知,这是使用.NET Core访问PS的唯一方法),该项目运行得很好,但是,使用C#Azure Function App(使用.NET Core)项目时,我在尝试访问URL时遇到以下错误: Executed 'Function1' (Failed, Id=dbb00673-938b-4d50-9576-9b3a4ec
Executed 'Function1' (Failed, Id=dbb00673-938b-4d50-9576-9b3a4ec06c06) [2018-11-05 09:51:17] System.Private.CoreLib: Exception while executing function: Function1. AmberTest: Could not load file or assembly 'System.Management.Automation, Version=6.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified. System.Private.CoreLib: Could not load the specified file.
我已安装以下NuGet软件包(适用于PowerShell Core):
- Microsoft.PowerShell.Commands.Diagnostics 1.0.23
- Microsoft.PowerShell.SDK 6.1.0
- Microsoft.WSMan.Management 6.1.0
问题是运行时的程序集,即
[FunctionProject]\bin\Debug\netcoreapp2.1\bin\runtimes
文件夹未加载到函数上下文中,因此我们看到无法加载文件或程序集
错误
解决方法是将这些程序集添加到bin
文件夹,即[FunctionProject]\bin\Debug\netcoreapp2.1\bin
文件夹
右键单击函数项目,Edit.csproj
在下面添加内容。第一项将程序集复制到发布目录,第二项复制任务用于本地调试
<PropertyGroup>
<SDKVersion>6.1.0</SDKVersion>
<SDKPlatform>win-x86</SDKPlatform>
</PropertyGroup>
<ItemGroup>
<None Include="
$(USERPROFILE)\.nuget\packages\system.management\4.5.0\runtimes\win\lib\netcoreapp2.0\System.Management.dll;
$(USERPROFILE)\.nuget\packages\system.management.automation\$(SDKVersion)\runtimes\$(SDKPlatform)\lib\netstandard2.0\System.Management.Automation.dll;
$(USERPROFILE)\.nuget\packages\microsoft.management.infrastructure\1.0.0\runtimes\win10-x86\lib\netstandard1.6\Microsoft.Management.Infrastructure.dll;
$(USERPROFILE)\.nuget\packages\microsoft.powershell.commands.management\$(SDKVersion)\runtimes\$(SDKPlatform)\lib\netstandard2.0\Microsoft.PowerShell.Commands.Management.dll;
$(USERPROFILE)\.nuget\packages\microsoft.powershell.commands.utility\$(SDKVersion)\runtimes\$(SDKPlatform)\lib\netstandard2.0\Microsoft.PowerShell.Commands.Utility.dll;
$(USERPROFILE)\.nuget\packages\microsoft.powershell.commands.diagnostics\$(SDKVersion)\runtimes\$(SDKPlatform)\lib\netstandard2.0\Microsoft.PowerShell.Commands.Diagnostics.dll;
$(USERPROFILE)\.nuget\packages\microsoft.powershell.sdk\$(SDKVersion)\runtimes\$(SDKPlatform)\lib\netstandard2.0\Microsoft.PowerShell.SDK.dll;
$(USERPROFILE)\.nuget\packages\microsoft.powershell.security\$(SDKVersion)\runtimes\$(SDKPlatform)\lib\netstandard2.0\Microsoft.PowerShell.Security.dll;
$(USERPROFILE)\.nuget\packages\microsoft.powershell.coreclr.eventing\$(SDKVersion)\runtimes\$(SDKPlatform)\lib\netstandard2.0\Microsoft.PowerShell.CoreCLR.Eventing.dll;
$(USERPROFILE)\.nuget\packages\microsoft.powershell.consolehost\$(SDKVersion)\runtimes\$(SDKPlatform)\lib\netstandard2.0\Microsoft.PowerShell.ConsoleHost.dll;
$(USERPROFILE)\.nuget\packages\microsoft.powershell.markdownrender\$(SDKVersion)\runtimes\$(SDKPlatform)\lib\netstandard2.0\Microsoft.PowerShell.MarkdownRender.dll;
$(USERPROFILE)\.nuget\packages\microsoft.wsman.runtime\$(SDKVersion)\runtimes\$(SDKPlatform)\lib\netstandard2.0\Microsoft.WSMan.Runtime.dll;
$(USERPROFILE)\.nuget\packages\microsoft.wsman.management\$(SDKVersion)\runtimes\$(SDKPlatform)\lib\netstandard2.0\Microsoft.WSMan.Management.dll;
">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<Target Name="CopyRuntimeToBin" AfterTargets="Build">
<Copy SourceFiles="
$(USERPROFILE)\.nuget\packages\system.management\4.5.0\runtimes\win\lib\netcoreapp2.0\System.Management.dll;
$(USERPROFILE)\.nuget\packages\system.management.automation\$(SDKVersion)\runtimes\$(SDKPlatform)\lib\netstandard2.0\System.Management.Automation.dll;
$(USERPROFILE)\.nuget\packages\microsoft.management.infrastructure\1.0.0\runtimes\win10-x86\lib\netstandard1.6\Microsoft.Management.Infrastructure.dll;
$(USERPROFILE)\.nuget\packages\microsoft.powershell.commands.management\$(SDKVersion)\runtimes\$(SDKPlatform)\lib\netstandard2.0\Microsoft.PowerShell.Commands.Management.dll;
$(USERPROFILE)\.nuget\packages\microsoft.powershell.commands.utility\$(SDKVersion)\runtimes\$(SDKPlatform)\lib\netstandard2.0\Microsoft.PowerShell.Commands.Utility.dll;
$(USERPROFILE)\.nuget\packages\microsoft.powershell.commands.diagnostics\$(SDKVersion)\runtimes\$(SDKPlatform)\lib\netstandard2.0\Microsoft.PowerShell.Commands.Diagnostics.dll;
$(USERPROFILE)\.nuget\packages\microsoft.powershell.sdk\$(SDKVersion)\runtimes\$(SDKPlatform)\lib\netstandard2.0\Microsoft.PowerShell.SDK.dll;
$(USERPROFILE)\.nuget\packages\microsoft.powershell.security\$(SDKVersion)\runtimes\$(SDKPlatform)\lib\netstandard2.0\Microsoft.PowerShell.Security.dll;
$(USERPROFILE)\.nuget\packages\microsoft.powershell.coreclr.eventing\$(SDKVersion)\runtimes\$(SDKPlatform)\lib\netstandard2.0\Microsoft.PowerShell.CoreCLR.Eventing.dll;
$(USERPROFILE)\.nuget\packages\microsoft.powershell.consolehost\$(SDKVersion)\runtimes\$(SDKPlatform)\lib\netstandard2.0\Microsoft.PowerShell.ConsoleHost.dll;
$(USERPROFILE)\.nuget\packages\microsoft.powershell.markdownrender\$(SDKVersion)\runtimes\$(SDKPlatform)\lib\netstandard2.0\Microsoft.PowerShell.MarkdownRender.dll;
$(USERPROFILE)\.nuget\packages\microsoft.wsman.runtime\$(SDKVersion)\runtimes\$(SDKPlatform)\lib\netstandard2.0\Microsoft.WSMan.Runtime.dll;
$(USERPROFILE)\.nuget\packages\microsoft.wsman.management\$(SDKVersion)\runtimes\$(SDKPlatform)\lib\netstandard2.0\Microsoft.WSMan.Management.dll;
" DestinationFolder="$(OutputPath)\bin" />
</Target>
6.1.0
win-x86
保存最新
但就目前而言,这是正确的,并建议PowerShell函数应用程序即将进入V2。尽管如此,我的问题仍然没有答案,因为我需要从C#Function应用程序访问PowerShell。是的,我的目标是在今年年底之前有一个私人预览。至于这个问题,我不太清楚。它在本地有效吗?它是否仅在常规的.NET core控制台应用程序中工作?对于常规的.NET core控制台应用程序,我可以运行PS core命令,但对于Azure功能项目,我无法执行同样的操作-这在本地和Azure中都不工作,因为我遇到了一个错误(我在问题中提到的错误)当我尝试访问提供的URL时。我的建议对您有用吗?请随时询问您是否仍然受到阻碍。否则你能接受答案来结束你的问题吗?它就像一个符咒!非常感谢你,很抱歉反应太晚。嘿,我也想实现同样的目标。在linux环境中的C#Azure函数中运行PowerShell命令。你能提供如何调用命令的示例c#代码段吗?Jerry Liu的回答确实解决了这个问题,但我遇到了新的问题,由于时间压力,我选择在azure函数上运行PS。在这里应用答案后,您可能会更幸运地发布一个关于您仍然存在的问题的新问题。