EXCH - Export d'une BAL via Script EMS

Exemple



###################################################################################################################
#
###################################################################################################################
#
# SCRIPT PREAMBLE :
# !!!!!!!!!!!!!!!!!
#
# SCRIPT NAME     :     EXEMPLE--EXPORT-Bal-to-PST.ps1
# VERSION         :     1.0
#
# REDACTOR OF SCRIPT : Patrice GUILLOUX - EMAIL : PGUILLOUX@OUTLOOK.COM
#
#
# GOAL : Use to export the content of  mailbox to a PST 
#        The export file will be place in $ExportShareFile destination
#
#
#
# How to use this script :
# -----------------------
# 1 - Change the value of $ExportShareFile if needed
# 2 - Launch Powershell and move to the folder where you have place the script
# 3 - Launch .\EXEMPLE--EXPORT-Bal_to-PST.ps1
# 4 - Provide the login of the mailbox when asked
# 5 - Choose an export option between the fourteen options
# 6 - The export will be Queued - Type GET-MailboxExportRequest | ft -Autosize to view the status
# 7 - When status is "Completed", find the exported PST file that will be placed in $ExportShareFile destination
#     
#
# IMPORTANT : you will have to modify some variables to adapt to your  infrastructure
#
#
#
###################################################################################################################
#
###################################################################################################################
#
# PREREQUISITES :
# ---------------
#
# 1 - In order to Import or export, you need to have the Exchange role : 
#    In french "Rôle d’importation et d’exportation de boîtes aux lettres"
#    In english "Mailbox Import Import" role
#    Tips : "Organization Management" group has this role !!!
#    If need , use the following command : 
#      New-ManagementRoleAssignment –Role "Mailbox Import Export" –User login-to-grant-this-role
#
# 2 - IMPORTANT : the share that will be used has destination had to be configured with some access rights :
#     --> The Exchange Trusted Subsystem need read/write permission to the destination network share (share and NTFS persmissions)
#     If you don't do that, you will receive a message that inform you that MS Exchange is unable to establish a connection to the target mailbox
#
#
# 3 - IMPORTANT for selection 2 (export by time range)
#     First, be sure that in regional settings, is English (United States) -
#     Open Control Panel --> Region and Language --> Formats --> select "English (United States)" 
#     --> Short date will be then M/d/yyyy
#
#
#
###################################################################################################################
#
# === STARTING THE SCRIPT EXECUTION ===============================================================================
#
###################################################################################################################
###################################################################################################################
# =====  FIRST : PREPARING THE ENVIRONNEMENT FOR SCRIPT EXECUTION =================================================
###################################################################################################################
###################################################################################################################
#
#
# Clean and prepare the screen first :
# ====================================
#
clear-host
#
Write-host "================================================================================================================================"
Write-host ""
#
#
# Provide the name of your Exchange Mailbox Server from where you will export the BAL :
# =====================================================================================
$MBXEXCHSRV = "Your-Exchange-FQDN-Server-Name"
#
# Provide the name of a server where you will put the export the BAL (if different from the Exchange mailbox server) :
# ====================================================================================================================
# $OtherServer = "Your-Other-Server-FQDN-Server-Name"
#
# Provide the login of the mailbox to export :
# ============================================
#
# This variable will be asked interactively to define the login of the user from which you want to export the mailbox content
$username = Read-Host 'Provide login of user'
Write-host ""
#
# Define the default export type :
# ================================

# $DefaultExportType = "All"
#
#
# Defining global environnement :
# ===============================
#
# This variable will define where you will export the mailbox content
# The following variable will define that the export will be place on the local Exchange mailboxes server
$ExportShareFile = "\\$MBXEXCHSRV\c$\" + $username + "_001.pst"
# if you want to place the export mailbox on another server than the Mailbox server comment the line above and uncomment the line below plus the corresponding $OtherServer variable above
# $ExportShareFile = "\\$OtherServer\c$\" + $username + "_001.pst"
#
Write-host -fore Cyan "The export mailbox will be created in : " $ExportShareFile

#
# In Exchange 2010 the Set-AdServerSettings  cmdlet is used to manage the AD environment in the current Exchange Management Shell (EMS) 
# session.  In Exchange 2007 there was a variable called AdminSessionADSettings  for the same purpose.  Exchange admins normally use 
# the Set-AdServerSettings cmdlet to change a session’s view scope, so that they can see objects in multiple domains.  
# By default EMS places the focus on the local domain. 
Set-ADServerSettings -ViewEntireForest $true
#
#
###################################################################################################################
###################################################################################################################
# =====  SECOND : DEFINING THE DIFFERENT FUNCTIONS IN ORDER TO BE AVAILABLE TO THE MAIN EXECUTION PART BELOW ======
###################################################################################################################
###################################################################################################################
#
#
## IMPORTANT # Note that BadItemLimit  use several times in the following sections define the number of corrupt items that 
## you autorize to not migrate before the export operation will be aborted - You can change this value (but not more than 50)
#
# FUNCTION 1 --> AllMBX :
# !!!!!!!!!!!!!!!!!!!!!!!
#
Function AllMBX {
#
# Exporting alls items of the mailbox 
New-MailboxExportRequest -Mailbox $username  -filepath $ExportShareFile -BadItemLimit 25
#
# End of function AllMBX 
}
#
###################################################################################################################
#
# FUNCTION 2 --> ByTimeOffset :
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#
Function ByTimeOffset {
Write-host -Fore Yellow "First, be sure that  regional settings is <English (United States)> on $MBXEXCHSRV"
Write-host -Fore Yellow "For that, on $MBXEXCHSRV Server, open Control Panel --> Region and Language --> Formats --> select <English (United States)> "
Write-Host ""
# This variable will be asked interactively to define the login of the user from which you want to export the mailbox content
$startoffset = Read-Host 'Provide the number of days from which you want to retrieve Emails (default= 7) '
if ($startoffset -eq "") {$startoffset = 7} 
#
# This variable will be asked interactively to define the starting offset (in days) from where you want to export the mailbox content
$endoffset = Read-Host 'Provide the number of days until which you want to retrieve Emails (default= 1) '
if ($endoffset -eq "") {$endoffset = 1} #
#
[System.Reflection.Assembly]::LoadWithPartialName("System.Threading")
#
[System.Reflection.Assembly]::LoadWithPartialName("System.Globalization")

[System.Threading.Thread]::CurrentThread.CurrentCulture = [System.Globalization.CultureInfo]::CreateSpecificCulture("en-us")
#
# If you need snap-in (no need if already loaded), uncomment one of the following lines 
# Add-PSSnapin *exchange*
# add-PSSnapIn Microsoft.Exchange, Microsoft.Windows.AD

# Fixing the right format for the minimum date of Emails to export
$Start = (get-date).adddays(-$startoffset)
Write-host "Start date to filter : $Start"

# Fixing the right format for the maximum date of Emails to export
$End = (get-date).adddays(-$endoffset)
Write-host "End date to filter : $End"

# Fixing the contentfilter format for the New-MailboxExportRequest command
$DateFilter = "(Received -gt '"+$Start+"') -and (Received -lt '"+$End+"')"
# write-host "Value for Contentfilter : $DateFilter"

#
# Exporting the Email (mails will be exported in PST between the $start and $end date offset
New-MailboxExportRequest -Mailbox $username -ContentFilter $DateFilter -filepath $ExportShareFile -BadItemLimit 25
#
# End of function ByTimeOffset 
}
#
###################################################################################################################
#
# FUNCTION 3 --> OnlyInbox :
# !!!!!!!!!!!!!!!!!!!!!!!!!!
#
#
Function OnlyInbox {
#
# Fixing the filter for the New-MailboxExportRequest command
$FolderFilter = "#Inbox#"
#
# Exporting the alls items in the Inbox
New-MailboxExportRequest -Mailbox $username -IncludeFolders $FolderFilter -filepath $ExportShareFile -BadItemLimit 25
#
# End of function OnlyInbox 
}
#
###################################################################################################################
#
# FUNCTION 4 --> OnlySentItems :
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#
#
Function OnlySentItems {
#
# Fixing the filter for the New-MailboxExportRequest command
$FolderFilter = "#SentItems#"
#
# Exporting the alls items in the Sent folder
New-MailboxExportRequest -Mailbox $username -IncludeFolders $FolderFilter -filepath $ExportShareFile -BadItemLimit 25
#
# End of function OnlySentItems 
}
#
###################################################################################################################
#
# FUNCTION 5 --> OnlyTasks :
# !!!!!!!!!!!!!!!!!!!!!!!!!!
#
#
Function OnlyTasks {
#
# Fixing the filter for the New-MailboxExportRequest command
$FolderFilter = "#Tasks#"
#
# Exporting the alls items in the Tasks folder
New-MailboxExportRequest -Mailbox $username -IncludeFolders $FolderFilter -filepath $ExportShareFile -BadItemLimit 25
#
# End of function OnlyTasks 
}
#
###################################################################################################################
#
# FUNCTION 6 --> OnlyContacts :
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#
#
Function OnlyContacts {
#
# Fixing the filter for the New-MailboxExportRequest command
$FolderFilter = "#Contacts#"
#
# Exporting the alls items in the Contacts folder
New-MailboxExportRequest -Mailbox $username -IncludeFolders $FolderFilter -filepath $ExportShareFile -BadItemLimit 25
#
# End of function OnlyContacts 
}
#
###################################################################################################################
#
# FUNCTION 7 --> OnlyCalendar :
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#
#
Function OnlyCalendar {
#
# Fixing the filter for the New-MailboxExportRequest command
$FolderFilter = "#Calendar#"
#
# Exporting the alls items in the calendar
New-MailboxExportRequest -Mailbox $username -IncludeFolders $FolderFilter -filepath $ExportShareFile -BadItemLimit 25
#
# End of function OnlyCalendar
}
#
###################################################################################################################
#
# FUNCTION 8 --> FolderToExport :
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#
#
Function FolderToExport {
#
$CustomFolderFilter = Read-Host 'Type the folder to export - Be careful to the syntax - Note that subfolders will be included '
$FolderFilter = $CustomFolderFilter + "/*"
#
# Exporting the alls items in the provided folder
New-MailboxExportRequest -Mailbox $username -IncludeFolders $FolderFilter -filepath $ExportShareFile -BadItemLimit 25
#
# End of function FolderToExport
}
#
###################################################################################################################
#
# FUNCTION 9 --> StringInSubject :
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#
#
Function StringInSubject {
$ExportFilterString = Read-Host 'Type the string to search in the subject '
$ExportFilter = "*" + $ExportFilterString + "*"
# Write-host $ExportFilter
#
# Exporting alls items that has the string in the subject 
#
# TIPS -- IMPORTANT :
# +++++++++++++++++++
#
# The documentation states that the syntax for -ContentFilter is - {subject -like '*mystring*'}
# but it doesn't work when used with variable instead of direct string
# Then the syntx need to be :  -contentfilter "subject -like '$myvariable'" in order to work !!!!!
# See : http://stackoverflow.com/questions/4017423/cannot-use-variable-with-get-user-filter-in-exchange-management-console
#
New-MailboxExportRequest -Mailbox $username -ContentFilter "Subject -like '$ExportFilter'" -filepath $ExportShareFile -BadItemLimit 25
#
# End of function StringInSubject
}
#
###################################################################################################################
#
# FUNCTION 10 --> StringInBody :
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#
#
Function StringInBody {
$ExportFilterString = Read-Host 'Type the string to search in the body '
$ExportFilter = "*" + $ExportFilterString + "*"
#
# Exporting alls items that has the string in the body
#
# TIPS -- IMPORTANT :
# +++++++++++++++++++
#
# The documentation states that the syntax for -ContentFilter is - {subject -like '*mystring*'}
# but it doesn't work when used with variable instead of direct string
# Then the syntx need to be :  -contentfilter "subject -like '$myvariable'" in order to work !!!!!
# See : http://stackoverflow.com/questions/4017423/cannot-use-variable-with-get-user-filter-in-exchange-management-console
#
New-MailboxExportRequest -Mailbox $username -ContentFilter "Body -like '$ExportFilter'" -filepath $ExportShareFile -BadItemLimit 25
#
# End of function StringInBody
}
#
###################################################################################################################
#
# FUNCTION 11 --> MailWithAttachment :
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#
# Note : this includes mail with only mail attached (like replied mails, forwarded mails...)
#
Function MailWithAttachment {
#
# Exporting alls items that has at least one attachment 
New-MailboxExportRequest -Mailbox $username -ContentFilter {HasAttachment -eq $true} -filepath $ExportShareFile -BadItemLimit 25
#
# End of function MailWithAttachment
}
#
###################################################################################################################
#
# FUNCTION 12 --> StringSomewhere :
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#
Function StringSomewhere {
$ExportFilterString = Read-Host 'Type the string to search (search in all parts : Email address, subject and body) '
$ExportFilter = "*" + $ExportFilterString + "*"
#
# Exporting alls items that has the Email Address as a sender
#
# TIPS -- IMPORTANT :
# +++++++++++++++++++
#
# The documentation states that the syntax for -ContentFilter is - {subject -like '*mystring*'}
# but it doesn't work when used with variable instead of direct string
# Then the syntax need to be :  -contentfilter "subject -like '$myvariable'" in order to work !!!!!
# See : http://stackoverflow.com/questions/4017423/cannot-use-variable-with-get-user-filter-in-exchange-management-console
#
New-MailboxExportRequest -Mailbox $username -ContentFilter "All -like '$ExportFilter'" -filepath $ExportShareFile -BadItemLimit 25
#
# End of function StringSomewhere
}
#
###################################################################################################################
#
# FUNCTION 13 --> SenderEmailAddress :
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#
Function SenderEmailAddress {
$EmailAddressFilter = Read-Host 'Type the sender Email to search '
#
# Exporting alls items that has the Email Address as a sender
#
# TIPS -- IMPORTANT :
# +++++++++++++++++++
#
# The documentation states that the syntax for -ContentFilter is - {subject -like '*mystring*'}
# but it doesn't work when used with variable instead of direct string
# Then the syntax need to be :  -contentfilter "subject -like '$myvariable'" in order to work !!!!!
# See : http://stackoverflow.com/questions/4017423/cannot-use-variable-with-get-user-filter-in-exchange-management-console
#
New-MailboxExportRequest -Mailbox $username -ContentFilter "Sender -eq '$EmailAddressFilter'" -filepath $ExportShareFile -BadItemLimit 25
#
# End of function SenderEmailAddress
}
#
###################################################################################################################
#
# FUNCTION 14 --> ReceiptEmailAddress :
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#
#
Function ReceiptEmailAddress {
$EmailAddressFilter = Read-Host 'Type the receiver Email to search '
#
# Exporting alls items that has the Email Address as a sender
#
# TIPS -- IMPORTANT :
# +++++++++++++++++++
#
# The documentation states that the syntax for -ContentFilter is - {subject -like '*mystring*'}
# but it doesn't work when used with variable instead of direct string
# Then the syntax need to be :  -contentfilter "subject -like '$myvariable'" in order to work !!!!!
# See : http://stackoverflow.com/questions/4017423/cannot-use-variable-with-get-user-filter-in-exchange-management-console
#
# New-MailboxExportRequest -Mailbox $username -ContentFilterLanguage "fr-FR" -ContentFilter "Participants -eq '$EmailAddressFilter'" -filepath $ExportShareFile -BadItemLimit 25
# New-MailboxExportRequest -Mailbox $username -ContentFilterLanguage "en-US" -ContentFilter "Participants -eq '$EmailAddressFilter'" -filepath $ExportShareFile -BadItemLimit 25
# for contentFilterLanguage, see : https://msdn.microsoft.com/fr-fr/library/system.globalization.cultureinfo(vs.71).aspx
New-MailboxExportRequest -Mailbox $username -ContentFilter "Participants -eq '$EmailAddressFilter'" -filepath $ExportShareFile -BadItemLimit 25
#
# End of function ReceiptEmailAddress
}
#
###################################################################################################################
###################################################################################################################
# ========  THIRD : MAIN SCRIPT EXECUTION PART ====================================================================
###################################################################################################################
###################################################################################################################
#
# Determine the export filter type :
# ==================================
#
# Initiate the variable $ExportChoice to be equal to 0
# This variable will be later asked interactively and she's used in the following WHILE statement
# to determine the index of the function call
#
[int]$ExportChoice = 0
#
# ---- Start of the WHILE Statement to determine the function's number to call ------------
while ( $ExportChoice -lt 1 -or $ExportChoice -gt 14 ){
#
# Displaying  the different available choice and the corresponding export actions
# Each choice will be assiociates with a specific function in the SWITCH statement below
#
Write-host ""
Write-host ""
Write-host " Choose your export mode beetween the following list"
Write-host " ==========================================="
Write-host ""
Write-host ""
Write-host -FORE Cyan "1. Export all the mailbox"
Write-host ""
Write-host -FORE DarkYellow "2. Export a subset by range time"
Write-host ""
Write-host -FORE DarkYellow "3. Export only the INBOX"
Write-host ""
Write-host -FORE DarkYellow "4. Export only the SENT Items"
Write-host ""
Write-host -FORE DarkYellow "5. Export only the TASKS"
Write-host ""
Write-host -FORE DarkYellow "6. Export only the CONTACTS"
Write-host ""
Write-host -FORE DarkYellow "7. Export only the CALENDAR"
Write-host ""
Write-host -FORE DarkYellow "8. Export only another specific folder - you will be asked to type its name"
Write-host ""
Write-host -FORE DarkYellow "9. Export only the mails that contains a specific string in the subject"
Write-host ""
Write-host -FORE DarkYellow "10. Export only the mails that contains a specific string in the body"
Write-host ""
Write-host -FORE DarkYellow "11. Export only the mails that have attachments"
Write-host ""
Write-host -FORE DarkYellow "12. Export only the mails that have a specific string in subject, body or recipient fields"
Write-host ""
Write-host -FORE DarkRed "13. Export only the mails that have a specific email address as sender - DOESN'T CURRENTLY WORKS WITH EXCHANGE 2010"
Write-host ""
Write-host -FORE DarkRed "14. Export only the mails that have a specific email address as destination  - DOESN'T CURRENTLY WORKS WITH EXCHANGE 2010"
Write-host ""
#
# Ask you to enter a choice and record the corresponding number in the $ExportChoice variable
[Int]$ExportChoice = read-host "Please enter your choice between 1 and 14"
#
# ---- END of WHILE Statement -------------------------------------------------------------
}
#
#
# ---- BEGIN OF THE SWITCH STATEMENT TO CALL THE FUNCTION CORRESPONDING TO THE $ExportNumber value ------
#
# We use the Switch statement to call the function that corresponds to the chosen number
#
Switch( $ExportChoice ){
  1{AllMBX}
  2{ByTimeOffset}
  3{OnlyInbox}
  4{OnlySentItems}
  5{OnlyTasks}
  6{OnlyContacts}
  7{OnlyCalendar}
  8{FolderToExport}
  9{StringInSubject}
  10{StringInBody}
  11{MailWithAttachment}
  12{StringSomewhere}
  13{SenderEmailAddress}
  14{ReceiptEmailAddress}
default{AllMbx}
#
# ---- END OF THE SWITCH STATEMENT ----------------------------------------------------------------------
#
}
#
###################################################################################################################
#
###################################################################################################################
#
write-host ""
Write-host "End of script "
Write-host "================================================================================================================================"
#
#
###################################################################################################################
#
# TROUBLESHOOTING SECTION :
# =========================
#
#
# IF PB, Use the following commands to debug
#
# To view the status's progress of the requests :
# Get-MailboxExportRequest | ft -AutoSize
#
# To get more details on the requests and get their Identities
# Get-MailboxExportRequest | fl
#
# To get the details (excluded folders, contentfilter,... that have been applied, use the following command :
# Get-MailboxExportRequestStatistics -identity "xxxxxxx" | fl
# TIPS : you obtain the identity from the preceeding command (Get-MailboxExportRequest | fl)
#
# To remove the request when completed :
# Get-MailboxExportRequest -Status completed | Remove-MailboxExportRequest
#
# To remove the failed request :
# Get-MailboxExportRequest -Status failed | Remove-MailboxExportRequest
#
# To remove a queued request before executing :
# Get-MailboxExportRequest -status queued | Remove-MailboxExportRequest
#
#
###################################################################################################################


​​Retour...

TIPs

En construction...

Retour...

Contact :

Patrice Guilloux
Sociéte : AR Systèmes 
FRANCE

pguilloux@outlook.com
Certifié Microsoft :



Certifié HP (APC, AIS, ASE, Master ASE) :
 
   
© Copyright SiteName. Tous droits réservés.