1

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.

Some team members think that the exposed functions in the module should take objects as arguments instead of 2 or 3 primitive type parameters. Others argue that if you look at Powershell's base commands, while they may return objects, they never take objects as parameters.

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 ? 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 ?

2 Answers 2

3

Others argue that if you look at Powershell's base commands, while they may return objects, they never take objects as parameters.

That is rubbish. How about Stop-Process, the first example off the top of my head? You can pass it an array of integers (process id's), an array of strings (process names), or an array of Process objects:

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. 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.

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. 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.

Sign up to request clarification or add additional context in comments.

Comments

1

I'd go with using primitive types as arguments (KISS). Internally Powershell is going to turn everything into PSObjects anyway. 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.

IMHO

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.