我之前从未在PowerShell中遇到此错误。使用IDE它可以工作,但它在Exchange shell窗口中不起作用。
我可以整天用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
要重现,您只需要使用2个或更多Exchange服务器名称填充$servers
$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)
这就是最终的工作,谢谢你帮助@MacroPower。你删除了你的帖子,所以我不知道如何给你回信?
你是对的,因为$ server变量没有显式地输入为字符串,所以.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;
}