0

How would I pipe the information I've pulled out from a spreadsheet into a Powershell object?

$rangeAddr=$startCell + ":" + $endCell
    $sh.Range($rangeAddr).Value2 | foreach {
        New-Object PSObject -Property @{"Build"=$sh.Name;"AppName"=$_;}
    }
methuselah
  • 12,766
  • 47
  • 165
  • 315
  • Which version of PowerShell are you using and what isn't working as you expect with the script you posted. BTW the double quotes around `"Build"` and `"AppName"` are not necessary and neither is that last semi-colon. – Keith Hill Oct 26 '13 at 23:35
  • I'm using 1.0 on an enterprise machine (can't upgrade) - I'm trying to create an object from the data I'm pulling out of an Excel spreadsheet – methuselah Oct 26 '13 at 23:41
  • Are you getting error or not the results you expected? I assume for Build you really want the Name of the worksheet? And for AppName, you want the cell contents? – Keith Hill Oct 26 '13 at 23:44
  • Yes, I'm not getting any errors at the moment. Ideally I would like to create one object with four properties? How feasible is that? – methuselah Oct 26 '13 at 23:50
  • http://stackoverflow.com/questions/19612072/powershell-pulling-information-from-an-extra-column - the full code is there - but I want to pull down information from column J and K as well, then pipe it all into an object to export to a CSV file under 4 different columns – methuselah Oct 26 '13 at 23:52
  • 1
    I'm not sure how you wouldn't be getting errors as there is *no* `-Property` parameter on New-Object in PowerShell 1.0. – Keith Hill Oct 26 '13 at 23:54
  • Got some help here > http://stackoverflow.com/questions/19211632/read-excel-sheet-in-powershell – methuselah Oct 26 '13 at 23:56

1 Answers1

2

I believe creating objects from hash tables was in V2, but not V1.

Does this work?

$rangeAddr=$startCell + ":" + $endCell
    $sh.Range($rangeAddr).Value2 |
      foreach {
        $NewObject = "" | Select Build,Name
        $Newobject.Build    = $sh.Name
        $NewObject.Name  = $_ 
        $NewObject
       }
mjolinor
  • 66,130
  • 7
  • 114
  • 135
  • Thanks. It works great. Is it possible to add a third/fourth column with information from column J/K in the spreadsheet (same start/endcell)? – methuselah Oct 27 '13 at 00:12
  • I'm sure it is, but I don't use com enough to tell you off the top of my head. – mjolinor Oct 27 '13 at 00:25
  • I think you have an issue between your use of `Select Build,*Name*` and the later use of `$NewObject.*AppName*`. Those should be the same. :-) – Keith Hill Oct 27 '13 at 05:58