相关疑难解决方法(0)

如何通过C#将参数传递给PowerShell

我有问题通过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();
}
Run Code Online (Sandbox Code Playgroud)

PS:

Function Foo ($arg1, $arg2)
{
    Write-Host $arg1 …
Run Code Online (Sandbox Code Playgroud)

c# powershell

6
推荐指数
1
解决办法
4555
查看次数

标签 统计

c# ×1

powershell ×1