Quantcast
Channel: VMware Communities : All Content - All Communities
Viewing all articles
Browse latest Browse all 183671

How to create a function parameter like VM in Get-VMHost?

$
0
0

I am trying to create a PowerShell advanced function that has a VM parameter that behaves like the VM parameter of the PowerCLI Get-VMHost cmdlet. The Get-VMHost VM parameter is of type VMware.VimAutomation.ViCore.Types.V1.Inventory.VirtualMachine[]. You can see this if you execute the following commands in a PowerCLI session:

 

(Get-Command Get-VMHost).Parameters.VM


However when using this cmdlet you can also specify a string for the VM parameter value. Like:

 

Get-VMHost -VM "MyVM"


If I make the following PowerShell advanced function that has a VM parameter of type VMware.VimAutomation.ViCore.Types.V1.Inventory.VirtualMachine[]:

 

functionTest-Parameter {  [cmdletbinding()]  param([VMware.VimAutomation.ViCore.Types.V1.Inventory.VirtualMachine[]]$VM)    $VM
}

 

and I pass a string as value:

Test-Parameter -VM "MyVM"

 

I get the following error message:

 

Test-Parameter : Cannot process argument transformation on parameter 'VM'. Cannot convert the "MyVM" value of type "System.String" to type "VMware.VimAutomation.ViCore.Types.V1.Inventory.VirtualMachine[]".
At line:1 char:20
+ Test-Parameter -VM "MyVM"
+                    ~~~~~~
    + CategoryInfo          : InvalidData: (:) [Test-Parameter], ParameterBindingArgumentTransformationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Test-Parameter


Does anyone know how to replicate the behaviour of the Get-VMHost VM parameter?

 

I know that I can use PSObject as the type and do type checking in my function. But that is not what I want. I want it exactly as Get-VMHost does.

 

Regards, Robert


Viewing all articles
Browse latest Browse all 183671

Trending Articles



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