如何通过 C# 将参数传递给 PowerShell [英] How to pass arguments to PowerShell via C#

查看:49
本文介绍了如何通过 C# 将参数传递给 PowerShell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在通过 C# 向 PowerShell 传递参数时遇到问题

I having issues with passing arguments to PowerShell via C#

我收到以下异常:

"提示用户失败的命令,原因是宿主程序或命令类型不支持用户交互.尝试主机程序支持用户交互,例如 Windows PowerShell 控制台或 Windows PowerShell ISE,并从不支持用户交互的命令类型,例如 WindowsPowerShell 工作流"​​

"A command that prompts the user failed because the host program or the command type does not support user interaction. Try a host program that supports user interaction, such as the Windows PowerShell Console or Windows PowerShell ISE, and remove prompt-related commands from command types that do not support user interaction, such as Windows PowerShell workflows"

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]

我在这里错过了什么?我怎样才能做到这一点?

What am i missing here? how can i make this work?

推荐答案

例外与参数无关.要么不使用需要实现主机 UI(包括Write-Host)的命令,要么实现你自己的自定义主机(PSHost)和这个 UI(PSHostUserInterface>).这是一个简单主机的示例(如果您选择这种方式,MSDN 上还有更多相关内容):http://msdn.microsoft.com/en-us/library/windows/desktop/ee706559(v=vs.85).aspx

The exception is not about arguments. Either do not use commands that require host UI implemented (Write-Host included) or implement you own custom host (PSHost) and this UI (PSHostUserInterface). Here is the example of a simple host (and there is much more on MSDN about this, if you choose this way): http://msdn.microsoft.com/en-us/library/windows/desktop/ee706559(v=vs.85).aspx

对于简单的任务,实现一个带有 UI 的主机可能太多了.您可以考虑简单地定义一个具有相同参数的函数 Write-Host 并实现它,以便它在您的特定情况下工作(例如,[Console]::WriteLine(...)).这个函数应该在脚本中定义,或者更好地以不同的方式提供给它,例如调用另一个在全局范围内定义的脚本.

For simple tasks implementing a host with UI is too much, perhaps. You may consider simply to define a function Write-Host with the same arguments and implement it so that it works in your specific case (e.g. does [Console]::WriteLine(...)). This function should be defined in the script or better made available for it in a different way, e.g. invoke another script with it defined in the global scope.

附言如果您有自定义主机,则使用 CreateRunspace() 重载之一,该重载将主机实例作为参数以链接主机和运行空间.

P.S. And if you have your custom host then use one of the CreateRunspace() overloads that takes a host instance as an argument in order to link the host and the runspace.

这篇关于如何通过 C# 将参数传递给 PowerShell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆