C# 在C中转换Powershell返回值#

C# 在C中转换Powershell返回值#,c#,powershell,C#,Powershell,我想从PSObject中筛选属性,并将其转换为字符串值以供进一步使用。 在我的例子中,返回值有点复杂,不允许.ToString()转换 我有一个解决方案,但这只在这种情况下有效,如果输入值发生变化,我会得到很多错误 那么,有没有更通用的方法来获取这些值 using (PowerShell ps = PowerShell.Create()) { ps.AddCommand("get-help").AddArgument("C:\\Users\\kritzinger\\Work\\test.

我想从PSObject中筛选属性,并将其转换为字符串值以供进一步使用。 在我的例子中,返回值有点复杂,不允许.ToString()转换

我有一个解决方案,但这只在这种情况下有效,如果输入值发生变化,我会得到很多错误

那么,有没有更通用的方法来获取这些值

using (PowerShell ps = PowerShell.Create())
{
    ps.AddCommand("get-help").AddArgument("C:\\Users\\kritzinger\\Work\\test.ps1");
    Collection<PSObject> PSOutput = ps.Invoke();
    foreach (PSObject outputItem  in PSOutput)
    {
        dynamic description = outputItem.Properties["description"].Value;

        // not working
        string foo = description.ToString();
        // foo = "System.Management.Automation.PSObject[]"

        foreach (dynamic item in description)
        {
            // not working
            string bar = item.ToString();
            // bar = ""

            // working!
            string moo = item.Text;
            // moo = "here is the description of the poweshell script"
        }
    }
}
使用(PowerShell ps=PowerShell.Create())
{
ps.AddCommand(“获取帮助”).AddArgument(“C:\\Users\\kritzinger\\Work\\test.ps1”);
集合PSOutput=ps.Invoke();
foreach(PSOutput中的PSObject输出项)
{
动态描述=outputiItem.Properties[“description”].Value;
//不起作用
字符串foo=description.ToString();
//foo=“System.Management.Automation.PSObject[]”
foreach(描述中的动态项)
{
//不起作用
字符串栏=item.ToString();
//bar=“”
//工作!
字符串moo=item.Text;
//moo=“以下是对鲍威尔剧本的描述”
}
}
}
使用此

string foo = Convert.ToString(description).ToString(); 

它会成功的

抱歉,Convert.ToString结果与.ToString方法相同。Convert.ToString(description).ToString()给出一个错误,指出methode.ToString无效。