cmdlet says parameter cannot be retrieved: expression must be readable

冷暖自知 提交于 2019-12-25 01:50:39

问题


My cmdlet has a Get-Deal command, which will receive value from pipeline:

[Cmdlet(VerbsCommon.Get, "Deal")]
public class GetDealCmdlet : InsightBaseCmdlet
{
    private List<Object> _legalentities = new List<Object>();
    [Parameter(Position = 0, Mandatory = true, ValueFromPipeline = true,ValueFromPipelineByPropertyName = true)]
    public List<Object> Legalentity { set { _legalentities = value; } } 


    protected override void ProcessRecord() {...}
}

It works fine if I passed a list of string or other types. However if I passed an object that is created in Search-Deal:

foreach (...)
{
    PSObject dealValue = new PSObject();
    dealValue.Properties.Add(new PSNoteProperty(Legalentity,Convert.ToInt32($deal.Properties["LegalEntityID"].Value.ToString())));
    dealValue.Properties.Add(new PSNoteProperty("Name",deal.Properties["name"].Value.ToString()));
    WriteObject(dealValue);
}

I get an error :

Pipeline input cannot be processed because the default value of parameter 'Legalentity' cannot be retrieved. Exception getting 'Legalentity' : expression must be readable Parameter Name: expression

I am sure that the search-Deal works fine because

$a = Search-Deal name 

is working. And giving:

Get-Deal $a

returns the exact result that I want.

However

$a | Get-Deal 

will error out by the same one too.

EDIT: using

Trace-Command -Name ParameterBinding -Expression { Search-Deal JL | Get-Deal } -PSHost

I found the following:

CALLING BeginProcessing 
BIND PIPELINE object to parameters: [Get-Deal] 
PIPELINE object TYPE = [System.Management.Automation.PSCustomObject] 
RESTORING pipeline parameter's original values 
BIND PIPELINE object to parameters: [Out-Default] 
PIPELINE object TYPE = [System.Management.Automation.ErrorRecord] 
Parameter [InputObject] PIPELINE INPUT ValueFromPipeline NO COERCION 
BIND arg [Pipeline input cannot be processed because the default value of parameter 'LegalEntity' cannot be retrieved. Exception getting "LegalEntity": "Expression must be readable Parameter name: expression"] 

So I think it must be something wrong with the pipeline passing objects.

Thanks for any helps!


回答1:


The way PowerShell pipeline works will prevent this kind of scenario. Instaed of passing whole list - it will pass elements one by one. To prevent it from happening you can use unary comma:

, $a | Get-Deals

However my suggestion (as PowerShell user): do not do it, unless you have very good reason. Instead, accept/ write single objects. This is more natural and should spare future user similar griefs you have now (quite opposite actually - I would expect stream of objects returned rather than single "bloat" ;) )

Also: good practice is to name cmdlets with singular noun. Even if you expect more usually (Get-Process, Get-Service, Get-ChildItem...)




回答2:


It turned out the public List Legalentity { set { _legalentities = value; } } is missing the getter.

although I don't really know the reason behind it, adding the get {return xxx } will eliminate the error.

The error then makes sense. It is basically telling that you need to add a getter:

Pipeline input cannot be processed because the default value of parameter 'Legalentity' cannot be retrieved. Exception getting 'Legalentity' : expression must be readable Parameter Name: expression



来源:https://stackoverflow.com/questions/15888909/cmdlet-says-parameter-cannot-be-retrieved-expression-must-be-readable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!