Passing properties to a function pipeline output error

2.7k views Asked by At

I'm getting this error when I add to the return the -prefix "File_Name" to the end of Return $Results | Out-Excel -Prefix "File_Name" if I leave -prefix "File_Name" off of the script it works. What am I missing?

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

Updated but still failing:

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

ERROR:

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

This works:

I wanted to be able to use the function and set a file name prefix depending on name of the results. The code below works by adding another parameter "the object" I want to pass to the function, however I loose the ability to pipe the object to the function. Apparently if I use $input I can not take pipeline arguments?

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"`
1

There are 1 answers

1
user4003407 On BEST ANSWER

If you want to accept pipeline input from advanced function, then it have to have parameters, which declared as ValueFromPipeline or ValueFromPipelineByPropertyName and does not bound from command line.

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