op_Addition problem

This topic contains 2 replies, has 2 voices, and was last updated by  Vandrey Trindade 4 weeks, 1 day ago.

  • Author
    Posts
  • #91957

    Vandrey Trindade
    Participant

    Hi,
    I'm trying to add to a variable the result of three variables and it keeps giving me this error:
    "Method invocation failed because [System.Management.Automation.PSObject] doesn't contain a method named 'op_Addition'."

    Here is the code:

    $Domain = (Get-ADDomain).DistinguishedName
    $Query = "Support"
    $DistinguishedNames = @()
    $DistinguishedNames = Get-ADuser -Filter "Name -like '*$Query*'" -SearchBase $Domain | Select-Object DistinguishedName
    $DistinguishedNames += Get-ADGroup -Filter "Name -like '*$Query*'" -SearchBase $Domain | Select-Object DistinguishedName
    $DistinguishedNames += Get-ADOrganizationalUnit -Filter "Name -like '*$Query*'" -SearchBase $Domain | Select-Object DistinguishedName

    The problem is that I managed doing it a lot of times and now I just can't get it done...
    What I'm trying to do is to keep in only one variable all the results found on a query for the word "Support" on a domain, something like this:

    DistinguishedName
    – – – – – – – – –
    CN=G_Support,OU=Support,OU=Groups,OU=Organization,DC=domain,DC=local
    CN=SupportUser,OU=Support,OU=Users,OU=Organization,DC=domain,DC=local
    OU=Support,OU=Groups,OU=Organization,DC=domain,DC=local

    PS: I know that using Get-ADbject I can manage it better, but just wanted to know if there is a way because on Internet I found that the answer for it is just put this "$DistinguishedNames = @()" in the code.

  • #91958

    Sam Boutros
    Participant

    Line 4

    $DistinguishedNames = Get-ADuser ....
    

    undoes the benefit of line 3

    $DistinguishedNames = @()
    

    Line 4 re-declares $DistinguishedNames as System.Object (Selected.Microsoft.ActiveDirectory.Management.ADUser) instead of System.Array which is what line 3 does.

    To fix the script, change line 4 to read

    $DistinguishedNames += Get-ADuser ....
    

    For more information see https://superwidgets.wordpress.com/2018/01/01/practical-guide-to-powershell-arrays/

    • #91981

      Vandrey Trindade
      Participant

      Sam Boutros,

      Nice! Thanks a lot for the fast reply and attention!

You must be logged in to reply to this topic.