I\'ve been banging my head against the wall on this one.
I know that if I create an Array in Powershell, then copy the array, it\'ll copy it as a reference type not a va
To copy an array, you can do the following:
$c = (0,0,0)
$d = $c | foreach { $_ }
$c[0] = 1
"c is [$c]"
"d is [$d]"
Result
c is [1 0 0]
d is [0 0 0]
For your particular issue (comparing CPU usage of processes), something more specific would probably be better, as Keith pointed out.
Technically $d = $c
is not any sort of array copy (of reference or value). It's just stashing a reference to the array $c refers to, into $d. I think you only need to grab the array of processes once and then call the Refresh
method. You'll have to check the Exited
property first to make sure the associated process is still running. Of course, this won't help you if you're interested in any new process that start up. In that case, grab a snapshot of processes at different times, weed out all but the intersection of processes between the two arrays (by process Id) and then compute the differences in their property values - again based on process Id. Take make this easier, you might want to put each snapshot in a hashtable keyed off the process Id.