Advertisement
timsstuff

Import-PST.ps1

Oct 23rd, 2015
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [CmdletBinding()]
  2. param(
  3.     [ValidateScript({Test-Path $_ -PathType Leaf})]
  4.     [Parameter(Mandatory=$True,Position=1)][string]$CSV,
  5.    
  6.     [ValidateScript({Test-Path $_ -PathType Container})]
  7.     [Parameter(Mandatory=$True,Position=2)]
  8.     [string]$PSTPath
  9. )
  10.  
  11. Import-Module ActiveDirectory
  12. if(!(Get-PSSnapin |
  13.     Where-Object {$_.name -eq "Microsoft.Exchange.Management.PowerShell.E2010"})) {
  14.       ADD-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
  15. }
  16.  
  17. $start = Get-Date
  18. $startnum = $start -replace "[\W]"
  19. $addomain = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain().Name
  20. $logfile = "PSTImportErrors.csv"
  21. $nl = "`r`n"
  22. if(Test-Path $logfile) {Rename-Item $logfile -NewName "$startnum $logfile" }
  23.  
  24. function writelog([string]$message) {
  25.     $timestamp = Get-Date
  26.     $fc = "White"
  27.     if($message.contains("ERROR")) {$fc ="Red"}
  28.     Add-content $logfile -value "$message"
  29.     Write-Host $message -ForegroundColor $fc
  30. }
  31.  
  32. writelog "#Import Errors"
  33. writelog "Email,PST"
  34. Write-Host $nl
  35.  
  36. if($PSTPath.substring($PSTPath.length-1,1) -ne "\") { $PSTPath += "\" }
  37.  
  38. Import-Csv $csv | ForEach-Object {
  39.     Set-Variable -Name user -Value $null -Scope 0
  40.     Set-Variable -Name mailbox -Value $null -Scope 0
  41.     $email = $_.Email
  42.     $pst = $_.pst
  43.     if($pst.Contains("\\")) {
  44.         $pstfull = $pst
  45.     } else {
  46.         $pstfull = $PSTPath + $pst
  47.     }
  48.  
  49.     $mailbox = Get-Mailbox $email
  50.    
  51.     if($mailbox -ne $null) {
  52.         $mir = Get-MailboxImportRequest -mailbox $mailbox
  53.         if($mir -eq $null) {
  54.             Write-Host "Importing PST file $pst for user $email"
  55.             New-MailboxImportRequest -Mailbox $email -Filepath $pstfull -BadItemLimit 200 -AcceptLargeDataLoss -WarningAction SilentlyContinue
  56.         } else {
  57.             Write-Host "Mailbox Import Request already exists for $email"
  58.         }
  59.     }
  60.     else {
  61.         writelog "$email,$pst"
  62.     }
  63. }
  64.  
  65. $end = Get-Date
  66. $elapsed = $end - $start
  67. Write-Host $nl
  68. Write-Host "Finished import at $end ($elapsed)"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement