我在powershell中构建了一个备份交换邮箱的功能。用简单的术语......
New-MailboxExportRequest -Mailbox $ Mailbox.Name -FilePath“$ MailboxBackupLocation \ $($ Mailbox.Database) - $($ Mailbox.DisplayName) - $($ Mailbox.Alias) - (FULL).pst”
这很好用,但我还有一个小问题。我需要接下来运行powershell命令来删除用户的交换。
我需要的是powershell脚本等待它备份的每个邮箱,直到它完成,然后转移到下一个邮箱....或者至少保持功能完成直到所有邮箱备份都是完成......
哦,如果它可以显示出很棒的进展。
function Backup-Mailbox
{
Param
(
$LogFileRequest = "",
#$User = 'mdgeorge',
#[Parameter(Mandatory=$true)]
#[ValidateSet('UPN','Email')]
$MailBoxIdentity,
[Parameter(Mandatory=$true)]
[ValidateSet('Full','Inbox','SentItems','DeletedItems','Calendar','Contacts','Drafts','Journal','Tasks','Notes','JunkEmail','CommunicationHistory','Voicemail','Fax','Conflicts','SyncIssues','LocalFailures','ServerFailures')]
$BackupType,
$BulkImport = $true,
$CSVBulkImportFileandFullPathOrigional = "\\TRI-DC01\CreateADUsers$\CSV\Exchange\BulkBackupExchangeUserOrigional.csv",
$CSVBulkImportFileandFullPath = "\\TRI-DC01\CreateADUsers$\CSV\Exchange\BulkBackupExchangeUser.csv",
$RenameCSVBulkImportFileandFullPath = "\\TRI-DC01\CreateADUsers$\History\BulkExchangeUser-Backuped-$((get-date).toString(‘yyyyMMdd-HHmmss’)).csv",
$MailboxBackupLocation = "\\it-fs02\pst$"
)
#'Inbox','SentItems','DeletedItems','Calendar','Contacts','Drafts','Journal','Tasks','Notes','JunkEmail','CommunicationHistory','Voicemail','Fax','Conflicts','SyncIssues','LocalFailures','ServerFailures'
InitilizeLogging -LogFileRequest $LogFileRequest
InitilizationAD
InitilizeExchange
Function Backup-TheMailBox
{
Param
(
[Parameter(Mandatory=$true)]
[ValidateSet('Full','Inbox','SentItems','DeletedItems','Calendar','Contacts','Drafts','Journal','Tasks','Notes','JunkEmail','CommunicationHistory','Voicemail','Fax','Conflicts','SyncIssues','LocalFailures','ServerFailures')]
$BackupType,
[Parameter(Mandatory=$true)]
$MailBox,
[Parameter(Mandatory=$true)]
$MailboxBackupLocation
)
Switch ($BackupType)
{
'Full'
{
if (!(Test-Path "\\it-fs02\pst$\$($Mailbox.Database)-$($Mailbox.DisplayName)-$($Mailbox.Alias)-(FULL).pst"))
{
New-MailboxExportRequest -Mailbox $Mailbox.Name -FilePath "$MailboxBackupLocation\$($Mailbox.Database)-$($Mailbox.DisplayName)-$($Mailbox.Alias)-(FULL).pst"
}
else
{
New-MailboxExportRequest -Mailbox $Mailbox.Name -FilePath "$MailboxBackupLocation\$($Mailbox.Database)-$($Mailbox.DisplayName)-$($Mailbox.Alias)-(FULL)-$((get-date).toString(‘yyyyMMdd-HHmmss’)).pst"
}
}
'Contacts'
{
if (!(Test-Path "\\it-fs02\pst$\$($Mailbox.Database)-$($Mailbox.DisplayName)-$($Mailbox.Alias)-(CONTACTS).pst"))
{
New-MailboxExportRequest -Mailbox $Mailbox.Name -IncludeFolders "#Contacts#" -FilePath "$MailboxBackupLocation\$($Mailbox.Database)-$($Mailbox.DisplayName)-$($Mailbox.Alias)-(CONTACTS).pst"
}
else
{
New-MailboxExportRequest -Mailbox $Mailbox.Name -IncludeFolders "#Contacts#" -FilePath "$MailboxBackupLocation\$($Mailbox.Database)-$($Mailbox.DisplayName)-$($Mailbox.Alias)-(CONTACTS)-$((get-date).toString(‘yyyyMMdd-HHmmss’)).pst"
}
}
'SentItems'
{
if (!(Test-Path "\\it-fs02\pst$\$($Mailbox.Database)-$($Mailbox.DisplayName)-$($Mailbox.Alias)-(SENTITEMS).pst"))
{
New-MailboxExportRequest -Mailbox $Mailbox.Name -IncludeFolders "#SentItems#" -FilePath "\\it-fs02\pst$\$($Mailbox.Database)-$($Mailbox.DisplayName)-$($Mailbox.Alias)-(SENTITEMS).pst"
}
else
{
New-MailboxExportRequest -Mailbox $Mailbox.Name -IncludeFolders "#SentItems#" -FilePath "\\it-fs02\pst$\$($Mailbox.Database)-$($Mailbox.DisplayName)-$($Mailbox.Alias)-(SENTITEMS)-$((get-date).toString(‘yyyyMMdd-HHmmss’)).pst"
}
}
'Inbox'
{
if (!(Test-Path "\\it-fs02\pst$\$($Mailbox.Database)-$($Mailbox.DisplayName)-$($Mailbox.Alias)-(INBOX).pst"))
{
New-MailboxExportRequest -Mailbox $Mailbox.Name -IncludeFolders "#Inbox#" -FilePath "\\it-fs02\pst$\$($Mailbox.Database)-$($Mailbox.DisplayName)-$($Mailbox.Alias)-(INBOX).pst"
}
else
{
New-MailboxExportRequest -Mailbox $Mailbox.Name -IncludeFolders "#Inbox#" -FilePath "\\it-fs02\pst$\$($Mailbox.Database)-$($Mailbox.DisplayName)-$($Mailbox.Alias)-(INBOX)-$((get-date).toString(‘yyyyMMdd-HHmmss’)).pst"
}
}
'DeletedItems'
{
if (!(Test-Path "\\it-fs02\pst$\$($Mailbox.Database)-$($Mailbox.DisplayName)-$($Mailbox.Alias)-(DELETEDITEMS).pst"))
{
New-MailboxExportRequest -Mailbox $Mailbox.Name -IncludeFolders "#DeletedItems#" -FilePath "\\it-fs02\pst$\$($Mailbox.Database)-$($Mailbox.DisplayName)-$($Mailbox.Alias)-(DELETEDITEMS).pst"
}
else
{
New-MailboxExportRequest -Mailbox $Mailbox.Name -IncludeFolders "#DeletedItems#" -FilePath "\\it-fs02\pst$\$($Mailbox.Database)-$($Mailbox.DisplayName)-$($Mailbox.Alias)-(DELETEDITEMS)-$((get-date).toString(‘yyyyMMdd-HHmmss’)).pst"
}
}
'Calendar'
{
if (!(Test-Path "\\it-fs02\pst$\$($Mailbox.Database)-$($Mailbox.DisplayName)-$($Mailbox.Alias)-(CALENDAR).pst"))
{
New-MailboxExportRequest -Mailbox $Mailbox.Name -IncludeFolders "#Calendar#" -FilePath "\\it-fs02\pst$\$($Mailbox.Database)-$($Mailbox.DisplayName)-$($Mailbox.Alias)-(CALENDAR).pst"
}
else
{
New-MailboxExportRequest -Mailbox $Mailbox.Name -IncludeFolders "#Calendar#" -FilePath "\\it-fs02\pst$\$($Mailbox.Database)-$($Mailbox.DisplayName)-$($Mailbox.Alias)-(CALENDAR)-$((get-date).toString(‘yyyyMMdd-HHmmss’)).pst"
}
}
'Drafts'
{
if (!(Test-Path "\\it-fs02\pst$\$($Mailbox.Database)-$($Mailbox.DisplayName)-$($Mailbox.Alias)-(DRAFTS).pst"))
{
New-MailboxExportRequest -Mailbox $Mailbox.Name -IncludeFolders "#Drafts#" -FilePath "\\it-fs02\pst$\$($Mailbox.Database)-$($Mailbox.DisplayName)-$($Mailbox.Alias)-(DRAFTS).pst"
}
else
{
New-MailboxExportRequest -Mailbox $Mailbox.Name -IncludeFolders "#Drafts#" -FilePath "\\it-fs02\pst$\$($Mailbox.Database)-$($Mailbox.DisplayName)-$($Mailbox.Alias)-(DRAFTS)-$((get-date).toString(‘yyyyMMdd-HHmmss’)).pst"
}
}
'Journal'
{
if (!(Test-Path "\\it-fs02\pst$\$($Mailbox.Database)-$($Mailbox.DisplayName)-$($Mailbox.Alias)-(JOURNAL).pst"))
{
New-MailboxExportRequest -Mailbox $Mailbox.Name -IncludeFolders "#Journal#" -FilePath "\\it-fs02\pst$\$($Mailbox.Database)-$($Mailbox.DisplayName)-$($Mailbox.Alias)-(JOURNAL).pst"
}
else
{
New-MailboxExportRequest -Mailbox $Mailbox.Name -IncludeFolders "#Journal#" -FilePath "\\it-fs02\pst$\$($Mailbox.Database)-$($Mailbox.DisplayName)-$($Mailbox.Alias)-(JOURNAL)-$((get-date).toString(‘yyyyMMdd-HHmmss’)).pst"
}
}
'Tasks'
{
if (!(Test-Path "\\it-fs02\pst$\$($Mailbox.Database)-$($Mailbox.DisplayName)-$($Mailbox.Alias)-(TASKS).pst"))
{
New-MailboxExportRequest -Mailbox $Mailbox.Name -IncludeFolders "#Tasks#" -FilePath "\\it-fs02\pst$\$($Mailbox.Database)-$($Mailbox.DisplayName)-$($Mailbox.Alias)-(TASKS).pst"
}
else
{
New-MailboxExportRequest -Mailbox $Mailbox.Name -IncludeFolders "#Tasks#" -FilePath "\\it-fs02\pst$\$($Mailbox.Database)-$($Mailbox.DisplayName)-$($Mailbox.Alias)-(TASKS)-$((get-date).toString(‘yyyyMMdd-HHmmss’)).pst"
}
}
'Notes'
{
if (!(Test-Path "\\it-fs02\pst$\$($Mailbox.Database)-$($Mailbox.DisplayName)-$($Mailbox.Alias)-(NOTES).pst"))
{
New-MailboxExportRequest -Mailbox $Mailbox.Name -IncludeFolders "#Notes#" -FilePath "\\it-fs02\pst$\$($Mailbox.Database)-$($Mailbox.DisplayName)-$($Mailbox.Alias)-(NOTES).pst"
}
else
{
New-MailboxExportRequest -Mailbox $Mailbox.Name -IncludeFolders "#Notes#" -FilePath "\\it-fs02\pst$\$($Mailbox.Database)-$($Mailbox.DisplayName)-$($Mailbox.Alias)-(NOTES)-$((get-date).toString(‘yyyyMMdd-HHmmss’)).pst"
}
}
'JunkEmail'
{
if (!(Test-Path "\\it-fs02\pst$\$($Mailbox.Database)-$($Mailbox.DisplayName)-$($Mailbox.Alias)-(JUNKEMAIL).pst"))
{
New-MailboxExportRequest -Mailbox $Mailbox.Name -IncludeFolders "#JunkEmail#" -FilePath "\\it-fs02\pst$\$($Mailbox.Database)-$($Mailbox.DisplayName)-$($Mailbox.Alias)-(JUNKEMAIL).pst"
}
else
{
New-MailboxExportRequest -Mailbox $Mailbox.Name -IncludeFolders "#JunkEmail#" -FilePath "\\it-fs02\pst$\$($Mailbox.Database)-$($Mailbox.DisplayName)-$($Mailbox.Alias)-(JUNKEMAIL)-$((get-date).toString(‘yyyyMMdd-HHmmss’)).pst"
}
}
'CommunicationHistory'
{
if (!(Test-Path "\\it-fs02\pst$\$($Mailbox.Database)-$($Mailbox.DisplayName)-$($Mailbox.Alias)-(COMMUNICATIONHISTORY).pst"))
{
New-MailboxExportRequest -Mailbox $Mailbox.Name -IncludeFolders "#CommunicationHistory#" -FilePath "\\it-fs02\pst$\$($Mailbox.Database)-$($Mailbox.DisplayName)-$($Mailbox.Alias)-(COMMUNICATIONHISTORY).pst"
}
else
{
New-MailboxExportRequest -Mailbox $Mailbox.Name -IncludeFolders "#CommunicationHistory#" -FilePath "\\it-fs02\pst$\$($Mailbox.Database)-$($Mailbox.DisplayName)-$($Mailbox.Alias)-(COMMUNICATIONHISTORY)-$((get-date).toString(‘yyyyMMdd-HHmmss’)).pst"
}
}
'Voicemail'
{
if (!(Test-Path "\\it-fs02\pst$\$($Mailbox.Database)-$($Mailbox.DisplayName)-$($Mailbox.Alias)-(VOICEMAIL).pst"))
{
New-MailboxExportRequest -Mailbox $Mailbox.Name -IncludeFolders "#Voicemail#" -FilePath "\\it-fs02\pst$\$($Mailbox.Database)-$($Mailbox.DisplayName)-$($Mailbox.Alias)-(VOICEMAIL).pst"
}
else
{
New-MailboxExportRequest -Mailbox $Mailbox.Name -IncludeFolders "#Voicemail#" -FilePath "\\it-fs02\pst$\$($Mailbox.Database)-$($Mailbox.DisplayName)-$($Mailbox.Alias)-(VOICEMAIL)-$((get-date).toString(‘yyyyMMdd-HHmmss’)).pst"
}
}
'Fax'
{
if (!(Test-Path "\\it-fs02\pst$\$($Mailbox.Database)-$($Mailbox.DisplayName)-$($Mailbox.Alias)-(FAX).pst"))
{
New-MailboxExportRequest -Mailbox $Mailbox.Name -IncludeFolders "#Fax#" -FilePath "\\it-fs02\pst$\$($Mailbox.Database)-$($Mailbox.DisplayName)-$($Mailbox.Alias)-(FAX).pst"
}
else
{
New-MailboxExportRequest -Mailbox $Mailbox.Name -IncludeFolders "#Fax#" -FilePath "\\it-fs02\pst$\$($Mailbox.Database)-$($Mailbox.DisplayName)-$($Mailbox.Alias)-(FAX)-$((get-date).toString(‘yyyyMMdd-HHmmss’)).pst"
}
}
'Conflicts'
{
if (!(Test-Path "\\it-fs02\pst$\$($Mailbox.Database)-$($Mailbox.DisplayName)-$($Mailbox.Alias)-(CONFLICTS).pst"))
{
New-MailboxExportRequest -Mailbox $Mailbox.Name -IncludeFolders "#Conflicts#" -FilePath "\\it-fs02\pst$\$($Mailbox.Database)-$($Mailbox.DisplayName)-$($Mailbox.Alias)-(CONFLICTS).pst"
}
else
{
New-MailboxExportRequest -Mailbox $Mailbox.Name -IncludeFolders "#Conflicts#" -FilePath "\\it-fs02\pst$\$($Mailbox.Database)-$($Mailbox.DisplayName)-$($Mailbox.Alias)-(CONFLICTS)-$((get-date).toString(‘yyyyMMdd-HHmmss’)).pst"
}
}
'SyncIssues'
{
if (!(Test-Path "\\it-fs02\pst$\$($Mailbox.Database)-$($Mailbox.DisplayName)-$($Mailbox.Alias)-(SYNCISSUES).pst"))
{
New-MailboxExportRequest -Mailbox $Mailbox.Name -IncludeFolders "#SyncIssues#" -FilePath "\\it-fs02\pst$\$($Mailbox.Database)-$($Mailbox.DisplayName)-$($Mailbox.Alias)-(SYNCISSUES).pst"
}
else
{
New-MailboxExportRequest -Mailbox $Mailbox.Name -IncludeFolders "#SyncIssues#" -FilePath "\\it-fs02\pst$\$($Mailbox.Database)-$($Mailbox.DisplayName)-$($Mailbox.Alias)-(SYNCISSUES)-$((get-date).toString(‘yyyyMMdd-HHmmss’)).pst"
}
}
'LocalFailures'
{
if (!(Test-Path "\\it-fs02\pst$\$($Mailbox.Database)-$($Mailbox.DisplayName)-$($Mailbox.Alias)-(LOCALFAILURES).pst"))
{
New-MailboxExportRequest -Mailbox $Mailbox.Name -IncludeFolders "#LocalFailures#" -FilePath "\\it-fs02\pst$\$($Mailbox.Database)-$($Mailbox.DisplayName)-$($Mailbox.Alias)-(LOCALFAILURES).pst"
}
else
{
New-MailboxExportRequest -Mailbox $Mailbox.Name -IncludeFolders "#LocalFailures#" -FilePath "\\it-fs02\pst$\$($Mailbox.Database)-$($Mailbox.DisplayName)-$($Mailbox.Alias)-(LOCALFAILURES)-$((get-date).toString(‘yyyyMMdd-HHmmss’)).pst"
}
}
'ServerFailures'
{
if (!(Test-Path "\\it-fs02\pst$\$($Mailbox.Database)-$($Mailbox.DisplayName)-$($Mailbox.Alias)-(SERVERFAILURES).pst"))
{
New-MailboxExportRequest -Mailbox $Mailbox.Name -IncludeFolders "#ServerFailures#" -FilePath "\\it-fs02\pst$\$($Mailbox.Database)-$($Mailbox.DisplayName)-$($Mailbox.Alias)-(SERVERFAILURES).pst"
}
else
{
New-MailboxExportRequest -Mailbox $Mailbox.Name -IncludeFolders "#ServerFailures#" -FilePath "\\it-fs02\pst$\$($Mailbox.Database)-$($Mailbox.DisplayName)-$($Mailbox.Alias)-(SERVERFAILURES)-$((get-date).toString(‘yyyyMMdd-HHmmss’)).pst"
}
}
}
}
#$baseou = "OU=Companies,DC=triangle,DC=com"
#$MyDisabledUsers = Search-ADAccount -SearchBase $baseou -AccountDisabled | select name
#$MyListOfDisabledUsersThatHaveMailboxes = foreach ($mbx in $MyDisabledUsers) {Get-Mailbox $mbx.name -ErrorAction SilentlyContinue}
#foreach ($Mailboxes in $MyListOfDisabledUsersThatHaveMailboxes) { New-MailboxExportRequest -Mailbox $Mailboxes -FilePath "\\it-fs02\pst$\$($Mailboxes.Database)-$($Mailboxes.DisplayName)-$($Mailboxes.Alias).pst" }
#
If ($BulkImport)
{
$CSVfile = Import-Csv $CSVBulkImportFileandFullPath
foreach ($row in $CSVfile)
{
$MailBoxIdentity = $row.GivenName + ' ' + $row.Surname
If (Verify-UserHasMailbox -UsersAMAccountName $MailBoxIdentity)
{
$Mailbox = get-mailbox $MailBoxIdentity #'Michael George'
Backup-TheMailBox -BackupType $BackupType -MailBox $Mailbox -MailboxBackupLocation $MailboxBackupLocation
#Backup Options:
#https://www.packtpub.com/books/content/exchange-server-2010-windows-powershell-managing-mailboxes
#New-MailboxExportRequest -Mailbox User1 -IncludeFolders "#Contacts#" -filepath \\server\share\user1.pst
#New-MailboxExportRequest -Mailbox testuser -IncludeFolders "Sent Items" -FilePath \\contoso-ex01\export\testuser_sent.pst -ExcludeDumpster
#New-MailboxExportRequest -Mailbox testuser -ContentFilter {Received -lt "09/01/2010"} -FilePath \\contoso-ex01\export\testuser_archive.pst -ExcludeDumpster -IsArchive
#
}
# Do
#{
#Write-Progress -Activity "Mailbox Backup:" -PercentComplete (Get-MailboxExportRequest -Mailbox $Mailbox.Name | Get-MailboxExportRequestStatistics).PercentComplete -ErrorAction SilentlyContinue
#"Mailbox Backup: $((Get-MailboxExportRequest -Mailbox $Mailbox.Name | Get-MailboxExportRequestStatistics).PercentComplete)%" # -NoNewLine
#start-sleep 5
#cls
#}
#Until ( (Get-MailboxExportRequest -Mailbox $Mailbox.Name | Get-MailboxExportRequestStatistics).PercentComplete -eq 100)
}
}
Else
{
If (Verify-UserHasMailbox -UsersAMAccountName $MailBoxIdentity)
{
$Mailbox = get-mailbox $MailBoxIdentity #'Michael George'
Backup-TheMailBox -BackupType $BackupType -MailBox $Mailbox -MailboxBackupLocation $MailboxBackupLocation
}
}
End-ExchangeCleanup
do
{
$i = 1
}
Until (!(Test-FileLock -Path $CSVBulkImportFileandFullPath))
Move-Item -Path $CSVBulkImportFileandFullPath -Destination $RenameCSVBulkImportFileandFullPath -Force
Copy-Item -Path $CSVBulkImportFileandFullPathOrigional -Destination $CSVBulkImportFileandFullPath
"COMPLETED PROCESSING FILE: $RenameCSVBulkImportFileandFullPath" | Add-Content $Logfile.LogFile
}
就像我说要备份邮箱一样,这很有效......但是我真的需要它才能完成该功能,直到完全备份邮箱,这样下一步就可以安全地删除该帐户。
最后,如果它能显示出很棒的进展。