Step1: Add System.Management.Automation.dll to project.
this is normaly not available so download it.
public string RunPowerShell()
{
Runspace runspace = RunspaceFactory.CreateRunspace();
//Opening Powershell RunSpace
runspace.Open();
//Opening pipeline to pass the powershell script
Pipeline pipeline = runspace.CreatePipeline();
//sending commands
pipeline.Commands.AddScript(@"Get-ADUser -Filter * -Properties displayName,mail,samaccountname,title,department,company,l,co,mobile,facsimileTelephoneNumber,manager,extensionAttribute2,extensionAttribute4,EmployeeType,canonicalName,enabled | select displayName,mail,samaccountname,title,department,company,l ,co,mobile,facsimileTelephoneNumber,manager,extensionAttribute2,extensionAttribute4,EmployeeType,canonicalName,enabled ");
//ending the script
pipeline.Commands.Add("Out-String");
//Creating PoweShell Object for result
Collection<PSObject> results = pipeline.Invoke();
//Closing Powershell RunSpace
runspace.Close();
StringBuilder stringBuilder = new StringBuilder();
foreach (PSObject obj in results)
{
stringBuilder.AppendLine(obj.ToString());
}
return stringBuilder.ToString();
}
No comments:
Post a Comment