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
Post a Comment