2012-01-27 147 views
0

我是相當新的PowerShell,我基本上是編寫一個腳本,執行基於主列的幾個.csv文件的連接。我使用從這裏的加入收藏腳本:http://poshcode.org/1461不能執行相同的PowerShell功能兩次,給出錯誤

由於我需要結合5個.csv文件,我需要運行此功能4次。

在第一次運行時,它工作正常,但後來嘗試再次運行該函數給出'沒有指定給cmd-let的對象'錯誤。

在試圖進行調試時,我直接複製並粘貼了該行,並且僅更改了變量名以創建新變量。

我必須做一些根本性的錯誤...

$SumFile = "VMSummary.csv" 
$MemFile = "VMMemory.csv" 
$ProcFile = "VMProcessor.csv" 
$OSFile = "VMOS.csv" 
$NicFile = "VMNics.csv" 

$SumFileCSV = Import-Csv $SumFile | Select VMElementName,GuestOS,Heartbeat,MemoryUsage,IpAddress 
$MemFileCSV = Import-Csv $MemFile | Select VMElementName,Reservation 
$ProcFileCSV = Import-Csv $ProcFile 
$OSFileCSV = Import-Csv $OSFile 
$NicFileCSV = Import-Csv $NicFile 

$JoinColumn = "VMElementName" 

function Join-Collections { 
PARAM(
    $FirstCollection 
, [string]$FirstJoinColumn 
, $SecondCollection 
, [string]$SecondJoinColumn=$FirstJoinColumn 
) 
PROCESS { 
    $ErrorActionPreference = "Inquire" 
    foreach($first in $FirstCollection) { 
     $SecondCollection | Where{ $_."$SecondJoinColumn" -eq $first."$FirstJoinColumn" } | Join-Object $first 
    } 
} 
BEGIN { 
    function Join-Object { 
    Param(
     [Parameter(Position=0)] 
     $First 
    , 
     [Parameter(ValueFromPipeline=$true)] 
     $Second 
    ) 
    BEGIN { 
     [string[]] $p1 = $First | gm -type Properties | select -expand Name 
    } 
    Process { 
     $Output = $First | Select $p1 
     foreach($p in $Second | gm -type Properties | Where { $p1 -notcontains $_.Name } | select -expand Name) { 
     Add-Member -in $Output -type NoteProperty -name $p -value $Second."$p" 
     } 
     $Output 
    } 
    } 
} 
} 

$Temp = Join-Collections $SumFileCSV $JoinColumn $MemFileCSV $JoinColumn 

$Temp 

##BREAKS HERE 
$Temp2 = Join-Collections $SumFileCSV $JoinColumn $MemFileCSV $JoinColumn 

UPDATE

它提供了以下錯誤:

No object has been specified to the get-member cmdlet 
+ foreach($p) in $Second | gm <<<< -type Properties | Where { $p1 -notcontains  $_.Name } | select -expand Name) 

CSV數據是非常直接的。當我打印出$溫度它打破之前,它吐出:

GuestOS  : Windows Server (R) 2008 Standard 
Heartbeat  : OK 
IpAddress  : 192.168.48.92 
MemoryUsage : 1024 
VMElementName : VM015 
Reservation : 1024 

GuestOS  : Windows Server (R) 2008 Standard 
Heartbeat  : OK 
IpAddress  : 192.168.48.151 
MemoryUsage : 1028 
VMElementName : VM053 
Reservation : 1028 

GuestOS  : Windows Server (R) 2008 Standard 
Heartbeat  : OK 
IpAddress  : 192.168.48.214 
MemoryUsage : 3084 
VMElementName : VM065 
Reservation : 3084 

GuestOS  : 
Heartbeat  : 
IpAddress  : 
MemoryUsage : 
VMElementName : VM074 
Reservation : 1024 

GuestOS  : Windows Server 2008 R2 Standard 
Heartbeat  : OK 
IpAddress  : 192.168.48.32 
MemoryUsage : 3072 
VMElementName : VM088 
Reservation : 3072 

GuestOS  : Windows Server (R) 2008 Enterprise 
Heartbeat  : OK 
IpAddress  : 192.168.48.81 
MemoryUsage : 3084 
VMElementName : VM090 
Reservation : 3084 

GuestOS  : Windows Server 2008 R2 Enterprise 
Heartbeat  : OK 
IpAddress  : 192.168.48.82 
MemoryUsage : 5120 
VMElementName : VM106 
Reservation : 5120 

該.csv數據的其餘部分是同一類的東西 - 只是統計上的不同的服務器。

理想是我想要做的是這樣的:

$Temp = Join-Collections $SumFileCSV $JoinColumn $MemFileCSV $JoinColumn 

$Temp = Join-Collections $Temp $JoinColumn $ProcFileCSV $JoinColumn 

$Temp = Join-Collections $Temp $JoinColumn $OSFileCSV $JoinColumn 

$Temp = Join-Collections $Temp $JoinColumn $NicFileCSV $JoinColumn | Export-Csv "VMJoined.csv" -NoTypeInformation -UseCulture 
+0

工作正常,我。 – manojlds 2012-01-27 05:16:15

+0

它給了什麼錯誤?還發布您正在合併的CSV數據示例。 – 2012-01-27 05:23:14

+0

更新了額外的信息... – 2012-01-27 05:49:34

回答

1

此代碼工作正常上Powershell的V3 CTP 2(這可能是什麼@manojlds使用)。但是,在Powershell V2中,當第二次調用Join-Collections時,Join-Object函數的參數$second未被綁定。這可以通過添加下列行來處理塊的Join-Object函數內很容易地驗證:

$psboundparameters | out-host 

你會發現,對於第一次調用Join-Collections當兩個參數(Join-Object結合,然而,第二時間$second不再綁定。

目前還不清楚是什麼原因造成這種行爲,但因爲它似乎在PowerShell中V3是工作我猜這是一個錯誤。

要在PowerShell中V2一個功能的工作可能explicitl通過這條線

$SecondCollection | Where{ $_."$SecondJoinColumn" -eq $first."$FirstJoinColumn" } | Join-Object $first 

::Y通過更換這行綁定參數

$SecondCollection | Where{ $_."$SecondJoinColumn" -eq $first."$FirstJoinColumn" } | %{Join-Object -first $first -second $_} 
+0

你說得對我使用V3: – manojlds 2012-01-27 15:02:14

+0

非常感謝! – 2012-01-29 23:16:46