2017-04-11 42 views
0

我從來沒有遇到過這個錯誤之前方法。使用它的IDE工作,但它不能在Exchange外殼窗口中工作。方法調用失敗,因爲[Microsoft.Exchange.Data.Directory.ADObjectId]不包含在PowerShell中名爲「op_Addition」

我可以在C#做這一切的一天,但由於某些原因,它不PowerShell中工作。我看到說我不能在.Add()方法使用+,以及定義$results爲數組,並使用+=與一幫在網上例子,但是沒有那些工作。

我會感激有人可以解決我的代碼,但也告訴我,這是爲什麼不工作,所以,我不能再犯這樣的錯誤。

的錯誤消息是

 
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 

要重現你只需要填充$servers有2個或多個Exchange服務器名稱

$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; 
    } 
} 
+0

嘗試做一個查找和「替代|外空;」。你的情況有所改善嗎?你能否在這裏包括更多關於實際發生問題的細節? –

+0

請檢查您嘗試連接的變量中包含違規對象('$ varname.GetType()。FullName'))。 –

回答

0

這是結束了工作,感謝你的幫助@ MacroPower。你刪除了你的帖子,所以我不知道如何給你信用回答?

你是對的,因爲$服務器變量並沒有顯式類型爲字符串,則的ToString()投放是必要的。

[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; 
    } 
相關問題