2016-11-13 36 views
0

失敗我有一個名爲Get-Organization cmdlet將返回下面的返回類型ValueFromPipelineByPropertyName在模塊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它返回一個模型DepartmentModel另一個cmdlet。 Get-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 

任何想法?

+0

嘗試將GetOrganizationCmdlet的名稱參數更改爲orgname? – 4c74356b41

+1

顯示'Get-Organization -Name的輸出 | Get-Member'和'(Get-Command Get-Department).ParameterSets'。 – PetSerAl

+0

是的,gm是一個合乎邏輯的問題,但我認爲那個人已經做到了 – 4c74356b41

回答

0

使用@PeterSerAl提到的命令我注意到返回時間被錯誤地設置爲一個集合。

它在修復後有效。

謝謝