title: Cannot execute same powershell function twice, gives error
body: <p>I'm fairly new to powershell and I'm basically writing a script which performs a join on several .csv files based on a primary column. I am using the Join-Collections script from here: <a href="http://poshcode.org/1461" rel="nofollow">http://poshcode.org/1461</a></p>
  
  <p>As I need to combine 5 .csv files, I need to run this function 4 times.</p>
  
  <p>On the first run, it works fine, but then trying to run the function again gives 'No object specified to the cmd-let' errors.</p>
  
  <p>In trying to debug, I've literally copy-and-pasted the line and only changed the variable name to make a new variable.</p>
  
  <p>I must be doing something fundamentally wrong...</p>
  
  <pre><code>$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
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>It gives the following error:</p>
  
  <pre><code>No object has been specified to the get-member cmdlet
  + foreach($p) in $Second | gm &lt;&lt;&lt;&lt;  -type Properties | Where { $p1 -notcontains     $_.Name } | select -expand Name) 
  </code></pre>
  
  <p>The csv data is pretty straight forward. When I print out $Temp just before it breaks, it spits out:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>The rest of the .csv data is the same sort of stuff - just stats on different servers.</p>
  
  <p>Ideally what I want to do is this : </p>
  
  <pre><code>$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
  </code></pre>