模块 cmdlet 上的 ValueFromPipelineByPropertyName 失败

ValueFromPipelineByPropertyName failing on module cmdlet

我有一个名为 Get-Organization 的 cmdlet,其中 return 如下 return 类型

public class OrgModel
{
    public string OrgName {get;set;}
}

[Cmdlet(VerbsCommon.Get, "Organization")]
[OutputType(typeof(OrgModel))]
 public GetOrganizationCmdlet : PSCmdlet
    {

        [Alias("OrgName")]
        [Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, Position = 0, HelpMessage = "The orgname.")]
        string Name{get;set}

        ...
    }

我有另一个名为 Department 的 cmdlet,它 return 是一个模型 DepartmentModelGet-Department -OrgName <somename> returns Orgname 里面的所有部门。 cmdlet 定义如下。

[Cmdlet(VerbsCommon.Get, "Department")]
public GetDepartmentCmdlet : PSCmdlet
{
    [Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, Position = 0, HelpMessage = "The org name.")]
        [ValidateNotNullOrEmpty]
        string OrgName {get;set}

    [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 1, HelpMessage = "Optional. The department name.")]
        string Name{get;set}

    ...
}

加载模块后,一切正常。它破裂的地方是管道。以下return是一个错误

Get-Organization -Name <somename> | Get-Department

如您所见,return 类型 OrgModel 有一个名为 OrgName 的 属性 定义,它应该自动绑定到 Get-Department 参数 OrgName 但它不是,并给出以下错误:

C:\WINDOWS\system32> Get-Organization -Name contoso | Get-Department
Get-Department : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.
At line:1 char:44
+ ... et-Organization -Name contoso | Get-Department
+                                               ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (OrgModel:PSObject) [Get-Department], ParameterBindingException
    + FullyQualifiedErrorId : InputObjectNotBound,GetDepartment

有什么想法吗?

使用@PeterSerAl 提到的命令我注意到 return 时间被错误地设置为一个集合。

修复后有效。

谢谢