powershell - No method found "op_addition" -


i want add specific selection of data in variable this:

$a = get-service | select -first 10 | ? name -like "app*" $collection = $null foreach($item in $a){   if($item.status -like "running"){     $collection = $collection + $item   } } 

when trying run got error like:

no method found "op_addition"

what can save selection in separate variable?

at first, $collection $null.

then, after first loop iteration, $collection single servicecontroller object, since $null + $object $object.

on second loop iteration, fails because servicecontroller don't have overloads +, error informs you.

you'll need declare $collection actual collection (you can use @() array subexpression operator) + work way expect:

$a = get-service | select -first 10 | ? name -like "app*" $collection = @() foreach($item in $a){   if($item.status -like "running"){     $collection = $collection + $item   } } 

alternatively, assign output loop directly $collection:

$a = get-service | select -first 10 | ? name -like "app*" $collection = foreach($item in $a){   if($item.status -like "running"){     $item   } } 

which of course simplified single where-object statement in pipeline:

$collection = get-service app* |where-object {$_.status -eq 'running'} 

Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -