Console to pull AD info get-aduser using powershell issue

  • Thread starter Thread starter Cheesebread
  • Start date Start date
C

Cheesebread

Guest
Hi

Two years ago I wrote a very simple POC to pull AD data using Powershell using a C# console app. It works. Stripped down it looks like this

string script = "get-aduser [USER_HERE]";

string test = script;


PowerShell _powerShell;


_powerShell = PowerShell.Create().AddScript(script);


Collection<PSObject> resultsList = _powerShell.Invoke();


if(resultsList !=null)

{ [DO SOMETHING


Today I am trying to do the same using .net core in C# and it won't work. recreated the earlier one first

using (PowerShell ps = PowerShell.Create())

{

string script = "get-aduser [USER_HERE]";


ps.AddScript(script);


var pipelineObjects = ps.Invoke();


foreach(var item in pipelineObjects)

{

Console.WriteLine(item.BaseObject.ToString());

}





}

And if fails silently. Hello world seems to work though. I found this on the web

Runspace runspace = RunspaceFactory.CreateRunspace();

runspace.Open();

Pipeline pipeline = runspace.CreatePipeline();



pipeline.Commands.AddScript("get-aduser [USER_HERE]");


var results = pipeline.Invoke();


runspace.Close();

It fails but at least tells me it doesn't recognize the AD commands. In Powershell I have to import ActiveDirectory for Powershell to recognize this. How do I do this in Core? .Net framework loaded this automatically I guess. Thank you for the help.

Continue reading...
 
Back
Top