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

 

posted @ 2013-01-23 20:31  小师傅  阅读(199)  评论(0编辑  收藏  举报