简体   繁体   中英

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

I am trying to run install and run Azure PowerShell modules in C# and invoke the script.

Installing Azure PowerShell modules called from C# .net6.0 is doable . This is from the Microsoft PowerShell team: https://github.com/PowerShell/PowerShellGet/blob/master/test/perf/benchmarks/BenchmarksV3RemoteRepo.cs

So I created a simple C# code to reproduce it.

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>

Errors:

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.

To view the error buffer inspect the powershell.ErrorBuffer.Result value

在此处输入图像描述

I have reproduced in my environment and I have faced similar error as below:

在此处输入图像描述

Then I have used the below command:

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

By using the above command my error got resolved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM