Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [CmdletBinding()]
- param(
- [ValidateScript({Test-Path $_ -PathType Leaf})]
- [Parameter(Mandatory=$True,Position=1)][string]$CSV,
- [ValidateScript({Test-Path $_ -PathType Container})]
- [Parameter(Mandatory=$True,Position=2)]
- [string]$PSTPath
- )
- Import-Module ActiveDirectory
- if(!(Get-PSSnapin |
- Where-Object {$_.name -eq "Microsoft.Exchange.Management.PowerShell.E2010"})) {
- ADD-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
- }
- $start = Get-Date
- $startnum = $start -replace "[\W]"
- $addomain = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain().Name
- $logfile = "PSTImportErrors.csv"
- $nl = "`r`n"
- if(Test-Path $logfile) {Rename-Item $logfile -NewName "$startnum $logfile" }
- function writelog([string]$message) {
- $timestamp = Get-Date
- $fc = "White"
- if($message.contains("ERROR")) {$fc ="Red"}
- Add-content $logfile -value "$message"
- Write-Host $message -ForegroundColor $fc
- }
- writelog "#Import Errors"
- writelog "Email,PST"
- Write-Host $nl
- if($PSTPath.substring($PSTPath.length-1,1) -ne "\") { $PSTPath += "\" }
- Import-Csv $csv | ForEach-Object {
- Set-Variable -Name user -Value $null -Scope 0
- Set-Variable -Name mailbox -Value $null -Scope 0
- $email = $_.Email
- $pst = $_.pst
- if($pst.Contains("\\")) {
- $pstfull = $pst
- } else {
- $pstfull = $PSTPath + $pst
- }
- $mailbox = Get-Mailbox $email
- if($mailbox -ne $null) {
- $mir = Get-MailboxImportRequest -mailbox $mailbox
- if($mir -eq $null) {
- Write-Host "Importing PST file $pst for user $email"
- New-MailboxImportRequest -Mailbox $email -Filepath $pstfull -BadItemLimit 200 -AcceptLargeDataLoss -WarningAction SilentlyContinue
- } else {
- Write-Host "Mailbox Import Request already exists for $email"
- }
- }
- else {
- writelog "$email,$pst"
- }
- }
- $end = Get-Date
- $elapsed = $end - $start
- Write-Host $nl
- Write-Host "Finished import at $end ($elapsed)"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement