PowerShell无法找到类型[VMware.VimAutomation.Vds.Impl.V1.VmwareVDPortgroupImpl]

时间:2017-07-03 09:26:11

标签: powershell exception powercli

目前,我使用VMware的PowerCLI Api遇到了一个奇怪的问题。 我编写了一个需要VMware.VimAutomation.Vds.Impl.V1.VmwareVDPortgroupImpl Object作为参数的函数,但每次我提供这样的对象时都会出错。 在调试时,我发现甚至没有将此Object分配给另一个具有相同类型的变量。

PS vi:\IX-eShelter> [VMware.VimAutomation.Vds.Impl.V1.VmwareVDPortgroupImpl]$t = Get-VDPortgroup -Name "dv-test-vlan" | Get-Member
Unable to find type [VMware.VimAutomation.Vds.Impl.V1.VmwareVDPortgroupImpl].
At line:1 char:1
+ [VMware.VimAutomation.Vds.Impl.V1.VmwareVDPortgroupImpl]$t = Get-VDPo ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (VMware.VimAutom...VDPortgroupImpl:TypeName) [], RuntimeExc
   eption
    + FullyQualifiedErrorId : TypeNotFound

我已经尝试了以下内容:

[System.Reflection.Assembly]::LoadWithPartialName("VMware.VimAutomation.Vds.Impl.V1.VmwareVDPortgroupImpl")
Add-Type -AssemblyName "VMware.VimAutomation.Vds.Impl"

编辑: 我的职责:

function New-VMDeployment {Param([Parameter(Mandatory=$true,ValueFromPipeline=$false,ValueFromPipelineByPropertyName=$true)][VMware.VimAutomation.Vds.Impl.V1.VmwareVDPortgroupImpl]$port)
Write-Host $port;
};

我这样称呼它:

$deployment = [PSCustomObject]@{ "port" = Get-VDPortgroup -Name "dv-test-vlan";};
$deployment | New-VMDeployment;

1 个答案:

答案 0 :(得分:0)

看起来分布式端口组的实现(Impl)类型已经破坏。您应该可以切换到Types类型并找到它可操作。

我建议将函数修改为:

function New-VMDeployment {
    param(
        [Parameter(
            Position=0,
            Mandatory=$true,
            ValueFromPipeline=$false,
            ValueFromPipelineByPropertyName=$true)
        ]
        [VMware.VimAutomation.Vds.Types.V1.VmwareVDPortgroup]$port
    )

    Write-Host $port
}

其他信息:https://blogs.vmware.com/PowerCLI/2016/04/powercli-best-practice-correct-use-strong-typing.html