代码之家  ›  专栏  ›  技术社区  ›  Node.JS

重新生成Microsoft PowerShell repo测试项目以在C#.net6中安装和运行AZ PowerShell SDK

  •  0
  • Node.JS  · 技术社区  · 3 年前

    我正在尝试在C#中运行安装和运行Azure PowerShell模块并调用脚本。

    正在安装从C调用的Azure PowerShell模块# .net6.0是可行的 。这来自Microsoft PowerShell团队: https://github.com/PowerShell/PowerShellGet/blob/master/test/perf/benchmarks/BenchmarksV3RemoteRepo.cs

    所以我创建了一个简单的C#代码来复制它。

    using System.Diagnostics;
    using System.Management.Automation;
    using System.Management.Automation.Runspaces;
    using Microsoft.PowerShell;
    
    // Setting up the PowerShell runspace
    var defaultSS = InitialSessionState.CreateDefault2();
    defaultSS.ExecutionPolicy = ExecutionPolicy.Unrestricted;
    var pwsh = PowerShell.Create(defaultSS);
    
    // Import the PSGet version we want to test
    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.
    

    要查看错误缓冲区,请检查 powershell.ErrorBuffer.Result 价值

    enter image description here

    0 回复  |  直到 3 年前
        1
  •  0
  •   RithwikBojja    3 年前

    我在我的环境中进行了复制,我遇到了类似的错误,如下所示:

    enter image description here

    然后我使用了以下命令:

    Install-Module -Name PowerShellGet -RequiredVersion 3.0.14-beta14 -AllowPrerelease -Force
    

    通过使用上面的命令,我的错误得到了解决。

    推荐文章