我从下面的 C# 方法调用 powershell cmdlet,它在 Install-Module Name CosmosDb 行失败,并出现以下错误
“使用“2”个参数调用“ShouldContinue”时出现异常:“一个命令
提示用户失败,因为主机程序或命令
类型不支持用户交互。楼主试图
使用以下消息请求确认:PowerShellGet
需要 NuGet 提供程序版本“2.8.5.201”或更高版本才能与之交互
基于 NuGet 的存储库。 NuGet 提供程序必须可用
'C:\Program Files\PackageManagement\ProviderAssemblies' 或
'C:\Users\用户名\AppData\Local\PackageManagement\ProviderAssemblies'。
您还可以通过运行来安装 NuGet 提供程序
'Install-PackageProvider -名称 NuGet -MinimumVersion 2.8.5.201
-力量'。您希望 PowerShellGet 现在安装并导入 NuGet 提供程序吗?”
C# Code:
{
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 中添加这两行
正如中所解释的这个答案 https://stackoverflow.com/a/68179928/45375, 您不能使用请求的 PowerShell 命令交互的使用 PowerShell SDK 时来自用户的输入,所以你唯一的选择是avoid这种交互式提示,根据具体命令,需要-Force
or -Confirm:$false
(尽管请注意neither可能支持参数)。
-
Install-PackageProvider https://learn.microsoft.com/powershell/module/packagemanagement/install-packageprovider支持-Confirm
,所以你应该尝试-Confirm:$false
(可能与-Force
)以查看是否以编程方式确认正常交互式提示。
-
Import-Module https://learn.microsoft.com/powershell/module/microsoft.powershell.core/import-module支持-Force
,但事实并非如此itself永远提示用户;反而-Force
表示强制重新加载已导入模块的意图。
-
What can然而,发生的情况(根据您的反馈建议)是正在导入的模块尝试创建一个elevated(以管理员身份运行)进程,这总是使the system放一个UAC(用户帐户控制) https://en.wikipedia.org/wiki/User_Account_Control prompt(“您想要允许此应用程序对您的设备进行更改吗?”)以确认执行此安全敏感操作的意图。
- 根据设计,此 UAC 提示可以not得到答复以编程方式,出于安全原因。
-
你唯一的选择是运行你的程序itself有海拔,例如从通过右键单击 PowerShell 图标并选择启动的 PowerShell 控制台Run as Administrator
.
As for 你尝试过什么:
Note:
- 下面做了一个general关于以编程方式响应的要点电源外壳提示。如上所述,实际上并非如此
Import-Module
它本身会发出提示,但在您的情况下,它是正在导入的特定模块;该提示("Do you want this app to make changes to your computer?"
) 是由system并且可以通过设计not以编程方式回答。
ECHO 'Y' | Import-Module ...
总是fails, 因为您根本无法使用 (PowerShell) 回答 PowerShell 的交互式提示pipeline input.
- 以编程方式响应交互式的唯一方法电源外壳提示是如果PowerShellCLI被调用并通过提供响应stdin - see 这个答案 https://stackoverflow.com/a/66550241/45375.
想必你没有看到失败(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()
返回值(仅包含success输出);检查errors(错误流),使用pipeline.Error https://learn.microsoft.com/en-US/dotnet/api/System.Management.Automation.Runspaces.Pipeline.Error
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)