我需要将PowerShell PSObject转换为stream,方法与PowerShell相同。PowerShell还以通用方式提供标准流上的输出。
例如
$PSVersionTable
只输出2列(名称、值),不输出整个哈希表。
所以,我希望每个PSObject都以相同的标准方式进行处理,就像PowerShell在引擎盖下那样。此外,我想把它转换成DataTable。
我知道,还有其他的链接说明了这个问题,但是它们在PSObject的所有属性上运行。不知怎么的,PowerShell在盒子里做这件事。
示例
$PSVersionTable版本
作为
PSVersion 5.1.17134.228
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
$host
或
Get-Host
Get-WebApplication
输出
Name Application pool Protocols Physical Path
---- ---------------- --------- -------------
IisHostedServer DefaultAppPool http C:\TestDirectory\Src\IisHostedServer
Get-ChildItem -Path "C:\TestDirectory\TestSubDir\Studio"
:
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 29.06.2018 11:54 TestFileDir
d----- 24.07.2018 16:37 TestSites
-a---- 13.08.2018 11:53 343 TestXml.xml
我想在C中以同样的方式获得这些输出,并且需要将它们显示为字符串或数据表。是否有任何从PSObject到stream的转换,或者以完全通用的方式返回上面的输出?
var outputCollection = new PSDataCollection<PSObject>();
outputCollection.DataAdded += OnOutputDataAdded;
powerShell.Invoke(null, outputCollection);
private void OnOutputDataAdded(object sender, DataAddedEventArgs e)
{
var records = (PSDataCollection<PSObject>) sender;
var data = records[e.Index];
var outputTable = ConverToLogableDataStream(data);
}
private void ConverToLogableDataStream(PSObject)
{
...
...
}