Powershell cmdlet 开发最佳实践
我目前正在整理一些 Powershell cmdlet。 构建它们很容易,但我不知道我是否以可接受的方式构建它们(可以这么说)。
将数据传递到 Powershell 管道时是否应遵循任何指南/最佳实践? 目前,我实际上输出了 DataSet 类型的单个对象 - 如果任何 cmdlet 想要在下游使用它,那么它们必须循环遍历该 DataSet 中的 DataTable,然后循环遍历每个 DataTable 中的 DataRow。
我想问题是……我这样做会惹恼任何人吗? 或者我应该输出本质上是一堆行的数据?
预先感谢大家
-JT
I'm currently putting together some Powershell cmdlets. Building them is easy enough but I don't know if I'm building them in an acceptable manner (so to speak).
Are there any guidelines/best practices that one should follow for passing data into the Powershell pipeline? At the moment I'm actually output a single object of type DataSet - if any cmdlet wanted to use it downstream then they would have to loop over the DataTables in that DataSet, then loop over the DataRows in each DataTable.
I guess the question is....am I going to p!ss anyone off by doing this? Or should I be outputting data that is inherently a bunch of rows?
Thanks all in advance
-JT
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
输出最适合表示您正在写出的内容的任何类型的对象都是可以接受的 - DataSet 绝对没问题。 唯一潜在的警告是,PowerShell v2 可能会发现自己运行在 .NET Framework 的简化版本上(例如在 Server Core 上),因此,如果您的 cmdlet 存在这种情况,您需要谨慎行事,以确保您输出的对象存在于可能使用您的 cmdlet 的每个系统上。
尽管如此,管道在包含对象集合时效果最佳。 DataSet 本身并不是一个集合。 换句话说,您希望下游 cmdlet 能够通过管道一次接收一个对象,以便这些 cmdlet 不必手动枚举对象。 我不太清楚你在做什么 - 很可能 DataSet 是完全合适的 - 但我通常更喜欢看到内部通过 DataSet 的 cmdlet 循环,创建它自己的自定义对象(以便表中的每一列都成为一个属性),并将这些对象输出到管道。 这只会增加可以消耗您所输出内容的下游 cmdlet 的数量。
一个简单的测试是将 cmdlet 的输出通过管道传输到 Export-CSV。 如果它有效(对于数据集可能无效),那么您通常会做正确的事情。 现在,您可能需要创建一个输出数据集的 cmdlet,并且您仅打算您编写的某些其他 cmdlet(使用数据集)针对该输出进行操作。 没有错。 不过,最大的灵活性是单个对象,因为它使所有 PowerShell 的核心 cmdlet 都可以处理您的输出。
希望有帮助。
It's acceptable to output whatever type of object is best used to represent what you're writing out - a DataSet is absolutely fine. The only potential caution is that v2 of PowerShell may find itself running on a reduced version of the .NET Framework (such as on Server Core), so if that's a potential scenario for your cmdlets, you need to use some caution to make sure the object you're outputting exists on every system where your cmdlet might be used.
All that said, the pipeline works best when it contains collections of objects; a DataSet isn't a collection per se. In other words, you want downstream cmdlets to be able to receive one object at a time via the pipeline, so that those cmdlets don't have to manually enumerate through an object. I don't know a lot about exactly what you're doing - it could well be that a DataSet is entirely appropriate - but I'd generally prefer to see a cmdlet loop through the DataSet internally, create its own custom objects (so that each column in the table becomes a property), and output those objects to the pipeline. That simply increases the number of downstream cmdlets that can consume what you're putting out.
A simple test is to pipe your cmdlet's output to Export-CSV. If it works (and it probably wouldn't with a DataSet), then you're doing the right thing generally. Now, you may well need to create a cmdlet which outputs a DataSet and you only intend for certain other cmdlets you've written (which consume DataSets) to operate against that output. Nothing wrong with that. Max flexibility is single objects, though, since it enables all of PowerShell's core cmdlets to work on your output.
Hope that helps.
MSDN 有一套令人惊叹的Cmdlet 开发指南,其中我发现自己开发时非常有用。 它们分为三个不同的部分:
MSDN has an amazing set of Cmdlet Development Guidelines which I found extremely useful when developing my own. They are broken up into three different sections: