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.

6
  • 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.." ? Commented May 20, 2014 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. Commented May 20, 2014 at 10:26
  • We clearly(sic!) have a different version of clarity in mind. Do you mean adding more than one object to $objServersList? Commented May 20, 2014 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. Commented May 20, 2014 at 11:12
  • Here are some useful tips for the future: stackoverflow.com/questions/how-to-ask msmvps.com/blogs/jon_skeet/archive/2010/08/29/… Commented May 20, 2014 at 11:23

2 Answers 2

6

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 $_}
Sign up to request clarification or add additional context in comments.

3 Comments

So the 'objServersList = New-Object PSObject $objServersList | Add-Member -MemberType NoteProperty -Name ServerName -Value "" -PassThru | Add-Member -MemberType NoteProperty -Name OSType -Value ""' are useless?
Yes, the older syntax (perhaps there are more options to set using it, couldn't say from head). See improved answer
I will accept the answer as it seems to match my needs. Thanks
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

2 Comments

nope. I have already created the object. I want to add values. I will rephrase my question to be more clear.
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.