簡體   English   中英

重新生成 Microsoft PowerShell repo 測試項目以在 C# .net6 中安裝並運行 AZ PowerShell SDK

[英]Re-producing Microsoft PowerShell repo test project to install and Running AZ PowerShell SDK in C# .net6

我正在嘗試在 C# 中運行安裝和運行 Azure PowerShell 模塊並調用腳本。

安裝從 C# .net6.0 調用的 Azure PowerShell 模塊是可行的 這是來自 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);

項目:

<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

在此處輸入圖像描述

我已經在我的環境中重現了,我遇到了如下類似的錯誤:

在此處輸入圖像描述

然后我使用了以下命令:

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

通過使用上面的命令,我的錯誤得到了解決。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM