本文介绍了PowerShell函数ValueFromPipelineByPropertyName不使用别名参数名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 尝试创建一个函数,该函数使用别名属性获取管道上的对象。我不确定这是哪里出了问题。 过程示例: { Param ( [Parameter(ValueFromPipelineByPropertyName = $ true)] [alias(givenname)] [System.String] $ FirstName, [参数(ValueFromPipelineByPropertyName = $ true)] [别名(sn)] [System.String] $姓氏) write-hostfirstName = $ FirstName / $($ FirstName.GetType()。FullName) Write-hostLastName = $ LastName / $($ LastName.GetType ().FullName)} 如果我运行这个命令: Get-Aduser -filter {sn -eq'smith'} -properties sn,givenname | Get-Name 输出如下所示: firstName = / string LastName = / string 函数似乎从传入的对象中获取sn和givenname属性。我错过了什么?解决方案 AD Cmdlet在这里被指责 这里的问题在于AD Cmdlet以非标准方式返回对象。例如,如果您使用命令的输出并选择一个不存在的属性,则使用任何其他cmdlet,您将不会收回任何内容,如下所示: get-date |选择仓鼠 仓鼠 ------- > 看,什么都没有。当然,它说仓鼠,但那里没有实际的物体。这是标准的PowerShell行为。 现在,看一下Get-ADUser的功能: get-aduser -Filter {sn -eq'adkison'} |选择仓鼠 仓鼠 ------- {} 它创建一个$ null!因此,你的函数会发生什么,PowerShell会查找-LastName或-FirstName的属性,获得一个$ null然后停在那里。它很糟糕! 最好的解决方法是像这样交换参数名称,它仍然可以工作: 函数Get-Name { Param ( [Parameter(ValueFromPipelineByPropertyName = $ true)] [别名('FirstName')] [System.String] $ givenname, [参数(ValueFromPipelineByPropertyName = $ true)] [别名(sn,lastname)) ] [System.String] $ Surname ) write-hostfirstName = $ 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关于这个主题的真棒回答。 Trying to create a function that takes objects on the pipeline using the alias property. I'm not sure where this is going wrong.Example of the process: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)"}If I run this command:Get-Aduser -filter {sn -eq 'smith'} -properties sn,givenname | Get-Namethe output looks like this:firstName = / stringLastName = / stringThe Function never seems to grab the sn and givenname attributes from the passed in object. What am I missing? 解决方案 The AD Cmdlets are to blame hereThe 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 HamsterHamster------->See, nothing. Sure, it says Hamster, but there is no actual Object there. This is standard PowerShell behavior.Now, look at what Get-ADUser does instead:get-aduser -Filter {sn -eq 'adkison'} | select HamsterHamster-------{}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-NamefirstName = James / System.StringLastName = Adkison / System.StringWant to know more?Check out this awesome answer from /u/JBSmith on the topic. 这篇关于PowerShell函数ValueFromPipelineByPropertyName不使用别名参数名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-23 18:10