简体   繁体   中英

new-object psobject add value

I understand that custom objects are created like this :

$obj=new-object psobject

and then I understand that you can add members (and values) like this :

$obj | Add-Member noteproperty name Jeff

Now the question is, how do you populate the object, add and remove "rows" of values ?

The only way around I found is to create an array and then push objects inside it, like this :

$array = @()
$array+=new-object PSObject -Property @{Name="Jeff"}
$array+=new-object PSObject -Property @{Name="Joe"}
$array+=new-object PSObject -Property @{Name="John"}

etc..

Is there a straight forward way to "increment" the values of the members in an object ?

$obj+=(Name=John) 

doesn't work.

Thanks

A very late response, but I hope it helps someone who needs to count objects.

Let's start with a list of users we wish to count.

> $users = 1..10 | % {New-object psobject -Property @{ Name = "User $_"; Age = $_ } }
> $users
Age Name
--- ----
  1 User 1
  2 User 2
  3 User 3
  4 User 4
  5 User 5
  6 User 6
  7 User 7
  8 User 8
  9 User 9
 10 User 10

To count them, put them into a hash table of counters

 > # Create hash table
 > $counter = @{}
 > # Assign users as keys in the table
 > $users  | % { $counter.Add($_, 0) }
 > $counter
Name                           Value
----                           -----
@{Age=4; Name=User 4}          0
@{Age=1; Name=User 1}          0
@{Age=3; Name=User 3}          0
@{Age=5; Name=User 5}          0
@{Age=10; Name=User 10}        0
@{Age=9; Name=User 9}          0
@{Age=8; Name=User 8}          0
@{Age=7; Name=User 7}          0
@{Age=6; Name=User 6}          0
@{Age=2; Name=User 2}          0

Then you can increment the counter whenever you encounter the user in your script. For example, to increment "User 1" twice and "User 4" once

> $counter[$users[0]] += 1
> $counter[$users[0]] += 1
> $counter[$users[3]] += 1
> $counter

Name                           Value
----                           -----
@{Age=4; Name=User 4}          1
@{Age=1; Name=User 1}          2
@{Age=3; Name=User 3}          0
@{Age=5; Name=User 5}          0
@{Age=10; Name=User 10}        0
@{Age=9; Name=User 9}          0
@{Age=8; Name=User 8}          0
@{Age=7; Name=User 7}          0
@{Age=6; Name=User 6}          0
@{Age=2; Name=User 2}          0

In your example above, I believe you end up with a System.Management.Automation.PSCustomObject, not an array. I use something similar to what you're doing when I build reports that contain custom objects. If you're really just using that to store one property, though, it's probably overkill. You could just do something like this:

$names += "John"
$names += "Fred"

In the case that you really want to add new note properties to an object throughout a script, this is how I do it. Keep in mind PowerShell doesn't like to add note properties with the same name, so if you do that you'll have to simply set the property with =

Here's an example of what I do:

$params += @{Name = $_.Name}
$params += @{Calculation = $someCalculatedValue}
$collection += New-Object -Type PSObject -Property $params

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM