【问题标题】:Passing properties to a function pipeline output error将属性传递给函数管道输出错误
【发布时间】:2015-08-26 02:04:12
【问题描述】:

当我将-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

这行得通:

我希望能够使用该函数并根据结果名称设置文件名前缀。下面的代码通过添加另一个参数“对象”来工作,我想传递给函数,但是我失去了将对象传递给函数的能力。显然,如果我使用 $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"`

【问题讨论】:

  • 去掉Return关键字
  • 现在允许-Prefix "File_Name" 但是现在对象数据没有填写 cvs 文件。我认为问题在于该函数使用 $input 变量$input | Export-CSV -Path $Path -UseCulture -Encoding UTF8 -NoTypeInformation
  • 您指定Prefix 参数接受ValueFromPipeline,因此它正在使用您输入的数据。除非您需要那里,否则我会删除该参数选项。
  • 我删除了管道 REQ 的 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 ...

标签: function powershell csv export-to-csv pipeline


【解决方案1】:

如果你想接受来自高级函数的管道输入,那么它必须有参数,声明为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

【讨论】:

  • 工作就像一个魅力,正是我想要的。谢谢petseral
猜你喜欢
  • 1970-01-01
  • 2013-09-15
  • 2021-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-07
相关资源
最近更新 更多