【发布时间】:2018-07-25 11:30:28
【问题描述】:
我在使用 ValueFromPipelineByPropertyName 从管道中获取值时遇到一些问题。
当我运行 Get-Input -ComputerName 'PC-01' | Get-Data 时,cmdlet Get-Input 应该只返回计算机名称“PC-01”,而 Get-Data 函数应该返回“从 Get-Input 传递的值:PC-01”。相反,我收到此错误:
我构建了这两个小示例 cmdlet 只是为了掌握使用管道的窍门。
function Get-Input {
[CmdletBinding()]
Param(
[Parameter(
Mandatory = $true,
ValueFromPipelineByPropertyName = $true
)]
[string]$ComputerName
)
Process {
Write-Output -InputObject $ComputerName
}
}
function Get-Data {
[CmdletBinding()]
Param(
[Parameter(
Mandatory = $true,
ValueFromPipelineByPropertyName = $true
)]
[string]$ComputerName
)
Process {
Write-Output -InputObject "Value passed from Get-Input: $($ComputerName)."
}
}
如果我将 $ComputerName 更改为 $Name 并运行以下命令,它可以工作:
PS C:\Users\frede> Get-Service -Name AdobeARMservice | Get-Data
Value passed from Get-Input: AdobeARMservice.
如果我掌握了 PowerShell 中管道的概念,我应该能够运行以下命令 Get-Input -ComputerName 'PC-01' | Get-Data 并将 ComputerName 传递给 Get-Data。
有什么我需要在某处声明的吗?
【问题讨论】:
-
我会敦促你做进一步的调试;您可能会发现从您的函数返回到
Get-Member的管道对象将具有教育意义。 -
你的
Get-Input函数没有做任何事情 - 它接受一个字符串参数并输出相同的字符串 - 结果字符串将没有ComputerName属性
标签: powershell pipeline