A
A
ASiD2020-04-07 18:24:50
PowerShell
ASiD, 2020-04-07 18:24:50

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();
        }

The data is obtained, but in the form of text. I need to break them into columns in order to process them and add them to the DataGrid. Please tell me how can this be done?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MaxKozlov, 2020-04-07
@MaxKozlov

well, first of all, I would use System.DirectoryServices, not Powershell...
But if you really want to, then PSObject has a Properties property...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question