По сути, моя цель - зафиксировать ход выполнения скрипта Powershell в моем экземпляре C #. Я нашел этот ответ , который связан, но он не работает (ничего в консоли вывода). Вот моя реализация, использующая предложение в принятом ответе.
Test.ps1
Write-Progress -Activity "Finding user" -CurrentOperation "TEST1" -PercentComplete 1
#Some task
Write-Progress -Activity "Finding user" -CurrentOperation "TEST2" -PercentComplete 2
#Some other task
Write-Progress -Activity "Finding user" -CurrentOperation "TEST3" -PercentComplete 3
#Some other task
...
TEST.cs
//creating PS instance
PowerShell ps = PowerShell.Create();
//creating a runspace
Runspace runspace = RunspaceFactory.CreateRunspace();
//Associating the runspace
ps.Runspace = runspace;
//creating the pipeline
Pipeline pipeline = ps.Runspace.CreatePipeline();
ps.Runspace.Open();
//Feeding the script to the pipeline
Command myCommand = new Command("TEST.ps1");
pipeline.Commands.Add(myCommand);
ps.Streams.Progress.DataAdded += (sender, eventargs) => {
PSDataCollection<ProgressRecord> progressRecords = (PSDataCollection<ProgressRecord>)sender;
System.Diagnostics.Debug.WriteLine("Progress is {0} percent complete", progressRecords[eventargs.Index].PercentComplete);
};
// execute the script
results = pipeline.Invoke();
У меня нет проблем с успешным запуском скрипта, но событие, связанное с прогрессом, похоже, никогда не сработает. Я что-то упустил?