Я пытаюсь запустить сценарий PowerShell из C #. У меня нет проблем с передачей строк в сценарий, однако, когда я пытаюсь передать массив в сценарий PowerShell, возникает исключение. Вот код C #:
string [] test = {"1","2","3","4"};
RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
runspace.ApartmentState = System.Threading.ApartmentState.STA;
runspace.ThreadOptions = PSThreadOptions.UseCurrentThread;
runspace.Open();
RunspaceInvoke invoker = new RunspaceInvoke();
invoker.Invoke("Set-ExecutionPolicy Unrestricted");
Pipeline pipeline = runspace.CreatePipeline();
Command myCmd = new Command(@"C:\test.ps1");
CommandParameter param = new CommandParameter("responseCollection", test);
myCmd.Parameters.Add(param);
pipeline.Commands.Add(myCmd);
// Execute PowerShell script
Collection<PSObject> results = pipeline.Invoke();
Вот сценарий PowerShell:
param([string[]] $reponseCollection)
$a = $responseCollection[0]
Каждые когда этот код выполняет, он выдает:
Cannot index into a null array.
Я знаю, что код для выполнения сценария PowerShell верен при передаче строк в сценарий PowerShell, он был тщательно протестирован.