【问题标题】:Unable to install NuGet provider by invoking powershell in C#无法通过在 C# 中调用 powershell 来安装 NuGet 提供程序
【发布时间】:2021-09-11 17:07:16
【问题描述】:

我从下面的 C# 方法调用 powershell cmdlet,它在 Install-Module Name CosmosDb 行失败,并出现以下错误

“使用“2”个参数调用“ShouldContinue”的异常:“一个命令 提示用户失败,因为主机程序或命令 type 不支持用户交互。主机试图 使用以下消息请求确认:PowerShellGet 需要 NuGet 提供程序版本“2.8.5.201”或更高版本才能与之交互 基于 NuGet 的存储库。 NuGet 提供程序必须在 'C:\Program Files\PackageManagement\ProviderAssemblies' 或 'C:\Users\UserName\AppData\Local\PackageManagement\ProviderAssemblies'。 您还可以通过运行安装 NuGet 提供程序 'Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -力量'。是否希望 PowerShellGet 现在安装和导入 NuGet 提供程序?"

C#代码:

{            
            InitialSessionState initialSessionState = InitialSessionState.CreateDefault();
            initialSessionState.ExecutionPolicy = ExecutionPolicy.Unrestricted;
            using Runspace runspace = RunspaceFactory.CreateRunspace( initialSessionState );
            string path = Path.Combine( CosmosDataFixture.Root , @"TestData\GetrRecording.ps1" );
            runspace.Open();
            Pipeline pipeline = runspace.CreatePipeline();
            pipeline.Commands.AddScript( path );
            Collection<PSObject> results = pipeline.Invoke();
            runspace.Close();
}

我的 ps1 脚本文件中的 Powershell cmdlet

ECHO 'Y'|Import-Module "$env:ProgramFiles\Azure Cosmos DB Emulator\PSModules\Microsoft.Azure.CosmosDB.Emulator"
Install-Module PowershellGet -Force -Scope CurrentUser
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.208 -Force 
Install-Module -Name CosmosDB -Scope CurrentUser -Force

我需要第一行和最后一行,但错误提示我在 middl 中添加这两个

【问题讨论】:

    标签: c# powershell nuget


    【解决方案1】:

    正如this answer 中解释的那样,当您使用 PowerShell SDK 时,您不能使用要求用户交互式输入的 PowerShell 命令,因此您唯一的选择是 避免这样的交互式提示,根据具体的命令,需要-Force-Confirm:$false(但请注意,可能不支持两个参数)。

    • Install-PackageProvider 支持-Confirm,因此您应该尝试-Confirm:$false(可能与-Force 结合使用)看看它是否以编程方式确认了正常交互的提示。

    • Import-Module 支持-Force,但它本身不会提示用户;相反,-Force 发出强制重新加载已导入模块的意图。

      • 可能会发生什么,但是(根据您的反馈建议)正在导入的模块会尝试创建一个 提升(以管理员身份运行)进程,这总是使系统发出UAC (User Account Control)提示(“您要允许此应用对您的设备进行更改吗?”)以确认执行此安全性的意图-敏感操作。

        • 根据设计,出于安全原因,此 UAC 提示不能以编程方式回答。
      • 您唯一的选择是运行您的程序本身并使用提升,例如从通过右键单击 PowerShell 图标并选择 @987654336 启动的 PowerShell 控制台@。


    至于你尝试了什么

    注意:

    • 以下是关于以编程方式响应 PowerShell 提示的一般 点。如上所述,实际上并不是Import-Module 本身提出提示,但在您的情况下,它是正在导入的特定模块;该提示 ("Do you want this app to make changes to your computer?") 是 系统 提出的 UAC 提示,并且可以通过设计 以编程方式回答。
    ECHO 'Y' | Import-Module ...
    

    总是失败,因为您根本无法使用(PowerShell)管道输入回答 PowerShell 的交互式提示。

    • 以编程方式响应交互式 PowerShell 提示的唯一方法是调用 PowerShell CLI 并通过 stdin 提供响应 - 请参阅this answer

    大概你没有看到失败(Import-Module: The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.),因为它不是由.Invoke()返回值直接报告的(它只包含成功输出);要检查错误(错误流),请使用pipeline.Error

    【讨论】:

    • 我尝试关注您帖子中的其他答案,但不知道如何在代码中使用标准输入?你能用我的代码证明一下吗?
    • 如果我们谈论的是 UAC 提示(“您要允许此应用程序对您的设备进行更改吗?”),则没有解决方案。它是由系统而不是PowerShell显示的提示,它被设计为not允许以编程方式响应。假设您可以完全禁用 UAC,但出于安全原因强烈建议不要这样做。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-15
    • 2022-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多