无论您如何意外删除Exchange中的某个人的邮箱,您都可以使用PowerShell ( PoSh ) 将其恢复。关键是在您意识到邮箱已被删除后立即执行此操作。
当我们意识到我们不小心做到了这一点时,这是一个令人心碎的时刻。如果您只删除了几封电子邮件,我们也可以向您展示如何在 Office 365 中恢复已删除的电子邮件(how to recover deleted emails in Office 365)。
在开始之前,请注意,这是为了在 Office 365 环境和Exchange 2010的(Exchange 2010)Exchange Online中恢复已删除的邮箱。我们还假设您拥有使用所述方法的管理权限,并且已经完成了一些基本的 PowerShell 脚本(basic PowerShell scripting)。
删除邮箱时会发生什么?(What Happens When a Mailbox Is Deleted?)
邮箱将移动到Exchange Online中的(Exchange Online)Azure Active Directory (AD) 回收站或 Exchange 2010 中的断开连接的邮箱(Disconnected Mailboxes )目录。
根据现有的保留政策,它可能会在那里放置长达 30 天。这给了我们一个宽限期来认识到我们的错误并从中恢复。
在 Exchange Online 中恢复已删除的邮箱(Restore a Deleted Mailbox In Exchange Online)
通过 PowerShell 连接到 Exchange Online(Connect To Exchange Online Via PowerShell)
在您的本地会话中,打开Powershell控制台并使用以下命令将您的登录凭据设置到变量中。
$userCredential
这使得以后使用其他脚本更容易。
$userCredential = get-Credential
将打开一个窗口,您可以在其中输入用于管理Exchange Online的用户名和密码。
接下来,设置执行策略级别,以便我们可以在会话中实际执行操作。这允许我们运行未签名的命令。但是您可能还想学习签署 PowerShell 脚本的最佳实践(best practices for signing PowerShell scripts)。
set-ExecutionPolicy Unrestricted
出现提示时,按Y表示是。
现在我们将创建变量$session用于打开本地计算机和Exchange Online之间的连接。
$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $userCredential -Authentication Basic -AllowRedirection
Powershell-liveid是您的Office站点的 ID。通常这是贵公司名称的一些变体。
使用以下命令在Exchange Online上打开(Exchange Online)PoSh会话:
import-PSSession $Session -DisableNameChecking
现在我们直接在Exchange Online中使用(Exchange Online)PowerShell,即使我们坐在自己的计算机上。
使用 PowerShell 恢复已删除的邮箱(Restore a Deleted Mailbox With PowerShell)
下一部分非常简单。建立联系将花费我们更长的时间。
我们所要做的就是运行如下所示的 cmdlet:
undo-SoftDeletedMailbox [email protected] -WindowsLiveID [email protected] -Password (ConvertTo-SecureString -String 'newpassword' -AsPlainText -Force)
[email protected]的两个实例更改为我们要还原的邮箱的适当邮箱名称和Windows LiveID 。请注意,它们可能不相同。
我们还必须为邮箱设置一个新密码。将脚本中的newpassword(newpassword)更改为您选择的密码。您需要将其转发给用户,并要求他们在下次登录时更改密码。
最后,使用下一个 cmdlet 进行检查以确保它有效。
get-Mailbox [email protected]
如果已还原,则 cmdlet 将返回有关已还原邮箱的信息。如果返回错误,请再次执行命令并确保使用正确的邮箱和Windows LiveID。
如果这仍然不起作用,那么我们需要从系统备份中恢复邮箱。有许多类型的系统备份,所以这超出了我们今天所做的范围。
完成后,我们必须关闭PoSh会话。我们运行的PoSh会话的数量是有限的。让它打开使用这些会话之一。除非我们关闭它,否则我们将不得不等待会话超时,然后才能开始另一个会话。
remove-PSSession $Session
就是这样。如果再次发生,您甚至可能希望将其编写到PowerShell 模块中以便于使用。(PowerShell Module)
在 Exchange 2010 中恢复已删除的邮箱(Restore a Deleted Mailbox In Exchange 2010)
这不适用于Office 365或混合环境。它仅适用于本地Exchange 2010。
在邮件服务器上,打开Exchange 管理控制台(Exchange Management Console )(EMC)。
导航到收件人配置(Recipient Configuration )>已断开邮箱(Disconnected Mailbox)。我们应该在那里看到用户的邮箱。
如果我们不这样做,那么邮箱数据库清理过程还没有发生。没关系,我们可以强制。
- 以管理员身份打开Exchange 命令行管理程序(Exchange Management Shell )( EMS )。这是我们可以执行特定于 Exchange 的 PowerShell(Exchange-specific PowerShell)工作的地方。
- 现在我们输入 cmdlet:
Get-MailboxDatabase | Clean-MailboxDatabase
- 完成后,让我们回到EMC并右键单击Disconnected Mailbox,然后单击Refresh。
- 我们现在应该看到那里的邮箱,它和另一个最近被删除的邮箱一起。
Connect-Mailbox -Identity "username" -Database "Mailbox Database" -User "username"
- Username是此人的Windows帐户名称(如Test User),Mailbox Database是在 EMC的Disconnected Mailbox窗口中 其名称旁边列出的数据库名称。
- 刷新断开的邮箱(Disconnected Mailbox),您应该会看到他们的邮箱不再显示在那里。导航到收件人配置(Recipient Configuration )>邮箱(Mailbox )并确保用户的邮箱在那里。
检查它以确保所有设置都正确,例如电子邮件地址和别名。如果它看起来不错,下次用户打开他们的Outlook时,一切都会原样存在。
邮箱已恢复(Mailbox Restored)
这就是如何使用PowerShell来还原Exchange Online和Exchange 2010中的邮箱。如果你有一个混合环境,它会有点复杂,但可以做到。
只要知道存在这些不同的 cmdlet,您就可以很好地使用Exchange,无论是什么版本或配置。
How to Restore a Deleted Mailbox with PowerShell
No matter how you accidentally dеleted a person’s mailbox in Exchange, you can probably restore it using PowerЅhell (PoSh). The key is doing it аs soon as you realize the mailbox haѕ been deleted.
It’s a heart-jumped-into-my-throat moment when we realize we’ve accidentally done this. If you’ve only deleted a few email messages, we can show you how to recover deleted emails in Office 365 too.
Before we begin, note that this is meant for restoring deleted mailboxes in Exchange Online for the Office 365 environment and Exchange 2010. We’re also assuming that you have the administrative rights to use the methods described and have done some basic PowerShell scripting.
What Happens When a Mailbox Is Deleted?
The mailbox gets moved to either the Azure Active Directory (AD) recycle bin in Exchange Online or the Disconnected Mailboxes directory in Exchange 2010.
It may sit there for up to 30 days depending on the retention policies that are in place. That gives us a grace period to recognize our mistake and recover from it.
Restore a Deleted Mailbox In Exchange Online
Connect To Exchange Online Via PowerShell
In your local session, open the Powershell console and use the following command to set your login credentials into the variable.
$userCredential
This makes it easier to work with other scripts later on.
$userCredential = get-Credential
A window will open where you can enter your username and password for managing Exchange Online.
Next, set the execution policy level so that we can actually do things in our session. This allows us to run our unsigned commands. But you may want to learn best practices for signing PowerShell scripts too.
set-ExecutionPolicy Unrestricted
When prompted, press Y for Yes.
Now we’ll create the variable $session to be used to open a connection between the local computer and Exchange Online.
$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $userCredential -Authentication Basic -AllowRedirection
Powershell-liveid is the id of your Office site. Usually this is some variation of your company’s name.
Use the following command to open a PoSh session on Exchange Online:
import-PSSession $Session -DisableNameChecking
Now we’re working with PowerShell directly in Exchange Online, even though we’re sitting at our own computer.
Restore a Deleted Mailbox With PowerShell
The next part is super easy. Making the connection will have taken us far longer.
All we have to do is run the cmdlet shown below:
undo-SoftDeletedMailbox [email protected] -WindowsLiveID [email protected] -Password (ConvertTo-SecureString -String 'newpassword' -AsPlainText -Force)
Change both instances of [email protected] to the appropriate mailbox name and Windows LiveID of the mailbox we want to restore. Note that they may not be the same.
We also have to set a new password for the mailbox. Change newpassword in the script to the password of your choice. You’ll need to relay that to the user and ask them to change their password the next time they log in.
Finally, use the next cmdlet to check to make sure that this worked.
get-Mailbox [email protected]
If it has been restored, the cmdlet will return information about the restored mailbox. If it returns an error, go through the commands again and ensure that the correct mailbox and Windows LiveID are being used.
If that still doesn’t work, then we’ll need to restore the mailbox from a system backup. There’s many types of system backups, so that’s beyond the scope of what we’re doing today.
When we’re done, we must close the PoSh session. The number of PoSh sessions we have running is limited. Leaving it open uses one of those sessions. Unless we close it, we’ll have to wait for the session to time-out before we can start another one.
remove-PSSession $Session
That’s it. You may even want to script this into a PowerShell Module for ease of use if it happens again.
Restore a Deleted Mailbox In Exchange 2010
This doesn’t work for Office 365 or hybrid environments. It only works for Exchange 2010 on-premises.
On the mail server, open the Exchange Management Console (EMC).
Navigate to Recipient Configuration > Disconnected Mailbox. We should see the user’s mailbox there.
If we don’t, then the mailbox database cleaning process hasn’t happened yet. That’s OK, we can force it.
- Open the Exchange Management Shell (EMS) as administrator. This is where we can do Exchange-specific PowerShell work.
- Now we enter the cmdlet:
Get-MailboxDatabase | Clean-MailboxDatabase
- Once that is done, let’s go back to the EMC and right-click on Disconnected Mailbox, then click on Refresh.
- We should see the mailbox there now, and it is, along with another one that was deleted recently.
- Go back to EMS and enter the cmdlet:
Connect-Mailbox -Identity "username" -Database "Mailbox Database" -User "username"
- Username is the person’s Windows account name (like Test User), and Mailbox Database is the name of the database listed beside their name in the Disconnected Mailbox window in EMC.
- Refresh Disconnected Mailbox and you should see that their mailbox is no longer displayed there. Navigate to Recipient Configuration > Mailbox and ensure the user’s mailbox is there.
Check it to make sure all the settings are correct, like e-mail address and alias. If it looks good, the next time the user opens their Outlook, everything will be there just as it was.
Mailbox Restored
That’s how to use PowerShell to restore mailboxes in Exchange Online and Exchange 2010. If you’ve got a hybrid environment, it’s a bit more complicated but it can be done.
Just knowing that these different cmdlets exists puts you in a good position to work with Exchange, no matter what version or configuration.