简体   繁体   English

从 c# 调用 Powershell 漂亮的控制台 Output

[英]Calling Powershell Pretty Console Output from c#

Powershell has some pretty nifty formating modules for displaying objects, arrays, collections, dictionaries, and tabular data. Powershell 有一些非常漂亮的格式化模块用于显示对象、arrays、collections、字典和表格数据。

Since Powershell is all .NET, I'm assuming there is a Assembly containing the logic for this.由于 Powershell 都是 .NET,我假设有一个包含此逻辑的程序集。

Who can come up with a way to call these powershell formating from .NET:谁能想出一种方法来调用这些 powershell 从 .NET 格式化:

I'd imagine something like this:我会想象这样的事情:

Console.WriteLine(Powershell.DisplayObject(obj))

This is how you do it in V2:这就是您在 V2 中的操作方式:

PowerShell.Create().AddScript("get-Process |Out-String").Invoke()

The key thing is to call OUT-STRING.关键是调用 OUT-STRING。

Experiment!实验! Enjoy!享受! Engage!从事!

Jeffrey Snover [MSFT] Windows Management Partner Architect Jeffrey Snover [MSFT] Windows 管理合伙人架构师

you might want to control the width of the output with |您可能想要控制 output 的宽度 | out-string -width 120 for instance.例如,out-string -width 120。

This might not be where you are going here but check out the Extended Type System.这可能不是您要去的地方,但请查看扩展类型系统。

Here's a good spot to start: msdn PS blog blog这是一个很好的起点: msdn PS blog blog

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM