使用 ValueFromPipelineByPropertyName 从管道获取价值
Get value from pipeline with ValueFromPipelineByPropertyName
我在使用 ValueFromPipelineByPropertyName
从管道获取值时遇到一些问题。
当我 运行 Get-Input -ComputerName 'PC-01' | Get-Data
cmdlet Get-Input
应该只是 return 计算机名称 "PC-01",而 Get-Data
函数应该 return "Value passed from Get-Input: PC-01"。相反,我收到此错误:
Get-Data : 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:33
+ Get-Input -ComputerName PC-01 | Get-Data
+ ~~~~~~~~
+ CategoryInfo : InvalidArgument: (PC-01:PSObject) [Get-Data], ParameterBindingException
+ FullyQualifiedErrorId : InputObjectNotBound,Get-Data
我构建了这两个小示例 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
。
有什么地方需要申报吗?
你还必须写 ValueFromPipeline=$true:
Mandatory = $true,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName = $true
您好
詹尼克
你会需要这个芽。
ValueFromPipelineByPropertyName
用于布尔值 ($True / $False),并不是在寻找您的字符串。
[CmdletBinding()]
param
(
[Parameter(
Mandatory = $true,
ValueFromPipeline = $true
)]
[string]$ComputerName
)
如名称 (ValueFromPipelineByPropertyName
) 所示,您是在告诉解析器根据 属性 名称 绑定一个值。
Get-Input
函数需要输出一个对象,该对象具有一个名为 ComputerName
的 属性 才能工作:
function Get-Input
{
[CmdletBinding()]
param
(
[Parameter(
Mandatory = $true,
ValueFromPipelineByPropertyName = $true
)]
[string]$ComputerName
)
process
{
Write-Output $(New-Object psobject -Propert @{ComputerName = $ComputerName})
}
}
现在您可以:
Get-Input -ComputerName 'PC-01' |Get-Data
如果您希望 Get-Data
支持来自 Get-Service
的计算机名称输入,您必须添加一个与 属性 输出的对象类型上的适当 属性 名称相匹配的别名 Get-Service
,即。 MachineName
:
function Get-Data
{
[CmdletBinding()]
param
(
[Parameter(
Mandatory = $true,
ValueFromPipelineByPropertyName = $true
)]
[Alias('MachineName')]
[string]$ComputerName
)
process
{
Write-Output -InputObject "Value passed from Get-Input: $($ComputerName)."
}
}
现在这两个都可以了:
Get-Service -Name AdobeARMService |Get-Data
Get-Input -ComputerName PC-01 |Get-Data
我在使用 ValueFromPipelineByPropertyName
从管道获取值时遇到一些问题。
当我 运行 Get-Input -ComputerName 'PC-01' | Get-Data
cmdlet Get-Input
应该只是 return 计算机名称 "PC-01",而 Get-Data
函数应该 return "Value passed from Get-Input: PC-01"。相反,我收到此错误:
Get-Data : 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:33 + Get-Input -ComputerName PC-01 | Get-Data + ~~~~~~~~ + CategoryInfo : InvalidArgument: (PC-01:PSObject) [Get-Data], ParameterBindingException + FullyQualifiedErrorId : InputObjectNotBound,Get-Data
我构建了这两个小示例 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
。
有什么地方需要申报吗?
你还必须写 ValueFromPipeline=$true:
Mandatory = $true,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName = $true
您好 詹尼克
你会需要这个芽。
ValueFromPipelineByPropertyName
用于布尔值 ($True / $False),并不是在寻找您的字符串。
[CmdletBinding()]
param
(
[Parameter(
Mandatory = $true,
ValueFromPipeline = $true
)]
[string]$ComputerName
)
如名称 (ValueFromPipelineByPropertyName
) 所示,您是在告诉解析器根据 属性 名称 绑定一个值。
Get-Input
函数需要输出一个对象,该对象具有一个名为 ComputerName
的 属性 才能工作:
function Get-Input
{
[CmdletBinding()]
param
(
[Parameter(
Mandatory = $true,
ValueFromPipelineByPropertyName = $true
)]
[string]$ComputerName
)
process
{
Write-Output $(New-Object psobject -Propert @{ComputerName = $ComputerName})
}
}
现在您可以:
Get-Input -ComputerName 'PC-01' |Get-Data
如果您希望 Get-Data
支持来自 Get-Service
的计算机名称输入,您必须添加一个与 属性 输出的对象类型上的适当 属性 名称相匹配的别名 Get-Service
,即。 MachineName
:
function Get-Data
{
[CmdletBinding()]
param
(
[Parameter(
Mandatory = $true,
ValueFromPipelineByPropertyName = $true
)]
[Alias('MachineName')]
[string]$ComputerName
)
process
{
Write-Output -InputObject "Value passed from Get-Input: $($ComputerName)."
}
}
现在这两个都可以了:
Get-Service -Name AdobeARMService |Get-Data
Get-Input -ComputerName PC-01 |Get-Data