首页 > 解决方案 > C# Powershell 脚本未写入 Visual Studio 中的输出

问题描述

我有一个简单的 WPF 表单,我希望将某个 powershell 脚本的输出写入 Visual Studio 输出。但是,我在 Visual Studio 中没有得到正确的输出。

电源外壳:

nmap -O 198.168.1.1/24

我现在有一个只有一个按钮的 wpf 表单,代码如下:

MainWindow.xaml.cs:

    private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
    {

    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Debug.WriteLine("I am Here!");
        PowerShell ps = PowerShell.Create();
        ps.AddScript(@"C:\Research\Network Scanner\powerscript.ps1");

        Collection<PSObject> resultsN = null;

        resultsN = ps.Invoke();

        Debug.WriteLine(resultsN);

        // ps.Invoke();
    }
}

}

输出:

我在这里!

线程 0x4378 已退出,代码为 0 (0x0)。System.Collections.ObjectModel.Collection`1[System.Management.Automation.PSObject]

标签: c#powershell

解决方案


这会有所帮助。foreach(结果中的 PSObject obj) { Debug.WriteLine(obj.ToString()); }


推荐阅读