PowerShell 函数 ValueFromPipelineByPropertyName 不使用别名参数名称 [英] PowerShell function ValueFromPipelineByPropertyName not using alias parameter name

查看:17
本文介绍了PowerShell 函数 ValueFromPipelineByPropertyName 不使用别名参数名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试创建一个函数,该函数使用别名属性获取管道上的对象.我不确定这是哪里出了问题.

Trying to create a function that takes objects on the pipeline using the alias property. I'm not sure where this is going wrong.

流程示例:

function Get-Name
{
  Param
  (
    [Parameter(ValueFromPipelineByPropertyName=$true)]
    [alias("givenname")]
    [System.String] $FirstName,

    [Parameter(ValueFromPipelineByPropertyName=$true)]
    [alias("sn")]
    [System.String] $LastName
  )
  write-host "firstName = $FirstName / $($FirstName.GetType().FullName)"
  Write-host "LastName  = $LastName  / $($LastName.GetType().FullName)"
}

如果我运行这个命令:

Get-Aduser -filter {sn -eq 'smith'} -properties sn,givenname | Get-Name

输出如下:

firstName =  / string
LastName  =   / string

函数似乎永远不会从传入的对象中获取 sn 和 givenname 属性.我错过了什么?

The Function never seems to grab the sn and givenname attributes from the passed in object. What am I missing?

推荐答案

AD Cmdlet 是罪魁祸首

这里的问题是 AD Cmdlet 以非常不标准的方式返回对象.例如,对于任何其他 cmdlet,如果您获取命令的输出并选择一个不存在的属性,您将一无所获,如下所示:

The problem here is that the AD Cmdlets return objects in really non-standard ways. For instance, with any other cmdlet if you take the output of the command and select a non-existing property, you'll get back nothing, like this:

get-date | select Hamster

Hamster                                                                                                                                                                                                                        
-------     

>

看,没什么.当然,它说的是仓鼠,但那里没有实际的对象.这是标准的 PowerShell 行为.

See, nothing. Sure, it says Hamster, but there is no actual Object there. This is standard PowerShell behavior.

现在,看看 Get-ADUser 做了什么:

Now, look at what Get-ADUser does instead:

get-aduser -Filter {sn -eq 'adkison'} | select Hamster

Hamster                                                                                                                                                                                                                        
-------                                                                                                                                                                                                                        
{}                    

它创建了一个 $null!因此,您的函数会发生什么,PowerShell 将查找 -LastName 或 -FirstName 的属性,获取 $null 并在那里停止.糟透了!

It creates a $null! So what will happen with your function is that PowerShell will look for a property of -LastName or -FirstName, get a $null and then stop right there. It sucks!

解决这个问题的最好方法是像这样交换参数名称,它仍然有效:

The best way around this is to swap the parameter names like this, and it will still work:

function Get-Name
{
  Param
  (
    [Parameter(ValueFromPipelineByPropertyName=$true)]
    [alias('FirstName')]
    [System.String] $givenname,

    [Parameter(ValueFromPipelineByPropertyName=$true)]
    [alias("sn","lastname")]
    [System.String] $Surname
  )
  write-host "firstName = $givenname / $($givenname.GetType().FullName)"
  Write-host "LastName  = $SurName  / $($SurName.GetType().FullName)"
}


get-aduser -Filter {sn -eq 'adkison'} | Get-Name

firstName = James / System.String
LastName  = Adkison  / System.String

想了解更多?

查看这个来自/u/JBSmith 的关于该主题的精彩回答.

这篇关于PowerShell 函数 ValueFromPipelineByPropertyName 不使用别名参数名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆