我通過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();
}
PS:
Function Foo ($arg1, $arg2)
{
Write-Host $arg1
Write-Host $arg2
}
Foo $args[0] $args[1]
缺少什麼我在這裏?我怎麼做這個工作?