2012-01-27 2 views
0

저는 powershell을 처음 사용하기 때문에 기본적으로 기본 열을 기반으로 여러 .csv 파일에 대한 조인을 수행하는 스크립트를 작성하고 있습니다. 여기에서 Join-Collections 스크립트를 사용하고 있습니다 : http://poshcode.org/1461동일한 powershell 함수를 두 번 실행할 수없고 오류가 발생합니다.

5 개의 .csv 파일을 결합해야하므로이 기능을 4 번 실행해야합니다.

처음에는 제대로 작동하지만 함수를 다시 실행하려고하면 'cmd-let에 지정된 개체가 없습니다.'오류가 발생합니다.

디버깅을 시도하면서 문자 그대로 복사하여 붙여 넣었으며 변수 이름 만 변경하여 새 변수를 만들었습니다.

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로 데이터가 꽤 정직 : 나는 근본적으로 잘못된 일을해야

...

$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는

그것은 다음과 같은 오류를 제공합니다. .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 

나머지는 물건의 같은 종류입니다 - 단지 다른 서버에 통계 : 나는 그것을 파괴 직전 $ 온도를 인쇄 할 때, 그것은 밖으로 뱉어.

이상적으로 제가하고 싶은 것은 이것이다 :

$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

+0

어떤 오류가 있습니까? 또한 병합중인 CSV 데이터의 예를 게시하십시오. –

+0

추가 정보로 업데이트 ... –

답변

1

이 코드는 (아마 인 사용 무엇 @manojlds) 파워 쉘 V3 CTP 2에서 잘 작동합니다. Powershell V2에서는 을 두 번 호출 할 때 Join-Object 함수의 $second 매개 변수가 바인딩되지 않습니다. 이 쉽게 Join-Object 함수 내부 처리 블록에 다음 라인을 추가로 확인할 수있다 :

$psboundparameters | out-host 

당신은 Join-Object의 (처음 두 파라미터 Join-Collections을 호출 할 때 것은 결합되는 그러나 번째 알 $second 더 이상 구속되지 않습니다.

이이 문제의 원인을 불분명하지만 파워 쉘 V3에서 작동하는 것 같군 이후 그것이 버그 같은데요.

을 파워 쉘 V2 하나의 기능이 작동하려면 수 명백한 Y는이 라인을 대체하여 매개 변수를 바인딩 :

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

를이 선으로 :

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

당신은 v3을 사용하는 저에 대해 맞습니다. – manojlds

+0

많은 감사를드립니다! –

관련 문제