2012-01-27 148 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