ValueFromPipelineByPropertyName在模块cmdlet上失败

时间:2016-11-13 19:38:48

标签: powershell powershell-v2.0 cmdlet

我有一个名为Get-Organization的cmdlet,它返回以下作为返回类型

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,它返回一个模型DepartmentModelGet-Department -OrgName <somename>会返回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}

    ...
}

加载模块后,一切都按预期工作。它破碎的地方是管道。以下返回错误

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

正如您所看到的,返回类型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

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

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

修复后可以正常工作。

由于