我尝试在下面的C#代码中执行_Get-User -Identity "John Doe" | FL_
命令。
PowerShell powershell = PowerShell.Create();
powershell.AddCommand("get-user");
powershell.AddParameter("Identity", UserName.Text);
try
{
runspace.Open();
powershell.Runspace = runspace;
Collection<PSObject> results = powershell.Invoke();
var builder = new StringBuilder();
foreach (var psObject in results)
{
builder.AppendLine(psObject.ToString() + "\r\n");
}
ResultBox.Text = Server.HtmlEncode(builder.ToString());
}
我应该在哪里添加_FL_
命令?
发布于 2011-11-09 19:00:56
我已经搜索了很多。您可以更改常规的powershell脚本,而不是"WriteWhatYouWantToDo“。
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
RunspaceInvoke invo = new RunspaceInvoke(runspace);
invo.Invoke("Set-ExecutionPolicy Unrestricted");
Pipeline pipeline = runspace.CreatePipeline();
Command command = new Command("get-module -listAvailable | import-module\n" + WriteWhatYouWantToDo);
pipeline.Commands.Add(command);
pipeline.Commands.Add("Out-String");
try
{
Collection<PSObject> results = pipeline.Invoke();
runspace.Close();
StringBuilder stringBuilder = new StringBuilder();
foreach (PSObject obj in results)
{
stringBuilder.AppendLine(obj.ToString());
}
string result1 = stringBuilder.ToString();
string result = result1.Substring(0, 250); //define global scope
}
catch (Exception ex)
{
}
发布于 2011-11-09 18:43:53
当您添加每个命令时,它会被添加到管道中。所以如果你想做format-list
,你可以把它添加到管道中:
powershell.AddCommand("get-user");
powershell.AddParameter("Identity", UserName.Text);
powershell.AddCommand("format-list");'
//powershell.AddCommand("out-string");
但是我不确定您想要做什么,因为这样的事情可以使用results
在C#中完成
https://stackoverflow.com/questions/8069676
复制相似问题