locked
If-Else to search for a disconnected mailbox throws an error RRS feed

  • Question

  • Im using If-Else condition to search for a disconnected mailbox based on a user typing for the name he wants to search code below. It does what is expected as it can search for the nearest match name however I get multiple lines of error before the result is displayed. I just want to know why those error comes up, whats the cause of it? 

    Error
    ============

    Method invocation failed because [System.Management.Automation.PSObject] doesn't contain a method named 'op_Addition'.
    At D:\Scripts\AriesN\Search_DisconnectMbx_2.ps1:18 char:19
    +         $result += <<<<  Get-MailboxDatabase -Server $line | Get-MailboxStatistics | where {$_.disconnectdate -eq $nu
    ll -and $_.displayname -like "*" + $user_input  + "*"} | select displayname,servername,database
        + CategoryInfo          : InvalidOperation: (op_Addition:String) [], RuntimeException
        + FullyQualifiedErrorId : MethodNotFound

    Exchange 2007 Posh script
    ============

       

    ##search for all Exchange servers in our environment
    $MbxServer = Get-ExchangeServer | where {($_.serverrole -eq "Mailbox") -and ($_.Name -like "*PCMMGMB*" -and $_.Name -notlike "MCO*" -and $_.Name -notlike "*CAY*")} | ForEach-Object {$_.Name} 

    ##user types in the name to search for a nearest match
    $user_input = read-host "Type name to search"

    if ($user_input -eq "$null")
        {
            write-host "Please key in a valid string to search and run the script again"
        }


    else
        {
            Foreach ($line in $MbxServer)
            {
            $result += Get-MailboxDatabase -Server $line | Get-MailboxStatistics | where {$_.disconnectdate -eq $null -and $_.displayname -like "*" + $user_input  + "*"} | select displayname,servername,database
            }

        }
     $result

    ==================


    PoSH newbie, BaSH Oldie

    Tuesday, July 18, 2017 8:39 PM

All replies

  • Hi,

    I find this sentence “Method invocation failed because [System.Management.Automation.PSObject] doesn't contain a method named 'op_Addition'” in your error message. This error occurred because PowerShell was trying to collect collection of objects/custom objects and breaks. I modified your script and tested successfully in my environment, here is the script:

    $MbxServer = Get-ExchangeServer | where {($_.serverrole -like "*Mailbox*") -and ($_.Name -like "*PCMMGMB*" -and $_.Name -notlike "MCO*" -and $_.Name -notlike "*CAY*")}
    
    ##user types in the name to search for a nearest match
    $user_input = read-host "Type name to search"
    $results = @()
    if ($user_input -eq "$null")
        {
            write-host "Please key in a valid string to search and run the script again"
        }
    
    else
        {
            Foreach ($line in $MbxServer)
            {
            $result =  Get-MailboxStatistics $user_input| where {$_.disconnectdate -eq $null}
            $results += New-Object PsObject -Property @{
                        displayname = $result.displayname
                        servername = $result.servername
                        database =$result.database
                }
            }
        }
    $results | Select displayname,servername,database

    Best Regards,

    Manu Meng
    TechNet Community Support


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Wednesday, July 19, 2017 8:17 AM
    Moderator
  • I haven't tried your suggestion yet, but after modifying my script a bit (add a variable that would contain the array result in my ELSE statement (see below updated script) 

    I no longer have this error message however what I noticed is that my search result varies. So in my input if i type certain name it would find it and sometimes it wont. I verified that all the mailbox i am searching are indeed disabled. Are you aware of this type of behavior of Powershell? i just dont get the logic why it would have different results when searching for a disconnected mailbox.

    +++++++++++++++++++++

    Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin -ErrorAction SilentlyContinue

    $MbxServer = Get-ExchangeServer | where {($_.serverrole -eq "Mailbox") -and ($_.Name -like "*PCMMGMB*" -and $_.Name -notlike "MCO*" -and $_.Name -notlike "*CAY*")} | ForEach-Object {$_.Name} 

    $result = @()

    $user_input = read-host "Type name to search"

    if ($user_input -eq "$null")
        {
            write-host "Please key in a valid string to search and run the script again"
        }
    else
        {
            Foreach ($line in $MbxServer)
            {
            $result += Get-MailboxDatabase -Server $line | Get-MailboxStatistics | where {$_.disconnectdate -eq $null -and $_.displayname -like "*" + $user_input  + "*"} | select displayname,servername,database
            }
       
        }

    $result

    ++++++++++++++++++++++++++++++


    PoSH newbie, BaSH Oldie

    Friday, July 28, 2017 7:42 PM
  • I haven't tried your suggestion yet, but after modifying my script a bit (add a variable that would contain the array result in my ELSE statement (see below updated script) 

    I no longer have this error message however what I noticed is that my search result varies. So in my input if i type certain name it would find it and sometimes it wont. I verified that all the mailbox i am searching are indeed disabled. Are you aware of this type of behavior of Powershell? i just dont get the logic why it would have different results when searching for a disconnected mailbox.

    +++++++++++++++++++++

    Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin -ErrorAction SilentlyContinue

    $MbxServer = Get-ExchangeServer | where {($_.serverrole -eq "Mailbox") -and ($_.Name -like "*PCMMGMB*" -and $_.Name -notlike "MCO*" -and $_.Name -notlike "*CAY*")} | ForEach-Object {$_.Name} 

    $result = @()

    $user_input = read-host "Type name to search"

    if ($user_input -eq "$null")
        {
            write-host "Please key in a valid string to search and run the script again"
        }
    else
        {
            Foreach ($line in $MbxServer)
            {
            $result += Get-MailboxDatabase -Server $line | Get-MailboxStatistics | where {$_.disconnectdate -eq $null -and $_.displayname -like "*" + $user_input  + "*"} | select displayname,servername,database
            }
       
        }

    $result

    ++++++++++++++++++++++++++++++


    PoSH newbie, BaSH Oldie

    So I suggest you to try my script. It does satisfy your requirment. 

    Best Regards,

    Manu Meng
    TechNet Community Support


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Monday, July 31, 2017 1:42 AM
    Moderator
  • Tried your suggestion this time, but have to change the $result variable as it would throw an error if i simply use "$result =  Get-MailboxStatistics $user_input| where {$_.disconnectdate -eq $null}" 

    Since the $user_input would try to find the exact account of what I typed in. It wont do a nearest match. Thus I modified it as below. 

    However this one wont throw an error and no result as well. But the result is inside the $result var. If i typed $result it just show the column specified in the PSObject.

    SCRIPT
    =================

    $MbxServer = Get-ExchangeServer | where {($_.serverrole -eq "Mailbox") -and ($_.Name -like "*PCMMGMB*" -and $_.Name -notlike "MCO*" -and $_.Name -notlike "*CAY*")} | ForEach-Object {$_.Name} 

    $results = @()

    $user_input = read-host "Type name to search"

    if ($user_input -eq "$null")
        {
            write-host "Please key in a valid string to search and run the script again"
        }


    else
        {
            Foreach ($line in $MbxServer)
            {
            $result += Get-MailboxStatistics -Server $line | where {$_.disconnectdate -eq $null -and $_.displayname -like "*" + $user_input  + "*"} 
            $results += New-Object PSobject -Property @{
                Displayname = $result.displayname
                ServerName = $result.servername
                Database = $result.database}
            #$result2 += $MbxObj }
            }
       
        }
     $results | Select displayname,servername,database

    }


    PoSH newbie, BaSH Oldie

    Wednesday, August 2, 2017 6:31 PM