2

I have the following code.

$strProjectPath="C:\temp\mps.xls";
$strServersExtr= @()
$strOSExtr= @()
$objServersList = New-Object PSObject
$objServersList | Add-Member -MemberType NoteProperty -Name ServerName -Value "" -PassThru | Add-Member -MemberType NoteProperty -Name OSType -Value ""

I read an Excel file (through a loop) to put 2 specific columns to these two members. The problem is that I can't add "Values" one after the other. += doesn't work with objects. I had try also to collect to and array and then to the object but it didn't work also. How is it possible to add lines to an already existing object?

Example:

ServerName                                                  OSType
----------                                                  ------
blabla1                                                     Windows XP
blabla2                                                     Windows 7 Professional

PS1.The $strServersExtr and the $strOSExtr was an attempt to collect the 2 columns in the 2 arrays and then put them in to the object.

PS2. I work with PS 3.0 but any solution will be preferable as I try to make the code easy to work on PS 2.0.

Alex
  • 39
  • 1
  • 1
  • 5
  • Your question remain unclear, do you want to add a third column to all your objects stored in `$objServersList` variable? What do you mean by _"..I can't add "Values" one after the other.."_ ? – Raf May 20 '14 at 10:07
  • No, I think is clear. I don't speak about columns. That is easy to add members. The object is empty and I want to be like the example. No columns, rows. – Alex May 20 '14 at 10:26
  • We clearly(sic!) have a different version of clarity in mind. Do you mean adding more than one object to `$objServersList`? – Raf May 20 '14 at 10:51
  • @Raf, Not all people know terminology. If you want to help them, you need to be open minded and deduce what they really need. – majkinetor May 20 '14 at 11:12
  • Here are some useful tips for the future: http://stackoverflow.com/questions/how-to-ask http://msmvps.com/blogs/jon_skeet/archive/2010/08/29/writing-the-perfect-question.aspx – Raf May 20 '14 at 11:23
  • @Raf,Here are some useful tips for the future: stackoverflow.com/questions/how-to-ask #Keep an open mind. I tried to be as clear as I can be providing a example from what I want. If is something isn't clear to you, do not provide help. No bad feelings. No one is gonna die. And thanks for the downvote. Lucky I can't repay it for the wrong answer. Anyway. – Alex May 20 '14 at 14:18

2 Answers2

5

As I see it u want array of objects (not 'lines'). So

$serverList= @() 
$serverList+= @{ServerName= 'blabla1'; OSType='Windows XP'}
$serverList+= @{ServerName= 'blabla2'; OSType='Windows XP Profesional'}

#display as table
$serverList | % { new-object PSObject -Property $_}
majkinetor
  • 8,730
  • 9
  • 54
  • 72
  • 1
    So the 'objServersList = New-Object PSObject $objServersList | Add-Member -MemberType NoteProperty -Name ServerName -Value "" -PassThru | Add-Member -MemberType NoteProperty -Name OSType -Value ""' are useless? – Alex May 20 '14 at 10:55
  • Yes, the older syntax (perhaps there are more options to set using it, couldn't say from head). See improved answer – majkinetor May 20 '14 at 11:00
  • I will accept the answer as it seems to match my needs. Thanks – Alex May 20 '14 at 11:06
0

EDIT: To add more than one element to array use the +=, notation:

...
$objServer = New-Object PSObject 
$objServer | Add-Member -MemberType NoteProperty -Name ServerName -Value "" -PassThru | Add-Member -MemberType NoteProperty -Name OSType -Value ""
$objServersList +=,$objServer
Raf
  • 9,681
  • 1
  • 29
  • 41
  • nope. I have already created the object. I want to add values. I will rephrase my question to be more clear. – Alex May 20 '14 at 09:53
  • It doesn't work. Correcting the $objServersList with $objServer I have this: `Method invocation failed because [System.Management.Automation.PSObject] doesn't contain a method named 'op_Addition'. At line:1 char:1` – Alex May 20 '14 at 11:04