Determine Power-CLI Object-Type

Background

If you needed to pass some „Objects“ to a Power-Shell function you need the name of their Types.

Example

Four Parameters need to be passed to the function:

  • two Objects
    1. VI-Server
    2. Virtual-Machine
  • two Strings:
    1. diskName
    2. diskSize
function resizeVMDisk { Param([VMware.VimAutomation.ViCore.Types.V1.VIServer]$server, [VMware.VimAutomation.ViCore.Types.V1.Inventory.VirtualMachine]$vm, [string]$diskName, [string]$diskSize)

Algorithm

  1. Find an existing PowerCLI-Function which already uses the Object-Parameter you need.

In my case:

Get-VMHost

2) Pull the MetaData of this Command.

3) Display the „Attributes“ of the relevant Object-Parameter

4) Look for „Target Type List“

Result

„VM“ = [VMware.VimAutomation.ViCore.Types.V1.Inventory.VirtualMachine]

„Server“ = [VMware.VimAutomation.ViCore.Types.V1.VIServer]

Example

C:\LAB> $cmd = Get-Command -Module VMware.VimAutomation.Core -Name Get-VMHost
C:\LAB> $meta = New-Object System.Management.Automation.CommandMetadata ($cmd)
C:\LAB> $meta

Name                    : Get-VMHost
CommandType             : VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVMHost
DefaultParameterSetName : Default
SupportsShouldProcess   : False
SupportsPaging          : False
PositionalBinding       : True
SupportsTransactions    : False
HelpUri                 :
RemotingCapability      : PowerShell
ConfirmImpact           : None
Parameters              : {[Name, System.Management.Automation.ParameterMetadata], [NoRecursion, System.Management.Automation.ParameterMetadata], [VM,
                          System.Management.Automation.ParameterMetadata], [ResourcePool, System.Management.Automation.ParameterMetadata]…}


C:\LAB> $meta.Parameters["VM"].Attributes

Position                        : -2147483648
ParameterSetName                : SecondaryParameterSet
ValueFromPipeline               : True
Mandatory                       : False
ExperimentName                  :
ExperimentAction                : None
ValueFromPipelineByPropertyName : False
ValueFromRemainingArguments     : False
HelpMessage                     :
HelpMessageBaseName             :
HelpMessageResourceId           :
DontShow                        : False
TypeId                          : System.Management.Automation.ParameterAttribute

TypeId : System.Management.Automation.ValidateNotNullOrEmptyAttribute

Critical                        : True
ContextPrincipalName            :
ContextOptional                 : False
ContextRelationName             :
TargetTypeList                  : {VMware.VimAutomation.ViCore.Types.V1.Inventory.VirtualMachine}
TransformNullOptionalParameters : True
TypeId                          : VMware.VimAutomation.Sdk.Util10Ps.BaseCmdlet.ObnArgumentTransformationAttribute


C:\LAB> $meta.Parameters["Server"].Attributes

Position                        : -2147483648
ParameterSetName                : Default
ValueFromPipeline               : False
Mandatory                       : False
ExperimentName                  :
ExperimentAction                : None
ValueFromPipelineByPropertyName : False
ValueFromRemainingArguments     : False
HelpMessage                     :
HelpMessageBaseName             :
HelpMessageResourceId           :
DontShow                        : False
TypeId                          : System.Management.Automation.ParameterAttribute

Position                        : -2147483648
ParameterSetName                : ById
ValueFromPipeline               : False
Mandatory                       : False
ExperimentName                  :
ExperimentAction                : None
ValueFromPipelineByPropertyName : False
ValueFromRemainingArguments     : False
HelpMessage                     :
HelpMessageBaseName             :
HelpMessageResourceId           :
DontShow                        : False
TypeId                          : System.Management.Automation.ParameterAttribute

Position                        : -2147483648
ParameterSetName                : DistributedSwitch
ValueFromPipeline               : False
Mandatory                       : False
ExperimentName                  :
ExperimentAction                : None
ValueFromPipelineByPropertyName : False
ValueFromRemainingArguments     : False
HelpMessage                     :
HelpMessageBaseName             :
HelpMessageResourceId           :
DontShow                        : False
TypeId                          : System.Management.Automation.ParameterAttribute

Position                        : -2147483648
ParameterSetName                : SecondaryParameterSet
ValueFromPipeline               : False
Mandatory                       : False
ExperimentName                  :
ExperimentAction                : None
ValueFromPipelineByPropertyName : False
ValueFromRemainingArguments     : False
HelpMessage                     :
HelpMessageBaseName             :
HelpMessageResourceId           :
DontShow                        : False
TypeId                          : System.Management.Automation.ParameterAttribute

Critical                        : True
ContextPrincipalName            :
ContextOptional                 : False
ContextRelationName             :
TargetTypeList                  : {VMware.VimAutomation.ViCore.Types.V1.VIServer}
TransformNullOptionalParameters : True
TypeId                          : VMware.VimAutomation.Sdk.Util10Ps.BaseCmdlet.ObnArgumentTransformationAttribute

Schreibe einen Kommentar