Tôi gặp vấn đề với lập luận qua để PowerShell qua C#Làm thế nào để vượt qua đối số cho PowerShell qua C#
Tôi nhận được ngoại lệ sau đây:
"Một lệnh mà nhắc người dùng không thành công vì chương trình máy chủ hoặc loại lệnh không hỗ trợ tương tác của người dùng. Hãy thử chương trình máy chủ hỗ trợ tương tác người dùng, chẳng hạn như Bảng điều khiển Windows PowerShell hoặc Windows PowerShell ISE và xóa các lệnh liên quan khỏi các loại lệnh . chẳng hạn như Windows PowerShell công việc"
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]
am i thiếu gì ở đây? làm thế nào tôi có thể thực hiện công việc này?