简体   繁体   English

Powershell模块/脚本是否应该接受对象作为输入?

[英]Should Powershell modules/scripts accept objects as input?

There's been this debate in my team lately. 我的团队最近发生了这场辩论。 We need to create a Powershell module defining a couple of functions that will be reused in various scripts. 我们需要创建一个Powershell模块,该模块定义几个将在各种脚本中重用的功能。

Some team members think that the exposed functions in the module should take objects as arguments instead of 2 or 3 primitive type parameters. 一些团队成员认为模块中公开的函数应将对象作为参数,而不是2或3个原始类型参数。 Others argue that if you look at Powershell's base commands, while they may return objects, they never take objects as parameters. 其他人则认为,如果您查看Powershell的基本命令,尽管它们可能返回对象,但它们从未将对象作为参数。

Considering how tedious it is to manipulate objects in Powershell compared to "real object-oriented" languages, do you think it's worth the extra cost ? 考虑到与“真正的面向对象”语言相比,在Powershell中操作对象是多么乏味 ,您是否认为值得付出额外的费用? Are there any commonly accepted best practices regarding taking objects as input ? 关于以对象作为输入,是否有任何公认的最佳实践?

Generally speaking, was Powershell ever designed with full traditional object-orientation in mind, or does its command-line nature necessarily bastardize how we write and use Powershell code ? 一般而言, Powershell是否曾经设计过完全采用传统的面向对象的设计,还是它的命令行性质必然使我们编写和使用Powershell代码的方式变得愚蠢?

Others argue that if you look at Powershell's base commands, while they may return objects, they never take objects as parameters. 其他人则认为,如果您查看Powershell的基本命令,尽管它们可能返回对象,但它们从未将对象作为参数。

That is rubbish. 那是垃圾。 How about Stop-Process , the first example off the top of my head? Stop-Process怎么样,第一个浮现在脑海的例子如何? You can pass it an array of integers (process id's), an array of strings (process names), or an array of Process objects: 您可以向其传递整数数组(进程ID),字符串数组(进程名称)或Process对象数组:

Syntax 句法

 Stop-Process [-Id] <int[]> [-PassThru ] [-Force ] [-WhatIf ] [-Confirm ] [<CommonParameters>] Stop-Process -Name <string[]> [-PassThru ] [-Force ] [-WhatIf ] [-Confirm ] [<CommonParameters>] Stop-Process [-InputObject] <Process[]> [-PassThru ] [-Force ] [-WhatIf ] [-Confirm ] [<CommonParameters>] 

Or maybe Remove-Item which takes either -Path or -LiteralPath arguments but will equally accept any objects with either Path or LiteralPath attributes. 或者也许是Remove-Item ,它-LiteralPath -Path-LiteralPath参数,但同样会接受具有PathLiteralPath属性的任何对象。 Also, as with many other cmdlets it has a -Credential argument that is a pscredential object and can be specified either on the command line or as a property on pipeline objects. 此外,与许多其他cmdlet一样,它具有-Credential参数,它是一个pscredential对象,可以在命令行上指定,也可以在管道对象上指定为属性。

Follow some of these standard cmdlets as your model: the primary argument for a cmdlet is usually an array of some primitive, but can also be an attribute on appropriate objects in the pipeline, or an array of objects. 遵循这些标准cmdlet中的一些作为模型:cmdlet的主要参数通常是一些原始数组,但也可以是管道中适当对象的属性,也可以是对象数组。 Where there is an appropriate object accept it as one of the parameter sets but try to have other parameter sets that allow the command to be run on its own. 如果有适当的对象,则将其接受为参数集之一,但尝试使用其他参数集以允许命令单独运行。

Other parameters ( -Credential being the most obvious example) do just fine as objects. 其他参数( -Credential是最明显的示例)可以很好地用作对象。

I'd go with using primitive types as arguments (KISS). 我会使用原始类型作为参数(KISS)。 Internally Powershell is going to turn everything into PSObjects anyway. 无论如何,Powershell内部都会将所有内容转换为PSObjects。 Doesn't matter if you give it apples or oranges, it's going to be fruit salad. 不管你给它苹果还是桔子,它都会是水果沙拉。

Beyond that, as cloud computing becomes more prevalent and remote management the norm, it becomes more likely that whatever you use is going to end up getting serialized and sent down the wire. 除此之外,随着云计算变得越来越普遍并且远程管理成为规范,您使用的任何东西都更有可能最终被串行化并直接发送出去。 Primitive types generally serialize more efficiently and predictably, and result in much smaller payloads than dotnet objects. 基本类型通常更有效和可预测地进行序列化,从而产生的负载比dotnet对象小得多。

IMHO 恕我直言

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

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