“显示”;“测试网络连接”;Winforms文本框中的Powershell命令

“显示”;“测试网络连接”;Winforms文本框中的Powershell命令,winforms,powershell,Winforms,Powershell,我试图在Winforms文本框中显示Powershell命令“Test NetConnection”的结果 using (PowerShell ps = PowerShell.Create()) { ps.Commands.AddScript("Test-NetConnection"); Collection<PSObject> result = ps.Invoke();

我试图在Winforms文本框中显示Powershell命令“Test NetConnection”的结果

        using (PowerShell ps = PowerShell.Create())
        {
            ps.Commands.AddScript("Test-NetConnection");
            Collection<PSObject> result = ps.Invoke();
            foreach (var outputObject in result)
            {
                connectionStatus.Text += outputObject.ToString();
            }
        }
使用(PowerShell ps=PowerShell.Create())
{
ps.Commands.AddScript(“测试网络连接”);
收集结果=ps.Invoke();
foreach(结果中的var outputObject)
{
connectionStatus.Text+=outputObject.ToString();
}
}

当我在Powershell中运行“Test NetConnection”时,它会工作,如果我关闭ps.Commands.AddScript(“Test NetConnection”);对于ps.Commands.AddScript(“ipconfig/all”);它可以工作,但由于某些原因,使用“Test NetConnection”命令不会显示任何内容。我做错了什么?

试试这样的方法:

 using (PowerShell ps = PowerShell.Create())
            {
                ps.Commands.AddScript("Test-NetConnection");
                Collection<PSObject> result = ps.Invoke();

                int i = 0;
                foreach (var outputObject in result)
                {
                    connectionStatus.Text += string.Format("===>Row N°{0} :\r\nComputerName : {1}\r\nRemoteAddress : {2}\r\nInterfaceAlias : {3}\r\nSourceAddress : {4}\r\nPingSucceeded:{5}\r\n",
                                                                i++,
                                                                outputObject.Properties["ComputerName"].Value,
                                                                outputObject.Properties["RemoteAddress"].Value,
                                                                outputObject.Properties["InterfaceAlias"].Value,
                                                                outputObject.Properties["SourceAddress"].Value,
                                                                outputObject.Properties["PingSucceeded"].Value
                                                            );


                       
                }
            }
使用(PowerShell ps=PowerShell.Create())
{
ps.Commands.AddScript(“测试网络连接”);
收集结果=ps.Invoke();
int i=0;
foreach(结果中的var outputObject)
{
connectionStatus.Text+=string.Format(“==>行N°{0}:\r\N计算机名:{1}\r\N远程地址:{2}\r\N接口别名:{3}\r\N源地址:{4}\r\N成功的:{5}\r\N”,
i++,
outputObject.Properties[“ComputerName”]。值,
outputObject.Properties[“RemoteAddress”]。值,
outputObject.Properties[“InterfaceAlias”]值,
outputObject.Properties[“SourceAddress”].Value,
outputObject.Properties[“pingSuccessed”].Value
);
}
}

您的连接状态是一个文本框?您是否已将属性multiline to your textbox设置为true?是的,我已将multiline设置为true,但它仍然不起作用