将属性传递给函数管道输出错误

Passing properties to a function pipeline output error

我在 return 中添加 -prefix "File_Name"Return $Results | Out-Excel -Prefix "File_Name" 的末尾时出现此错误,如果我将 -prefix "File_Name" 留在脚本之外它会起作用.我错过了什么?

Out-Excel : 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 \util01\cs\Scripts\PowerShell\BradleyDev\Google Chrome\Set-Chrome-Service-ON-V3.ps1:52 char:21
+ } Return $Results | Out-Excel -Prefix "File_Name"
+                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (@{PC=CD-100TRAINER; Status=OFF-LINE}:PSObject) [Out-Excel], ParameterBindingException
    + FullyQualifiedErrorId : InputObjectNotBound,Out-Excel

foreach ($PC in $Process){
$counter ++
$Service = "gupdate"
$State = "delayed-auto"
$command = "sc.exe \$PC config $Service start= $State"
$count = $Process.Count
$command2 = "sc.exe \$PC config gupdatem start= demand"
if (test-connection -computername $PC -BufferSize 16 -Count 1 -quiet) {
    $Output = Invoke-Expression -Command $Command -ErrorAction Stop
    $Output = Invoke-Expression -Command $Command2 -ErrorAction Stop
    if($LASTEXITCODE -ne 0){
       Write-Host "$counter of $count : $PC : Failed More details: $Output" -foregroundcolor White -BackgroundColor RED
       $status = [string]$Output
    } else {
       Write-Host "$counter of $count : $PC : Success" -foregroundcolor DarkGreen
       $status = "Success"
    }
} else {
    Write-Host "$counter of $count : $PC : OFF-LINE" -foregroundcolor black -BackgroundColor gray
    $status = "OFF-LINE"
}       
$PCobject = [PSCustomObject] @{
    PC = $PC
    Status = $status
}
$Results +=$PCobject
} Return $Results | Out-Excel -Prefix "File_Name"

function Out-Excel {
param(
    [Parameter(Mandatory=$True,HelpMessage="Enter Prefix",ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
    [ValidateNotNullorEmpty()]
    [string[]]$Prefix = $null,
    $Path = "$env:temp$Prefix`_$(Get-Date -Format yyMMdd_HHmmss).csv"
)
$input | Export-CSV -Path $Path -UseCulture -Encoding UTF8 -NoTypeInformation
Invoke-Item -Path $Path
} #end function

已更新但仍然失败:

function Out-Excel {
    param(
        [Parameter(Mandatory=$True,HelpMessage="Enter Prefix")]
        [ValidateNotNullorEmpty()]
        [string[]]$Prefix = $null,
        $Path = "$env:temp$Prefix`_$(Get-Date -Format yyMMdd_HHmmss).csv"
    )
    $input | Export-CSV -Path $Path -UseCulture -Encoding UTF8 -NoTypeInformation
    Invoke-Item -Path $Path
} #end function

错误:

Out-Excel : 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 \util01\cs\Scripts\PowerShell\BradleyDev\Google Chrome\Set-Chrome-Service-ON-V3.ps1:52 char:14
+ } $Results | Out-Excel -Prefix "Services_Results"
+              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (@{PC=CD-100TRAINER; Status=OFF-LINE}:PSObject) [Out-Excel], ParameterBindingException
    + FullyQualifiedErrorId : InputObjectNotBound,Out-Excel

这个有效:

我希望能够使用该功能并根据结果名称设置文件名前缀。下面的代码通过添加我想传递给函数的另一个参数 "the object" 来工作, 但是我失去了将对象通过管道传递给函数的能力 。显然如果我使用 $input 我不能接受管道参数?

function Out-Excel {
param(
    [Parameter(Mandatory=$True,HelpMessage="Enter Prefix")]
    [ValidateNotNullorEmpty()]
    [string[]]$Prefix = $null,
    [Parameter(Mandatory=$True,HelpMessage="Enter Prefix")]
    [ValidateNotNullorEmpty()]
    [object]$Object = $null,
    $Path = "$env:temp$Prefix`_$(Get-Date -Format yyMMdd_HHmmss).csv"
)
$Object | Export-CSV -Path $Path -UseCulture -Encoding UTF8 -NoTypeInformation
Invoke-Item -Path $Path
} #end function

$Results +=$PCobject
 } Out-Excel -object $Results -Prefix "Services_Results"`

如果你想接受来自高级函数的管道输入,那么它必须有参数,这些参数声明为ValueFromPipelineValueFromPipelineByPropertyName并且不从命令行绑定。

function Out-Excel {
param(
    [Parameter(Mandatory=$True,HelpMessage="Enter Prefix")]
    [string]$Prefix = $null,
    [Parameter(Mandatory=$True,ValueFromPipeline=$True)]
    [psobject]$InputObject = $null,
    $Path = "$env:temp$Prefix`_$(Get-Date -Format yyMMdd_HHmmss).csv"
)
begin{
    $List=New-Object System.Collections.Generic.List[PSObject]
}
process{
    $List.Add($InputObject)
}
end{
    $List | Export-CSV -Path $Path -UseCulture -Encoding UTF8 -NoTypeInformation
    Invoke-Item -Path $Path
}
} #end function