使用 Powershell 对象并运行 foreach 循环来获得结果,
我得到了 2 个正确的 IP 地址结果 144.68.205.19 和 144.68.205.22 但它打印在 1 行中,
144.68.205.19144.68.205.22
应该像这样拆分新行,
144.68.205.19
144.68.205.22
请告知,这里有一个 C# 代码,
// Powershell
Runspace runSpace = RunspaceFactory.CreateRunspace();
runSpace.Open();
Pipeline pipeline = runSpace.CreatePipeline();
Command invokeScript = new Command("Invoke-Command");
RunspaceInvoke invoke = new RunspaceInvoke();
// invoke-command -computername compName -scriptblock { get-process }
ScriptBlock sb = invoke.Invoke("{"+ PowerShellCodeBox.Text +"}")[0].BaseObject as ScriptBlock;
invokeScript.Parameters.Add("scriptBlock", sb);
invokeScript.Parameters.Add("computername", TextBoxServer.Text);
string str = "";
pipeline.Commands.Add(invokeScript);
Collection<PSObject> output = pipeline.Invoke();
foreach (PSObject psObject in output)
{
str = str + psObject;
}
if (str == ""){
str = "Error";
ResultBox.ForeColor = System.Drawing.ColorTranslator.FromHtml("#FF0000");
}
ResultBox.Text = str;