小编hjo*_*lev的帖子

使用 ValueFromPipelineByPropertyName 从管道获取价值

我在使用从管道获取值时遇到一些问题ValueFromPipelineByPropertyName

当我运行Get-Input -ComputerName 'PC-01' | Get-Datacmdlet时Get-Input,应仅返回计算机名称“PC-01”,而该Get-Data函数应返回“从 Get-Input 传递的值:PC-01”。相反,我收到此错误:

Get-Data :输入对象不能绑定到命令的任何参数
要么是因为该命令不采用管道输入,要么是输入及其
属性与采用管道输入的任何参数都不匹配。
行:1 字符:33
+ 获取输入-计算机名 PC-01 | 获取数据
+ ~~~~~~~~~
    + CategoryInfo : InvalidArgument: (PC-01:PSObject) [获取数据],ParameterBindingException
    + FullQualifiedErrorId :InputObjectNotBound,获取数据

我构建了这两个小示例 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 …
Run Code Online (Sandbox Code Playgroud)

powershell pipeline

3
推荐指数
1
解决办法
5275
查看次数

标签 统计

pipeline ×1

powershell ×1