c# - PSobject 输出到控制台
问题描述
我是.net 的新手。在 c# 中调用将 powershell 命令输出到控制台时遇到问题。
代码:
PowerShell powershellCommand = PowerShell.Create();
powershellCommand.AddScript("get-process");
Collection<PSObject> results = powershellCommand.Invoke();
foreach (PSObject result in results)
{
Console.WriteLine(results);
}
Console.Read();
输出:System.Collections.ObjectModel.Collection`1[System.Management.Automation.PSObject]
解决方案
您正在迭代您的集合,但您不是在编写当前项目,而是整个集合。你必须写项目:
PowerShell powershellCommand = PowerShell.Create();
powershellCommand.AddScript("get-process");
Collection<PSObject> results = powershellCommand.Invoke();
foreach (PSObject result in results)
{
Console.WriteLine(result); //<-- result NOT results
}
Console.Read();
推荐阅读
- css - 在 IE11 中,flexbox div 不尊重容器,文本在一行中溢出,而不是换行
- hyperledger-composer - 作曲家网络部署,找不到命令
- angular - 如何仅为子模块注册 Angular HttpInterceptor
- machine-learning - SEGAN模型中用于语音增强的AEGenerator和Generator的区别
- html - 按类名的数据抓取元素
- .net - 在 Core 2.1 中发布控制台应用程序并更改 ASPNETCORE_ENVIRONMENT
- javascript - 这些条件语句有什么问题?
- c++ - 使用 boost-spirit 的函数解析器
- python - 如何遍历列表的列?
- typescript - 如何观察对调用 firestore onSnapShot 的函数所做的更改?