使用PowerShell批量删除邮箱-Exchange 2016

时间:2018-12-05 19:45:56

标签: powershell exchange-server

我正在尝试使用PowerShell从我们的云托管Exchange服务器中删除100多个邮箱,但是我一直收到此错误:

无法执行操作,因为在'...'上找不到对象'...'

空白显然是数组和我们的Exchange服务器中的对象。我正在从标题为'mailbox'的.csv文件中拉出对象(我已经尝试了电子邮件地址和显示名称)。我用过Disable-Mailbox和Remove-Mailbox,无济于事。

这是我的剧本:

Import-Csv "C:\temp\array.csv" | ForEach-Object {
    Remove-Mailbox -identity $_.mailbox -confirm:$false
}

我整天都在Google上搜索,但似乎找不到导致这些对象(实际上是我们Exchange环境中的邮箱)的原因。

1 个答案:

答案 0 :(得分:0)

我假设正在发生的事情是您的mailbox的CSV列与邮箱的identity不匹配

您可以使用条件来确保您在实际的邮箱上运行Remove-mailbox

#assuming that "array.csv" has a column named mailbox
Import-Csv "C:\temp\array.csv" | ForEach-Object {
    if((Get-Mailbox -Identity $_.Alias | Measure).Count -eq 1)
    {
        Write-Host "Removing "$_.mailbox -ForegroundColor Green
        Remove-Mailbox -Identity $_.mailbox -Confirm:$false #Here you should add -Whatif before running
    }
    else
    {
        Write-Host $_.mailbox " not found or not unique" -ForegroundColor Yellow
    }

}

或者,如果您确信查询是正确的,则可以通过坚持使用原始脚本并将-ErrorActionRemove-Mailbox放在Import-Csv "C:\temp\array.csv" | ForEach-Object { Remove-Mailbox -identity $_.mailbox -confirm:$false -ErrorAction SilentlyContinue } 上来忽略该错误

例如:

mailbox

要测试csv列identity是否与邮箱的Get-Mailbox相匹配,可以运行简单的Import-Csv "C:\temp\array.csv" | ForEach-Object { Get-Mailbox -identity $_.mailbox }

,product,p_date,region,p_value,c_date,eom
0,CD Short-Term WAMP,2010-01-01,MA,0.8763918845487475,201812,2018-12-31
1,CD Short-Term WAMP,2010-01-08,MA,0.8600652449166932,201812,2018-12-31
2,CD Short-Term WAMP,2010-01-15,MA,0.8593079486202981,201812,2018-12-31