does not contain a method named 'op_Addition'

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.

This forum is a space to discuss coding in PowerShell, and technical issues related to development.

- Question about a licensed SAPIEN product? Post here: Product Support for Registered Users
- Question about a trial SAPIEN product? Post here: Former and Future Customers - Questions
Post Reply
User avatar
wartech
Posts: 56
Last visit: Fri Nov 08, 2024 10:50 am

does not contain a method named 'op_Addition'

Post by wartech »

When I type the following code in a PowerShell console window (version 5.1) it works fine:
$locked = @()
$locked = Search-ADAccount -LockedOut

But when I run it in PowerShell Studio 2024 I get:

ERROR: Method invocation failed because [Microsoft.ActiveDirectory.Management.ADAccount] does not contain a method named 'op_Addition'.
Unlock-Backup.ps1 (21, 2): ERROR: At Line: 21 char: 2
ERROR: + $locked = Search-ADAccount -LockedOut
ERROR: + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR: + CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException
ERROR: + FullyQualifiedErrorId : MethodNotFound

Any idea what I'm doing wrong?
User avatar
Alexander Riedel
Posts: 8601
Last visit: Wed Dec 11, 2024 10:07 am
Answers: 23
Been upvoted: 43 times

Re: does not contain a method named 'op_Addition'

Post by Alexander Riedel »

This might help:
https://community.spiceworks.com/t/how- ... r/950539/4
It seems this happens if you attempt to fill or append an array from an empty object.
Alexander Riedel
SAPIEN Technologies, Inc.
User avatar
wartech
Posts: 56
Last visit: Fri Nov 08, 2024 10:50 am

Re: does not contain a method named 'op_Addition'

Post by wartech »

Thankyou for that.
Changed it to
  1. try
  2. {
  3.     $locked = (Search-ADAccount -LockedOut)
  4. }
  5. catch
  6. {
  7.     Write-Host "Error"
  8. }
and it works without ever displaying "Error".
It works, but I don't understand how :D
obnoxiousadult
Posts: 2
Last visit: Sun Dec 01, 2024 10:48 pm

Re: does not contain a method named 'op_Addition'

Post by obnoxiousadult »

The issue arises because in some contexts, $locked = @() creates an empty array, and PowerShell expects op_Addition to append results to it. This works fine in the standard console but behaves differently in PowerShell Studio, likely due to the execution environment interpreting the variable differently.
Post Reply