Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

CmdLet design advice - Multiple mutually exclusive parameters

$
0
0

Hi everyone, I'm looking for some design advice. I'm building a set of Powershell cmdlets for our product and I want to make sure the fit into the default idioms so that they are easy to use by admins with powershell experience.

The bit I'm thrashing on at the moment is how to link objects together, say I have a 'User' object and a 'Department' object in my system, I want to be able to build something like this

$dept = Get-Department -Name "Support"

New-User -Name "Stephen" -Department $dept

OK, that's nice and simple, but I also want to be able to specify the department direct in the New-User parameter such as

New-User -Name "Fred" -Department "Sales"

Or if I have some sort of automation generating the script I might want to specify the ID directly, something like

New-User -Name "Ian" -Department "7973D1B6-29B7-454A-AA53-60B2CB12F6A1"

As some of these are the same types (strings) and others are some other type,  other than setting the param type to object and inspecting what I've been passed (not very user friendly) I end up with three parameters on my cmdlet Department,DepartmentName,DepartmentId where only one can be used at a time, that's quite easy I can use parameter sets to ensure we only set one of those.

The problem comes on more complex objects that might have 10 or so of these external links.  Trying to build a matrix of parameter sets is not feasible at that point.

At the moment I've gone for three parameters, and sort out which one to use in code, but that means I have to set the all to Mandatory=false and that really spoils the consumers experience as I have to write-error about required parameters which I don't like.

So I was wondering what to do, and have been on and of for a week now, with no decision in sight :)

At the moment I'm leaning towards something along the lines of making all the objects that are outputted to the pipeline castable to some sort of interface like IMyObjectIdentity and then I could do something like, which would avoid the trip to the server to get the department (not an issue but when adding 10,000 items with 10 links each, it makes a difference)**  

$dept = New-ObjectIdentity -ID  "7973D1B6-29B7-454A-AA53-60B2CB12F6A1" -Type Department

New-User -Name "Kim" -Department $dept

I'm starting to ramble now, so I'll stop, but would love to hear any comments.

Thanks,

Stephen.

** One of the things we use these for is data import and it can result in 50,000 objects being imported, and avoiding the server roundtrips makes the process about 4 times faster in our tests, which matters in an already lengthy process.


Viewing all articles
Browse latest Browse all 21975


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>