VMware Cloud Community
evilensky
Enthusiast
Enthusiast
Jump to solution

How to accept either VM Name or VM Object on the same paramenter

I stole some code from LucD​ from 2012 which took a Folder object, and added a second parameter to accept a VM object and then get its folder.

Much as PowerCLI cmdlets do, I would like my function to also accept a string or wildcard for the -VM parameter, on which I'd simply do a get-VM to retrieve the VM object.

What is the laziest way to accomplish this? Can I add a 3rd mutually exclusive parameter set for a [String] param with the same name as -VM ?

    [CmdletBinding(DefaultParameterSetName='Folder')]

    

        param(

        [parameter(ParameterSetName='Folder', valuefrompipeline = $true,

        position = 0,

        HelpMessage = "Enter a folder")]

        [VMware.VimAutomation.ViCore.Impl.V1.Inventory.FolderImpl[]]$Folder,

        #[switch]$ShowHidden = $false,

       

        [parameter(ParameterSetName='VM', valuefrompipeline = $true,

        position = 0,

        HelpMessage = "Enter a VM")]

        [VMware.VimAutomation.ViCore.Impl.V1.VM.UniversalVirtualMachineImpl[]]$VirtualMachine

        #[switch]$ShowHidden = $false

        )

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Yes, OBN is a PowerCLI feature, not a PS feature.

A parameter alias can give another name to a parameter, not a different type.

The 1st solution in my post, with PSObject, is the one that I used quite often and is probably the simplest.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

3 Replies
LucD
Leadership
Leadership
Jump to solution

When you are referring OBN, you might want to have a look at my Home Made OBN post.

It describes my cheap method wby using PSObject, but also my MyOBN class solution.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
evilensky
Enthusiast
Enthusiast
Jump to solution

Wow, that is complex very quickly. Is OBN a PowerCLI-specific feature and not native to PS5?  I was naively hoping it was as simple as using aliases but for differing object types.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Yes, OBN is a PowerCLI feature, not a PS feature.

A parameter alias can give another name to a parameter, not a different type.

The 1st solution in my post, with PSObject, is the one that I used quite often and is probably the simplest.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference