forked from tomtorggler/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStart-OutlookBackup.ps1
47 lines (46 loc) · 2.21 KB
/
Start-OutlookBackup.ps1
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
function Start-OutlookBackup {
<#
.SYNOPSIS
Backup Outlook Contacts and Calendar folders to PST.
.DESCRIPTION
This function uses the Outlook COM object to copy the Contacts and Calendar Folders to a PST file.
.EXAMPLE
PS C:\> Start-OutlookBackup -EmailAddress [email protected] -Path c:\backup\file.pst
This example makes a PST file at c:\backup\file.pst and copys the Calendar and Contacts folders of the [email protected] Mailbox
to the PST file.
.INPUTS
None.
.OUTPUTS
None.
.NOTES
@torggler
2018-08-30
Missing: Error handling, folder selection
#>
[cmdletbinding()]
param(
[Parameter()]
[system.io.fileinfo]$Path = (Join-Path -Path $env:USERPROFILE -ChildPath Documents\olBackup.pst),
[Parameter()]
[string]$EmailAddress = ("{0}@{1}" -f $env:USERNAME,$env:USERDNSDOMAIN)$EmailAddress = ("{0}@{1}" -f $env:USERNAME,$env:USERDNSDOMAIN)
)
$Outlook = New-Object -ComObject Outlook.Application
Write-Verbose "Connected to Outlook Profile $($outlook.DefaultProfileName)"
$NS = $Outlook.GetNamespace('MAPI')
Write-Verbose "Opened Namespace $($NS.Type)"
$Store = $NS.Stores | Where-Object {$_.displayname -eq $EmailAddress}
Write-Verbose "Connected to Store $($Store.DisplayName)"
$Calendar = $Store.GetDefaultFolder([Microsoft.Office.Interop.Outlook.OlDefaultFolders]"olFolderCalendar")
$Contacts = $Store.GetDefaultFolder([Microsoft.Office.Interop.Outlook.OlDefaultFolders]"olFolderContacts")
Write-Verbose "Default Calendar Folder is $($Calendar.Name)"
Write-Verbose "Default Contacts Folder is $($Contacts.Name)"
$Outlook.Session.AddStore($Path)
$PST = $ns.Stores | Where-Object {$_.filepath -eq $Path.Fullname}
$ca = $Calendar.CopyTo($PST)
$co = $Contacts.CopyTo($PST)
Write-Verbose "Backed up $($ca.Items.Count) Calendar Items"
Write-Verbose "Backed up $($co.Items.Count) Contact Items"
$PSTRoot = $PST.GetRootFolder()
$PSTFolder = $NS.Folders.Item($PSTRoot.Name)
$NS.GetType().InvokeMember('RemoveStore',[System.Reflection.BindingFlags]::InvokeMethod,$null,$NS,($PSTFolder))
}