-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetOutlookCalendar_v2.ps1
378 lines (310 loc) · 13.5 KB
/
GetOutlookCalendar_v2.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
Function New-SQLQuery {
<#
.SYNOPSIS
Returns data from a SQL query.
.DESCRIPTION
Returns data from a SQL query. Assumes integrated authentication.
.EXAMPLE
New-SQLQuery -Server Server1 -Instance LYNC -Database lis -Query 'Select * from lis'
#>
[CmdletBinding(SupportsShouldProcess = $True)]
param (
[string]$Server,
[string]$Instance = '',
[string]$Database,
[string]$Query
)
#Define SQL Connection String
[string]$ServerAndInstance = $Server
if ($Instance -ne '')
{
[string]$ServerAndInstance = "$ServerAndInstance\$Instance"
}
[string]$connstring = "server=$ServerAndInstance;database=$Database;trusted_connection=true;"
#Define SQL Command
[object]$command = New-Object System.Data.SqlClient.SqlCommand
$command.CommandText = $Query
# Define SQL connection
[object]$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $connstring
$connection.Open()
$command.Connection = $connection
# Create SQL data adapter and associate the query with it
[object]$sqladapter = New-Object System.Data.SqlClient.SqlDataAdapter
$sqladapter.SelectCommand = $command
# Execute query
[object]$results = New-Object System.Data.Dataset
$recordcount = $sqladapter.Fill($results)
$connection.Close()
return $Results.Tables[0]
}
Function Get-OutlookCalendar {
<#
.Synopsis
This function returns items from Outlook Calendar. You can specify the mailbox and folder.
By: Michael Wong (modified source by Zachary Loeber)
.OlDefaultFolders
https://docs.microsoft.com/en-us/office/vba/api/outlook.oldefaultfolders
.EXAMPLE
Get-OutlookCalendar | Out-GridView
#>
[CmdLetBinding()]
param(
[string]$Mailbox,
[string]$FunctionGroup,
[string]$FolderName = '',
[switch]$ListMailbox,
[switch]$UnreadOnly,
[switch]$FilterByYear,
[switch]$Recurse,
[string[]]$Properties = @()
)
begin {
function Release-Ref ($ref) {
[System.Runtime.InteropServices.Marshal]::ReleaseComObject([System.__ComObject]$ref) | out-null
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
}
<#$props = @("body", "header", "returnpath", "spf", "SenderId", "antispam", "scl",
"pcl", "senderserver", "senderIP", "*", "subject")
$headerProps = @("header", "returnpath", "spf", "SenderId", "antispam", "scl",
"pcl", "senderserver", "senderIP", "*")#>
$props = @("header","Recipients", "*")
# validate properties
if($properties) {
foreach($prop in $properties) {
if($props -notcontains $prop) { throw "$prop is not a valid property" }
}
}
function getHeader($item) {
$headerScheme = "http://schemas.microsoft.com/mapi/proptag/0x007D001E"
return $item.propertyaccessor.getproperty($headerscheme)
}
function getRecipients($item) {
# property schema
$recipientsScheme = "http://schemas.microsoft.com/mapi/proptag/0x39FE001E"
$recips = $item.Recipients
$str = ''
if ($recips) {
foreach($recip in $recips) {
$str += $recip.PropertyAccessor.getproperty($recipientsScheme) + "|" ;
}
return $(if ($str.length -gt 30000) { $str.substring(0, 30000) } else { $str })
} else {
return $str
}
}
function Get-OutlookSubFolder($FolderSource) {
foreach ($Folder in $FolderSource.Folders) {
$Folder
Get-OutlookSubFolder($Folder)
}
}
function getCalendar($email, $mailboxowner, $mailboxresolvedname, $functiongroup, $foldername, $folderpath) {
$calstuff = new-object pscustomobject
$caldata = @{}
$caldata.MailBoxOwner = $mailboxowner
$caldata.FunctionGroup = $functiongroup
$caldata.MailBoxResolvedName = $mailboxresolvedname.Name
$caldata.Start = $email.Start -replace "(\w+),? (\w+) (\d+)\w+,? (\d+)", "$1 $3 $2 $4"
$caldata.End = $email.End -replace "(\w+),? (\w+) (\d+)\w+,? (\d+)", "$1 $3 $2 $4"
$caldata.Duration = $email.Duration
$caldata.Categories = $email.Categories
$caldata.Subject = $email.Subject
$caldata.Location = $email.Location
$caldata.IsRecurring = $email.IsRecurring
$caldata.Organizer = $email.Organizer
$caldata.Body = $(if ($email.Body.length -gt 30000) { $email.Body.substring(0, 30000) } else { $email.Body })
$caldata.FolderName = $foldername
$caldata.FolderPath = $folderpath
$caldata.Recipients = getRecipients($email)
return New-Object psobject -Property $caldata
}
Function Get-DefaultDateTime {
# Extract the default Date/Time formatting from the local computer's "Culture" settings, and then create the format to use when parsing the date/time information.
$CultureDateTimeFormat = (Get-Culture).DateTimeFormat
$DateFormat = $CultureDateTimeFormat.ShortDatePattern
$TimeFormat = $CultureDateTimeFormat.ShortTimePattern
$DateTimeFormat = "$DateFormat $TimeFormat"
return $DateTimeFormat
}
}
Process {
# load the required .NET types
Add-type -assembly "Microsoft.Office.Interop.Outlook" | out-null
# access Outlook object model
$outlook = new-object -comobject outlook.application
# connect to the namespace
$mapi = $outlook.GetNameSpace("MAPI")
if(!$mapi) { throw "Unable to create MAPI to Outlook. Be sure Microsoft Office is installed" }
if($ListMailbox) {
$mapi.folders | %{ $_.fullfolderpath.trim("\\") }
return
}
if(!$mailbox) {
if ($FolderName -eq '') {
$olFolders = "Microsoft.Office.Interop.Outlook.olDefaultFolders" -as [type]
$FolderSource = $mapi.getDefaultFolder($olFolders::olFolderInBox)
#$FolderSource = @($Mapi.Folders)
}
else {
try {
#$FolderSource = $Mapi.folders.item($FolderName)
$olFolders = "Microsoft.Office.Interop.Outlook.olDefaultFolders" -as [type]
$FolderSource = $mapi.getDefaultFolder($olFolders::$FolderName)
}
catch {
throw "Can't access folder $FolderName"
}
}
}
else {
$main = $mapi.CreateRecipient($mailbox)
#$main = $mapi.folders.item($mailbox)
if(!$main) {
throw "Can't access $mailbox. Use -ListMailbox to get valid mailboxes"
}
if ($FolderName -eq '') {
#pull all folders
#$FolderSource = $mapi.folders.item($mailbox).Folders
$FolderSource = $mapi.folders.item($mailbox).Folders
}
else {
try {
$olFolders = "Microsoft.Office.Interop.Outlook.olDefaultFolders" -as [type]
if($main.Resolve()){
$FolderSource = $mapi.GetSharedDefaultFolder($main, $olFolders::$FolderName)
}
}
catch {
throw "Can't access folder $foldername"
}
}
}
$Folders = @()
$Folders += $FolderSource
$Folders += if ($Recurse) { @(Get-OutlookSubFolder($FolderSource.Folders)) }
Foreach ($Folder in $Folders) {
if ($UnreadOnly) {
Write-Verbose "Get-Outlook: Retreiving unread items from folder $($Folder.Name)"
#$folder.items.Restrict("[UnRead] = True") | %{ getEmail $_ ($Folder).Name ($Folder).FolderPath }
$folder.items.Restrict("[UnRead] = True") | %{ getCalendar $_ $Mailbox $main $FunctionGroup ($Folder).Name ($Folder).FolderPath }
}
elseif ($FilterByYear) {
Write-Verbose "Get-Outlook: Retreiving date filtered items from folder $($Folder.Name)"
# Change Date Filter parameters & Trigger default Date/Time formatting
$datefilter = "1/1/2018 12:00AM"
$getDTFormat = Get-DefaultDateTime
$folder.items.Restrict("[Start] >= '"+ (Get-Date $datefilter -Format $getDTFormat) +"'") | %{ getCalendar $_ $Mailbox $main $FunctionGroup ($Folder).Name ($Folder).FolderPath }
#$folder.items.Restrict("[Start] >= '" + (Get-Date $datefilter -Format "MM/dd/yyyy hh:mm AMPM") + "'") | %{ getCalendar $_ $Mailbox $main $FunctionGroup ($Folder).Name ($Folder).FolderPath }
}
else {
Write-Verbose "Get-Outlook: Retreiving read items from folder $($Folder.Name)"
$folder.items | %{ getCalendar $_ $Mailbox $main $FunctionGroup ($Folder).Name ($Folder).FolderPath }
}
}
Release-Ref $outlook
}
}
Function Upload-FileWebDAV {
<#
.Synopsis
This function forward the XMl files generated to storage destination using WebDAV
By: Michael Wong
.EXAMPLE
Upload-FileWebDAV -File "C:\Apps\Michael\Projects\SMEP Structured Calendar\test.xml" -URL "\\<IP-Address>\WebDAV\DavWWWRoot"
#>
[CmdletBinding(SupportsShouldProcess = $True)]
param (
[string]$File,
[string]$URL
)
$uri = $URL
Remove-PSDrive WebDavShare -Force -ea 0
New-PSDrive -Name WebDavShare -PSProvider FileSystem -Root $uri | Out-Null
try {
Get-ChildItem "WebDavShare:\" Select-Object -First 1 | Out-Null
} catch {
Remove-PSDrive WebDavShare -Force -ea 0
$C = Get-Credential
New-PSDrive -Name WebDavShare -Credential $C -PSProvider FileSystem -Root $uri | Out-Null
}
Copy-Item $File -Destination $uri -force
Remove-PSDrive WebDavShare -Force -ea 0
}
Function Get-Inputs {
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$form = New-Object System.Windows.Forms.Form
$form.Text = 'SMEP Structured Calendar v01'
$form.Size = New-Object System.Drawing.Size(300,200)
$form.StartPosition = 'CenterScreen'
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = 'OK'
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $OKButton
$form.Controls.Add($OKButton)
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = 'Cancel'
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $CancelButton
$form.Controls.Add($CancelButton)
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = 'Please enter the candidate Shell Email Address:'
$form.Controls.Add($label)
$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(10,40)
$textBox.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox)
#Dropdown
[array]$DropDownArray = "SWK", "SBH", "CP", "PE", "DEV", "WELLS", "BE", "HR" ,"FIN" , "IMIT", "HSSE", "COMM", "NOV", "ER", "VP", "EXP", "GR", "LGL"
$DropDownLabel = new-object System.Windows.Forms.Label
$DropDownLabel.Location = new-object System.Drawing.Size(10,70)
$DropDownLabel.size = new-object System.Drawing.Size(280,20)
$DropDownLabel.Text = "Select SMEP Function Group:"
$Form.Controls.Add($DropDownLabel)
$DropDown = new-object System.Windows.Forms.ComboBox
$DropDown.Location = new-object System.Drawing.Size(10,90)
$DropDown.Size = new-object System.Drawing.Size(130,30)
ForEach ($Item in $DropDownArray) {
[void] $DropDown.Items.Add($Item)
}
$DropDown.SelectedItem = $DropDown.Items[0]
$Form.Controls.Add($DropDown)
$form.Topmost = $true
$form.Add_Shown({$textBox.Select()})
$result = $form.ShowDialog()
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
$x = @{}
$x.email = $textBox.Text
$x.functiongroup = $DropDown.SelectedItem.ToString()
#return $x
return New-Object psobject -Property $x
}
}
<# Main () - Execution #>
# get Inputs & define file location
$EmailID = Get-Inputs
$FileLocation = "C:\Apps\" + $EmailID.functiongroup + "_" + ($EmailID.email -split "@")[0] + ".csv"
# Check file exist prior to cleanup
if (Test-Path $FileLocation) {
Remove-Item $FileLocation
}
#Get-OutlookCalendar in CSV
#Get-OutlookCalendar $EmailID.email $EmailID.functiongroup olFolderCalendar | Export-Csv -NoTypeInformation $FileLocation
#Get-OutlookCalendar in CSV, filtered by year
Get-OutlookCalendar $EmailID.email $EmailID.functiongroup olFolderCalendar -FilterByYear | Export-Csv -NoTypeInformation $FileLocation
#Get-OutlookCalendar in Table-Grid view
#Get-OutlookCalendar $EmailID.email $EmailID.functiongroup olFolderCalendar | Out-GridView
#Get-OutlookCalendar in XML
#Get-OutlookCalendar $EmailID.email $EmailID.functiongroup olFolderCalendar | ConvertTo-Xml -as String -NoTypeInformation | Set-Content -Path $FileLocation
#File Export & CleanUp
Upload-FileWebDAV -File $FileLocation -URL "\\60.53.88.25\WebDAV\DavWWWRoot"
Remove-Item $FileLocation