0
votes

I've never encountered this error in PowerShell before. Using an IDE it works, but it doesn't work in the Exchange shell window.

I can do this all day in C#, but for some reason it doesn't work in Powershell. I've seen a bunch of examples on the net saying I can't use the + in the .Add() method, as well as defining $results as an array and using += with that, but none of those work.

I'd appreciate it someone could fix my code, but also tell me why this isn't working so that I can not make this mistake again.

The error message is

Method invocation failed because [Microsoft.Exchange.Data.Directory.ADObjectId]
doesn't contain a method named 'op_Addition'.
At C:\Users\XXXX\Desktop\bleh1.ps1:168 char:3
+ $results.Add($serverName + "|" + $totalDbs.ToString() + "|" + $activeDbs.ToSt ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~‌​~~~~~~~~~~~~~~~~~~~~‌​~~~
    + CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

To reproduce you only need to populate $servers with 2 or more Exchange server names

$servers = @("server1", "server2");

[int] $mountedDBs = 0;
[int] $dismountedDBs = 0;
$preferenceOne = 0;
$preferenceTwo = 0;
$preferenceThree = 0;
$preferenceFour = 0;
[int] $displayLineCount = 0;
$results = New-Object System.Collections.ArrayList

foreach($server in $servers)
{
    if ($server -ne "")
    {
        [Array]$values = Get-MailboxDatabase -Server $server -Status;
        $serverTest = $server;
        Clear-Item Variable:mountedDBs
        Clear-Item Variable:dismountedDBs

        for($i = 0; $i -lt $values.Count; $i++)
        {
            #servername     totaldbs    activedbs   passivedbs  preferencecountlist     mounteddbs      dismounteddbs   dagname
            $serverName = $server;    
            $totaldbs = $values.Count;
            $temp1 = $values[$i].Mounted;

            if ($values[$i].Mounted -eq "true")
            {
                $mountedDBs = $mountedDBs + 1 | Out-Null;;
            }
            else
            {
                $dismountedDBs = $dismountedDBs + 1 | Out-Null;;
            }

            $activationPrefTemp = $values[$i].ActivationPreference;
            for($j = 0; $j -lt $activationPrefTemp.Count; $j++)
            {
                $temp1 = $activationPrefTemp[$j].ToString();
                if ($activationPrefTemp[$j].ToString().Contains($server.ToString()))
                {
                    $activationPref = $activationPrefTemp[$j];
                    [string]$activationPrefNumber = $activationPref;
                    [int] $index = $activationPrefNumber.IndexOf(",");
                    $activationPrefNumber = $activationPrefNumber.Remove(0, $index + 1);
                    $activationPrefNumber = $activationPrefNumber.Trim()
                    $index = $activationPrefNumber.IndexOf("]");
                    $activationPrefNumber = $activationPrefNumber.Remove($index);

                    Switch ($activationPrefNumber)    
                    {
                        1 {$preferenceOne = $preferenceOne + 1; break;}
                        2 {$preferenceTwo = $preferenceTwo + 1; break;}
                        3 {$preferenceThree = $preferenceThree + 1; break;}
                        4 {$preferenceFour = $preferenceFour + 1; break;}
                        default {$null}
                    }
                }
            }    
            $mountedDBs = $mountedDBs;
        }
        $activeDbs = $preferenceOne;
        $passiveDbs = $preferenceTwo + $preferenceThree + $preferenceFour;
        $results.Add($serverName  + "|" + $totalDbs.ToString() + "|" + $activeDbs.ToString() + "|" + $passiveDbs.ToString() + "|" + $preferenceOne + "," + $preferenceTwo + "," + $preferenceThree + "," + $preferenceFour + "|" + $mountedDBs + "|" + $dismountedDbs + "|" + $dagName);
        $displayLineCount = $displayLineCount + 1 | Out-Null;
        $preferenceOne = 0 | Out-Null;
        $preferenceTwo = 0 | Out-Null;
        $preferenceThree = 0 | Out-Null;
        $preferenceFour = 0 | Out-Null;
    }
}
1
Try doing a find and replace of ' | Out-Null;'. Does your situation improve? Could you include more details on what is actually going wrong here?Jacob Colvin
Please check which of the variables you're trying to concatenate there contains the offending object ($varname.GetType().FullName).Ansgar Wiechers

1 Answers

0
votes

This is what ended up working, thank you for the help @MacroPower . You deleted your post so I do not know how to give you credit for answering?

You were right, because the $server variable was not explicitly typed as a string, the .ToString() cast was necessary.

[int] $mountedDBs = 0;
[int] $dismountedDBs = 0;
$preferenceOne = 0;
$preferenceTwo = 0;
$preferenceThree = 0;
$preferenceFour = 0;
[int] $displayLineCount = 0;
$results = New-Object "System.Collections.Generic.List[String]"


foreach($server in $servers)
{
    if ($server -ne "")
    {
        [Array]$values = Get-MailboxDatabase -Server $server -Status;
        $serverTest = $server;
        Clear-Item Variable:mountedDBs
        Clear-Item Variable:dismountedDBs

        for($i = 0; $i -lt $values.Count; $i++)
        {
            #servername     totaldbs    activedbs   passivedbs  preferencecountlist     mounteddbs      dismounteddbs   dagname
            $serverName = $server; 
            $totaldbs = $values.Count;
            $temp1 = $values[$i].Mounted;

            if ($values[$i].Mounted -eq "true")
            {
                $mountedDBs = $mountedDBs + 1;
            }
            else
            {
                $dismountedDBs = $dismountedDBs + 1;
            }

            $activationPrefTemp = $values[$i].ActivationPreference;
            for($j = 0; $j -lt $activationPrefTemp.Count; $j++)
            {
                $temp1 = $activationPrefTemp[$j].ToString();
                if ($activationPrefTemp[$j].ToString().Contains($server.ToString()))
                {
                    $activationPref = $activationPrefTemp[$j];
                    [string]$activationPrefNumber = $activationPref;
                    [int] $index = $activationPrefNumber.IndexOf(",");
                    $activationPrefNumber = $activationPrefNumber.Remove(0, $index + 1);
                    $activationPrefNumber = $activationPrefNumber.Trim()
                    $index = $activationPrefNumber.IndexOf("]");
                    $activationPrefNumber = $activationPrefNumber.Remove($index);

                    Switch ($activationPrefNumber) 
                    {
                        1 {$preferenceOne = $preferenceOne + 1; break;}
                        2 {$preferenceTwo = $preferenceTwo + 1; break;}
                        3 {$preferenceThree = $preferenceThree + 1; break;}
                        4 {$preferenceFour = $preferenceFour + 1; break;}
                        default {$null}
                    }
                }
            }           
            $mountedDBs = $mountedDBs;
        }
        $activeDbs = $preferenceOne;
        $passiveDbs = $preferenceTwo + $preferenceThree + $preferenceFour;

        $results.Add($serverName.ToString()  + "|" + $totalDbs.ToString() + "|" + $activeDbs.ToString() + "|" + $passiveDbs.ToString() + "|" + $preferenceOne + "," + $preferenceTwo + "," + $preferenceThree + "," + $preferenceFour + "|" + $mountedDBs + "|" + $dismountedDbs + "|" + $dagName);
        $displayLineCount = $displayLineCount + 1 | Out-Null;
        $preferenceOne = 0; 
        $preferenceTwo = 0;
        $preferenceThree = 0;
        $preferenceFour = 0;
    }