Powershell 输出格式 [英] Powershell output formatting

查看:79
本文介绍了Powershell 输出格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 C# 开发 Powershell Cmdlet,我想知道是否有一种方法可以控制我在 Cmdlet 本身中写入 Powershell 的对象的格式.基本上,我有太多属性的对象无法在屏幕上轻松显示,而我的 Cmdlet 正在使用 WriteObject()

I am developing a Powershell Cmdlet using C#, and I was wondering if there's a way of controlling the formatting of the objects I write to Powershell in the Cmdlet itself. Basically, I have objects with too many properties to display easily on the screen, and my Cmdlet is sending those to Powershell with WriteObject()

我希望我的 cmdlet 用户能够运行它们,并在 Powershell 控制台中读取返回的数据,就像他们使用命令提示符所做的那样.不幸的是,Powershell 试图在列中适应的属性数量意味着大多数都被截断了,当我添加更多属性时,它会给每个属性自己的行,这更糟.

I would like users of my cmdlets to be able to run them and, in the Powershell console, read the data returned as they would have done using the command prompt. Unfortunately, the number of properties Powershell is trying to fit in columns means most are truncated, and when I add more it gives each property its own line which is worse.

我见过允许用户适当格式化数据的东西,但没有允许开发人员设置默认值的东西.基本上我想要的是一个类似于属性的东西,我可以应用到被泵送到 Powershell 的对象的每个属性,它告诉 Powershell 是否显示每个属性(假设用户没有设置其他格式选项).

I've seen things which allow the user to format the data appropriately, but nothing that allows the developer to set a default. Basically what I want is something like an Attribute I can apply to each property of the objects being pumped through to Powershell which tells Powershell whether to display each property or not (assuming the user has set no other formatting options).

(我也尝试将它们设为公共字段而不是属性,并且 Powershell 仍然显示它们)

(I've tried making them public fields instead of properties too, and Powershell still shows them)

推荐答案

我假设您使用的是 PowerShell 2.0.

I'm assuming you're using PowerShell 2.0.

如果您的模块名为 MyModule,您可以在它旁边创建一个 MyModule.Format.ps1xml 文件,以描述 Format- 默认显示的属性ListFormat-Table 等.请参阅Get-Help about_Format.ps1xml格式化文件概述了解更多信息.

If your module is called MyModule, you can create a MyModule.Format.ps1xml file alongside it to describe what properties are displayed by default by Format-List, Format-Table, etc. See Get-Help about_Format.ps1xml and Formatting File Overview for more information.

要将格式化文件链接到模块,您需要创建一个模块清单并定义FormatsToProcess 项.New-ModuleManifest cmdlet 可以帮助您开始使用.

To link your formatting file to your module, you would create a module manifest and define the FormatsToProcess item. The New-ModuleManifest cmdlet can get you started with this.

这篇关于Powershell 输出格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆