Thursday, September 27, 2012

Create an Import Export Management Group with rights

To be able to use the other script to import PST Files, you first must have the permission to do so.

Since I am in favor of RBAC I created a little script to create an Exchange Import and Export Management Group, add the current user to this group and assign the appropriate rights to the group.

# To be able to import PST files you must be member of the Import Export Management Role.
# Bas Steelooper, XOBIT IT Services
# This script will create an AD Group in the users OU named Exchange-ImportExport-Management
# And add the current user to this group.
# You have to logoff and logon again to use the newly acquired rights

# Configure here the name of the group to create
$GroupName = "Exchange-ImportExport-Management"

# Do not edit below this line

#Prepare the needed modules
Import-Module ActiveDirectory
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010

# Create the AD Group as a Universal Group
New-ADgroup -Name $GroupName -GroupScope 2

# Add the current logged on user to the Group
Add-ADGroupMember -Identity $GroupName $env:username

# Assign the Import Export Rights to the Group
New-ManagementRoleAssignment -Name "ImportExport-Management" -SecurityGroup $GroupName -Role "Mailbox Import Export"

Monday, September 24, 2012

Importing a ####-Load of PST Files into Exchange

For a migration we needed to import a lot of PST files into the mailboxes of migrated users.. Since I am an IT guy, and therefor lazy, I created a script to do this.

This script creates a folder for each PST file with the name: "Import ()"

Prereqs for the script are:
* For each user with to be imported PST files create a folder with the first part of the emailadres (everything in front of the @-sign)
* add the PST files in the appropriate folder
* Edit the first two lines of the script to point to the share where the data resides, and the domainname with the @-Sign
* User must have the Import and Export management role assigned

# PST Import script.
# Bas Steelooper, XOBIT IT Services
# This script will import the PST files from a given location.
# The PST files must reside in a folder which is named as the part of the emailadres in front of the @-sign
# This script will import each folder one at a time and cleanup all the completed imports
# With the command Get-MailBoxImportRequest you can view all the failed import requests

# Configure the parameters. 
# $pstpath contains the share location where the PST Files are stored. ( The AD Computer account must have access to this location)
# $emaildomain contains the rest of the emailadres for all users
# $forceimport if $TRUE thePST-File will be forced imported, items which cannot be imported are skipped. Default is $FALSE
$pstpath = "\\<server>\<share>\UsersPSTData"
$emaildomain = ""
$forceimport = $TRUE

# Do not edit below this line

#Load the Exchange powershell Snap-IN
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010

get-ChildItem $pstpath | Foreach-object{ 

 $curuser = ""
 $curuser = $_
 $PSTFiles = get-ChildItem $pstpath\$curuser 
 $PSTFiles | Foreach-Object{ 
  if ($forceimport) {
   New-MailboxImportRequest -BatchName $curuser -BadItemLimit 200 -AcceptLargeDataLoss -Name $_ -MailBox $curuser$emaildomain -FilePath $pstpath\$curuser\$_ -TargetRootFolder "INBOX/Import ($_)"
  else {
   New-MailboxImportRequest -BatchName $curuser -Name $_ -MailBox $curuser$emaildomain -FilePath $pstpath\$curuser\$_ -TargetRootFolder "INBOX/Import ($_)"

 while ((Get-MailboxImportRequest -BatchName $curuser | Where {$_.Status -eq "Queued" -or $_.Status -eq "InProgress"}))
  sleep 60
 Get-MailboxImportRequest -BatchName $curuser | Where {$_.Status -eq "Completed"} | Remove-MailboxImportRequest -Confirm:$false;