Answer the question
In order to leave comments, you need to log in
How to get data from PowerShell in C#?
Hello. I am writing an application in C#.
There is a task to receive the data on computers from the domain and to deduce them in DataGrid. I use PowerShell to get data. I run the script using the system.management.automation module.
The PS function looks like this:
private string RunPSScript(string script)
{
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(script);
pipeline.Commands.Add("Out-String");
Collection<PSObject> results = pipeline.Invoke();
runspace.Close();
StringBuilder stringBuilder = new StringBuilder();
foreach (PSObject pSObject in results)
stringBuilder.AppendLine(pSObject.ToString());
return stringBuilder.ToString();
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question