powershell 管道对象来运行

sdj*_*uan 2 powershell

这是我正在尝试做的存根:

function Set-test {
[CmdletBinding()]
    param(
        [Parameter(Mandatory=$True,
               ValueFromPipeline=$True)]
        [object[]]$inputObject
    )
    Write-Verbose "Set-test: input obj= $inputObject" 

    PROCESS {
        foreach ($obj in $inputobject) {
            Write-Output $obj
        }
    }   
}

function Get-test {
    $obj = New-Object PSObject
    $obj | Add-Member -MemberType NoteProperty -Name "swName" -Value "banana"
    $obj | Add-Member -MemberType NoteProperty -Name "Version" -Value "3.2.2"
    Write-Output "obj = $obj"
    $obj | Set-test -verbose    
}
Run Code Online (Sandbox Code Playgroud)

这是输出:

PS > Get-test
obj = @{swName=banana; Version=3.2.2}
VERBOSE: Set-test: input obj= 
Get-Process : Cannot evaluate parameter 'Name' because its argument is  specified as a script block and there is no input. A script block cannot be evaluated without input.
At C:\Users\username\Documents\WindowsPowerShell\Modules\mytool\mytools.psm1:204 char:13
+     PROCESS {
+             ~
+ CategoryInfo          : MetadataError: (:) [Get-Process], ParameterBindingException
+ FullyQualifiedErrorId : ScriptBlockArgumentNoInput,Microsoft.PowerShell.Commands.GetProcessCommand
Run Code Online (Sandbox Code Playgroud)

从详细输出中可以看出,对象似乎没有通过管道传输到函数,这让我感到困惑。

小智 5

您不能在 begin{}、process{} 或 end{} 块之外的 set-test 中使用这种写详细信息。它把事情搞砸了。如果您将它移动到 process{} 块内,它可以正常工作。