2015-07-20 64 views
0

我嘗試將其他屬性添加到select-object輸出。但是,它有以下錯誤?爲「select-object」添加其他屬性?

Select-Object:無法找到接受參數'System.Object []'的位置參數。

$c = @{a = "...","...","..."; b = "...","...","..."} 
$files | 
% { 
    $filepath = $_.fullname 
    write-host $filepath 
    $type = getType $_.name 

    # The following statement works 
    Import-Csv $_ | select $c[$type] | OUT-GRIDVIEW 

    # The following doesn't work 
    Import-Csv $_ | select $c[$type] -property @{n="File"; e={$filepath}}| out-gridview 
} 
+1

單曲elect @($ c [$ type]; @ {n =「File」; e = {$ filepath}})' – PetSerAl

+0

'「...」'表示一個變量名? –

回答

1

您正在嘗試使用-Property兩次,含蓄與位置參數$c[$type],並明確您的計算性能。您需要將您的屬性列表和計算的屬性組合到一個數組中,並將其傳遞給-Property參數。要做到這一點

一種方法是使用@PetSerAl在你的問題的意見建議表達:

$files | % { 
    $filepath = $_.fullname 
    $type = getType $_.name 

    Import-Csv $_ | 
    select @($c[$type]; @{n="File";e={$filepath}}) | 
    Out-GridView 
} 

不過,我認爲這將是簡單到只需在您原有的屬性列表計算性能(S):

$c = @{ 
    a = "...", "...", "...", @{n='File';e={$_.FullName}} 
    b = "...", "...", "...", @{n='File';e={$_.FullName}} 
} 

,所以你不需要操作以後該列表:

$files | % { 
    $type = getType $_.Name 
    Import-Csv $_ | select $c[$type] | Out-GridView 
} 
相關問題