-
Notifications
You must be signed in to change notification settings - Fork 0
/
Export-ExchangeUMGreetings.ps1
87 lines (67 loc) · 4.03 KB
/
Export-ExchangeUMGreetings.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
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
# get um enabled mailboxes
$umMailboxes = @(Get-Mailbox -ResultSize Unlimited -Filter {UMEnabled -eq $true})
# Path to the Exchange Web Services DLL
$EWSManagedApiPath = "c:\Program Files\Microsoft\Exchange Server\V15\Bin\Microsoft.Exchange.WebServices.dll"
# Load EWS Managed API
Add-Type -Path $EWSManagedApiPath
# Create EWS Object
$Service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2013)
# csv object
[System.Collections.ArrayList]$csv = @()
# loop through each user
foreach ( $User in $umMailboxes ) {
# UM mailbox
$Mailbox = $User
# Get the SMTP address from the mailbox
$SmtpAddress = $Mailbox.PrimarySmtpAddress.ToString()
# Get the autodiscover URL
$Service.AutodiscoverUrl($SmtpAddress)
# Use EWS Impersonation to locate the root folder in the mailbox
$Service.ImpersonatedUserId = New-Object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $SmtpAddress)
$FolderId = [Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Root
$Folder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($Service,$FolderId)
# Create the search filter to find the UM custom greetingsd
$searchFilter = New-Object Microsoft.Exchange.WebServices.Data.SearchFilter+ContainsSubstring([Microsoft.Exchange.WebServices.Data.ItemSchema]::ItemClass,"IPM.Configuration.Um.CustomGreetings",[Microsoft.Exchange.WebServices.Data.ContainmentMode]::Substring,[Microsoft.Exchange.WebServices.Data.ComparisonMode]::IgnoreCase);
# Define the EWS search view
$view = New-Object Microsoft.Exchange.WebServices.Data.ItemView(100, 0, [Microsoft.Exchange.WebServices.Data.OffsetBasePoint]::Beginning)
$view.PropertySet = New-Object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
$view.Traversal = [Microsoft.Exchange.WebServices.Data.ItemTraversal]::Associated
$props = [PSCustomObject][ordered]@{
'Name' = $Mailbox.name
'userprincipalname' = $Mailbox.userprincipalname
'AwayGreeting' = if((Get-UMMailboxConfiguration $Mailbox.samaccountname).greeting -eq 'Away') { $true } else { $false }
'standardGreeting' = ''
'oofGreeting' = ''
}
# Do the search and enumerate the results
$results = $service.FindItems( $FolderId, $searchFilter, $view )
if ( $results -ne $null ) {
# Define the property set required to get the binary audio data
$psPropset = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
# Add the binary audio data property to the property set
$PidTagRoamingBinary = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x7C09,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary);
$psPropset.Add($PidTagRoamingBinary)
# Load the new properties
[Void]$service.LoadPropertiesForItems($results,$psPropset)
# Create a folder to export custom greeting
$Path = "c:\MailboxCustomGreetings\" + $Mailbox.name
$Path = new-item $Path -itemtype directory
}
# Loop through the results
foreach ( $Item in $results.Items ) {
# If this is the main greeting, set the flag to true
if ( $Item.ItemClass -eq "IPM.Configuration.Um.CustomGreetings.External" ) {
$Filename = $Mailbox.samaccountname + "_Standard.wav"
$props.standardGreeting = "$Path\$Filename"
}
# If this is the extended away greeting, set the flag to true
if ( $Item.ItemClass -eq "IPM.Configuration.Um.CustomGreetings.Oof" ) {
$Filename = $Mailbox.samaccountname + "_Oof.wav"
$props.oofGreeting = "$Path\$Filename"
}
[IO.File]::WriteAllBytes("$Path\$Filename",$Item.ExtendedProperties.Value)
}
$csv.Add($props) | Out-Null
}
# end
$csv | Export-Csv -NoClobber -NoTypeInformation "C:\MailboxCustomGreetings\ex.um.customgreetings.csv"