test
1、设置policy:Set-ExecutionPolicy Unrestricted
2.添加引用 :C:\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\v1.0\System.Management.Automation.dll
3.运行代码:
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
RunspaceInvoke runspaceInvoker = new RunspaceInvoke(runspace);
Pipeline pipeLine = runspace.CreatePipeline();
pipeLine.Commands.AddScript("import-module ActiveDirectory");
//pipeLine.Invoke();
不带返回结果
//pipeLine.Commands.AddScript("$a=new-aduser -name aaaaa");
带返回结果
pipeLine.Commands.AddScript("get-aduser -Identity beifeng");
pipeLine.Commands.AddScript(" (get-aduser -Identity beifeng).distinguishedname");
pipeLine.Commands.Add("Out-String");
Collection<PSObject> returnObjects = pipeLine.Invoke();
runspace.Close();
Response.Write( returnObjects);