在.NET 6中通过C#调用Azure PowerShell模块遇加载错误求助
.NET 6项目中调用Azure PowerShell模块失败的解决方法
问题背景
在.NET 6的C#项目中尝试安装并调用Azure PowerShell模块,参考官方示例编写代码后,运行时出现模块加载失败及命令未识别的错误。
测试代码
using System.Diagnostics; using System.Management.Automation; using System.Management.Automation.Runspaces; using Microsoft.PowerShell; // 配置PowerShell运行空间 var defaultSS = InitialSessionState.CreateDefault2(); defaultSS.ExecutionPolicy = ExecutionPolicy.Unrestricted; var pwsh = PowerShell.Create(defaultSS); // 导入指定版本的PowerShellGet pwsh.AddScript(@" [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 Import-Module PowerShellGet -RequiredVersion 3.0.14 -Force"); var result = pwsh.Invoke(); foreach (var outputItem in result) Debug.WriteLine(outputItem); pwsh.Commands.Clear(); pwsh.AddScript("Install-PSResource -Name Az -Repository PSGallery -TrustRepository -Reinstall"); var results = pwsh.Invoke(); foreach (var outputItem in results) Debug.WriteLine(outputItem);
项目csproj配置
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net6.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.PowerShell.Commands.Diagnostics" Version="7.2.7"/> <PackageReference Include="Microsoft.PowerShell.Commands.Management" Version="7.2.7"/> <PackageReference Include="Microsoft.PowerShell.Commands.Utility" Version="7.2.7"/> <PackageReference Include="Microsoft.PowerShell.ConsoleHost" Version="7.2.7"/> <PackageReference Include="Microsoft.PowerShell.CoreCLR.Eventing" Version="7.2.7"/> <PackageReference Include="Microsoft.PowerShell.Native" Version="7.3.0"/> <PackageReference Include="Microsoft.PowerShell.SDK" Version="7.2.7"/> <PackageReference Include="Microsoft.PowerShell.Security" Version="7.2.7"/> <PackageReference Include="System.Management.Automation" Version="7.2.7"/> </ItemGroup> </Project>
运行错误信息
The specified module 'PowerShellGet' with version '3.0.14' was not loaded because no valid module file was found in any module directory. The term 'Install-PSResource' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
解决方法
1. 预安装PowerShellGet 3.0.14模块
先在本地PowerShell环境中执行以下命令,确保模块已安装到系统模块路径:
Install-Module -Name PowerShellGet -RequiredVersion 3.0.14 -Force -AllowClobber
2. 显式指定模块路径加载
在创建InitialSessionState时,直接导入PowerShellGet模块的具体路径,确保运行空间能找到该模块:
var defaultSS = InitialSessionState.CreateDefault2(); // 根据实际安装路径调整,Windows默认路径示例 defaultSS.ImportPSModule(new[] { @"C:\Program Files\PowerShell\Modules\PowerShellGet\3.0.14" }); defaultSS.ExecutionPolicy = ExecutionPolicy.Unrestricted; var pwsh = PowerShell.Create(defaultSS);
3. 改用兼容的Install-Module命令
若Install-PSResource仍无法识别,可先使用PowerShellGet的传统命令安装Az模块:
pwsh.Commands.Clear(); pwsh.AddScript(@" Install-Module -Name Az -Repository PSGallery -TrustRepository -Force -AllowClobber "); var results = pwsh.Invoke();
4. 验证运行空间的模块搜索路径
在代码中添加输出模块路径的逻辑,确认运行空间的PSModulePath是否包含PowerShellGet所在目录:
pwsh.Commands.Clear(); pwsh.AddScript("$env:PSModulePath"); var pathResults = pwsh.Invoke(); foreach (var path in pathResults) Debug.WriteLine($"模块搜索路径:{path}");
5. 通过NuGet嵌入PowerShellGet模块
在csproj中添加PowerShellGet的NuGet包引用,将模块嵌入到项目输出中:
<PackageReference Include="PowerShellGet" Version="3.0.14" />
之后在代码中指定该模块的本地路径(通常在项目输出目录的Modules文件夹下)进行导入。
内容的提问来源于stack exchange,提问作者Node.JS
相关产品推荐
相关产品推荐

