【发布时间】:2023-03-11 08:45:02
【问题描述】:
我在通过 C# 向 PowerShell 传递参数时遇到问题
我收到以下异常:
"提示用户失败的命令,因为主机程序或 命令类型不支持用户交互。尝试主机程序 支持用户交互,例如 Windows PowerShell 控制台 或 Windows PowerShell ISE,并从 不支持用户交互的命令类型,例如 Windows PowerShell 工作流”
cs:
private static void RunPowershellScript(string scriptFile, string scriptParameters)
{
string scriptParameters = "param1 param2";
RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
runspace.Open();
RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
Pipeline pipeline = runspace.CreatePipeline();
Command scriptCommand = new Command(scriptFile);
Collection<CommandParameter> commandParameters = new Collection<CommandParameter>();
foreach (string scriptParameter in scriptParameters.Split(' '))
{
CommandParameter commandParm = new CommandParameter(null, scriptParameter);
commandParameters.Add(commandParm);
scriptCommand.Parameters.Add(commandParm);
}
pipeline.Commands.Add(scriptCommand);
Collection<PSObject> psObjects;
psObjects = pipeline.Invoke();
}
ps:
Function Foo ($arg1, $arg2)
{
Write-Host $arg1
Write-Host $arg2
}
Foo $args[0] $args[1]
我在这里缺少什么?我怎样才能做到这一点?
【问题讨论】:
标签: c# powershell