Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C中执行Powershell命令(获取用户标识“John Doe”FL)#_C#_Powershell - Fatal编程技术网

C# 在C中执行Powershell命令(获取用户标识“John Doe”FL)#

C# 在C中执行Powershell命令(获取用户标识“John Doe”FL)#,c#,powershell,C#,Powershell,我试图在下面的C代码中执行\u Get-User-Identity“John Doe”|FL命令 PowerShell PowerShell=PowerShell.Create(); powershell.AddCommand(“获取用户”); powershell.AddParameter(“标识”,UserName.Text); 尝试 { Open(); powershell.Runspace=运行空间; 收集结果=powershell.Invoke(); var builder=新的Str

我试图在下面的C代码中执行
\u Get-User-Identity“John Doe”|FL
命令

PowerShell PowerShell=PowerShell.Create();
powershell.AddCommand(“获取用户”);
powershell.AddParameter(“标识”,UserName.Text);
尝试
{
Open();
powershell.Runspace=运行空间;
收集结果=powershell.Invoke();
var builder=新的StringBuilder();
foreach(结果中的var psObject)
{
builder.AppendLine(psObject.ToString()+“\r\n”);
}
ResultBox.Text=Server.HtmlEncode(builder.ToString());
}

在哪里添加
\u FL\u
命令

添加每个命令时,都会将其添加到管道中。因此,如果要执行
格式列表
,可以将其添加到管道中:

   powershell.AddCommand("get-user");
   powershell.AddParameter("Identity", UserName.Text);
   powershell.AddCommand("format-list");'
   //powershell.AddCommand("out-string");

但是我不确定你想做什么,因为类似的事情可以用C语言完成,使用
结果

我搜索了很多。您可以更改常规powershell脚本,而不是“WriteWhatYouWantToDo”

Runspace Runspace=RunspaceFactory.CreateRunspace();
Open();
RunspaceInvoke invo=新的RunspaceInvoke(runspace);
invo.Invoke(“设置不受限制的执行策略”);
Pipeline Pipeline=runspace.CreatePipeline();
Command Command=new命令(“获取模块-列表可用|导入模块\n”+WRITEWATYOUWANTODO);
pipeline.Commands.Add(命令);
pipeline.Commands.Add(“输出字符串”);
尝试
{
收集结果=pipeline.Invoke();
runspace.Close();
StringBuilder StringBuilder=新的StringBuilder();
foreach(结果中的PSObject对象)
{
stringBuilder.AppendLine(obj.ToString());
}
string result1=stringBuilder.ToString();
string result=result1.Substring(0250);//定义全局范围
}
捕获(例外情况除外)
{
}

这不就是将
结果
传递给“FL”(一个不同的Powershell命令)吗?或者您可以使用完整的命令字符串?我认为它不会很好地工作,但已经很接近了。我会在这些命令中再添加一个:
powershell.AddCommand(“Out String”)。cmdlet
format list
不返回格式化文本,而是返回用于在实际输出时格式化的对象。我们必须触发这个输出。@RomanKuzmin-是的,但这就是为什么我在最后一行质疑为什么OP首先需要fl。
   powershell.AddCommand("get-user");
   powershell.AddParameter("Identity", UserName.Text);
   powershell.AddCommand("format-list");'
   //powershell.AddCommand("out-string");
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)
{

}