-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImport-PST.ps1
More file actions
88 lines (77 loc) · 3.41 KB
/
Import-PST.ps1
File metadata and controls
88 lines (77 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<#
.Synopsis
Imports PST files that are labelled with the OLDID-NEWID.PST format to mailboxes that match the NEWID
.DESCRIPTION
This script looks at a specified file path ($path), then tries to find all PSTs produced by the
MailMigrationTool.ps1 script. As such the PSTS are named in the following format;
OLDID-NEWID.PST
E.G. JohnDoe-JDoe.PST (where JDoe is the new user account ID, and name of the new mailbox)
.EXAMPLE
.\Import-PST -Path \\Server1\Exports -ImportPST
.OUTPUTS
.\FailedItems.CSV (Any failed imports)
.\Imported.CSV (All succesful imports)
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $false, Position = 1, ParameterSetName = "Default",
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
HelpMessage = "If ImportPST is enabled it will attempt to import the PSTs to the mailboxes belonging to the new aliases")]
[ValidateNotNullOrEmpty()]
[Switch]
$ImportPst,
[Parameter(Mandatory = $true, Position = 2, ParameterSetName = "Default",
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
HelpMessage = "Path to import PST files from")]
[ValidateNotNullOrEmpty()]
[string]
$Path
)
#region --------------- Function - OutputInfo -------------------------------
<# **********************************************************************************************************************
This Exports failed item details to a CSV file
************************************************************************************************************************* #>
function OutputInfo {
param (
$name,$newalias,$Info,$CSVname
)
$obj = [PSCustomObject]@{
Info = $info
name = $name
newalias = $newalias
} | Export-Csv -Path ($path + '\' + $CSVname + '.csv') -Append -NoTypeInformation
}
#endregion
If ($ImportPst) {
$ImportList = Get-ChildItem -Path $path -Include *.pst -Recurse
ForEach ($item in $ImportList) {
#Split out the name of the alias from the PST file in the folder
Write-Host "Splitting" $item
#The $IMalias is found by splitting out all the backslashes (\) in the path, selecting the last one
#(being the file name), then seperates the name of the two aliases, selects the second name, then
#trims the .PST from the end.
$IMalias = $item.ToString().Split('\')[-1].Split('-')[1].Split('.')[0]
Write-Host "looking for mailbox for" $IMalias
try {
$MailboxPresent = Get-Mailbox -Identity $IMalias
}
catch {
Write-Host "No mailbox found for" $IMalias
OutputInfo -info "No mailbox found" -name $item.Name -newalias $IMalias -CSVname 'FailedItems'
}
If ({Test-Path $item} -and {$MailboxPresent}){
#If the PST file is in the right spot, and a mailbox exists, create the import request
try {
New-MailboxImportRequest -Name $IMAlias -FilePath $item -Mailbox $IMalias
Write-Host "Request generated for" $IMalias
OutputInfo -Info "Import request created" -name $item.name -newalias $IMalias -CSVname 'Imported'
}
catch {
Write-host "There was an issue creating the mailbox import request for " $IMalias
OutputInfo -Info "Could not create import request" -name $item.Name -newalias $IMalias -CSVname 'FailedItems'
}
}
}
}