-
Notifications
You must be signed in to change notification settings - Fork 0
/
PWDM_Multi_Export.ps1
399 lines (281 loc) · 17.8 KB
/
PWDM_Multi_Export.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
Install-Module -Name PWPS_PWDM
Install-Module -Name PWPS_DAB
#region Configuration
$currentDirectory = $PSScriptRoot
# Read the .txt file line by line
$projectIdsFile = "$currentDirectory\project_ids.txt"
$export_path = "D:\PWDM Export\BENOI" # The top level folder path to export PWDM content for the target connected project
$script_name = "PWDMExportScript" # Name of script, used for logging
#endregion
# Check if the file exists
if (Test-Path -Path $projectIdsFile) {
$projectIds = Get-Content -Path $projectIdsFile
# Loop through each project ID from the file
foreach ($connected_project_id in $projectIds) {
#region Tokens
$progress_path = "$export_path\progress\$connected_project_id" # The folder path to store the master progress sheet
$log_path = "$export_path\logs\$connected_project_id\pwdm_export.log" # The log file path
Write-PWPSLog -Path $log_path -Cmdlet $script_name -Level Info -Message "Generating Token" -Verbose
#$token = Get-IMSNonFederatedAccountToken -UserName [email protected] -Password (Read-Host -AsSecureString)
$token = Get-IMSTokenFromConnectionClient
#endregion
#region Build Data Table
if (!(Test-Path "$progress_path\pwdm_export_progress.xlsx"))
{
try
{
Write-PWPSLog -Path $log_path -Cmdlet $script_name -Level Info -Message "Building master export list" -Verbose
#region Files table
[System.Data.DataTable]$export_progress_table = New-Object -TypeName System.Data.DataTable
$export_progress_table.TableName = "PWDM_Packages"
$export_progress_table.Columns.Add("PackageId", "System.Guid")
$export_progress_table.Columns.Add("PackageType", "System.String")
$export_progress_table.Columns.Add("Exported", "System.Boolean")
#region Add Submittals
$submittal_ids = Get-DMSubmittalIds -ConnectedProjectId $connected_project_id -IMSToken $token -BatchSize 50
Write-PWPSLog -Path $log_path -Cmdlet $script_name -Level Info -Message "Adding $(($submittal_ids | Measure-Object).Count) submittals" -Verbose
foreach ($package_id in $submittal_ids)
{
$new_row = $export_progress_table.NewRow()
$new_row.PackageId = $package_id
$new_row.PackageType = "Submittal"
$new_row.Exported = $false
$export_progress_table.Rows.Add($new_row)
}
#endregion
#region Add Transmittals
$transmittal_ids = Get-DMTransmittalIds -ConnectedProjectId $connected_project_id -IMSToken $token -BatchSize 50
Write-PWPSLog -Path $log_path -Cmdlet $script_name -Level Info -Message "Adding $(($transmittal_ids | Measure-Object).Count) transmittals" -Verbose
foreach ($package_id in $transmittal_ids)
{
$new_row = $export_progress_table.NewRow()
$new_row.PackageId = $package_id
$new_row.PackageType = "Transmittal"
$new_row.Exported = $false
$export_progress_table.Rows.Add($new_row)
}
#endregion
#region Add IncomingRFIs
$incoming_rfi_ids = Get-DMIncomingRFIIds -ConnectedProjectId $connected_project_id -IMSToken $token -BatchSize 50
Write-PWPSLog -Path $log_path -Cmdlet $script_name -Level Info -Message "Adding $(($incoming_rfi_ids | Measure-Object).Count) incoming rfis" -Verbose
foreach ($package_id in $incoming_rfi_ids)
{
$new_row = $export_progress_table.NewRow()
$new_row.PackageId = $package_id
$new_row.PackageType = "IncomingRFI"
$new_row.Exported = $false
$export_progress_table.Rows.Add($new_row)
}
#endregion
#region Add OutgoingRFIs
$outgoing_rfi_ids = Get-DMOutgoingRFIIds -ConnectedProjectId $connected_project_id -IMSToken $token -BatchSize 50
Write-PWPSLog -Path $log_path -Cmdlet $script_name -Level Info -Message "Adding $(($outgoing_rfi_ids | Measure-Object).Count) outgoing rfis" -Verbose
foreach ($package_id in $outgoing_rfi_ids)
{
$new_row = $export_progress_table.NewRow()
$new_row.PackageId = $package_id
$new_row.PackageType = "OutgoingRFI"
$new_row.Exported = $false
$export_progress_table.Rows.Add($new_row)
}
#endregion
#region Add IncomingCorrespondences
$incoming_correspondence_ids = Get-DMIncomingCorrespondenceIds -ConnectedProjectId $connected_project_id -IMSToken $token -BatchSize 50
Write-PWPSLog -Path $log_path -Cmdlet $script_name -Level Info -Message "Adding $(($incoming_correspondence_ids | Measure-Object).Count) incoming correspondence" -Verbose
foreach ($package_id in $incoming_correspondence_ids)
{
$new_row = $export_progress_table.NewRow()
$new_row.PackageId = $package_id
$new_row.PackageType = "IncomingCorrespondence"
$new_row.Exported = $false
$export_progress_table.Rows.Add($new_row)
}
#endregion
#region Add OutgoingCorrespondences
$outgoing_correspondence_ids = Get-DMOutgoingCorrespondenceIds -ConnectedProjectId $connected_project_id -IMSToken $token -BatchSize 50
Write-PWPSLog -Path $log_path -Cmdlet $script_name -Level Info -Message "Adding $(($outgoing_correspondence_ids | Measure-Object).Count) outgoing correspondence" -Verbose
foreach ($package_id in $outgoing_correspondence_ids)
{
$new_row = $export_progress_table.NewRow()
$new_row.PackageId = $package_id
$new_row.PackageType = "OutgoingCorrespondence"
$new_row.Exported = $false
$export_progress_table.Rows.Add($new_row)
}
#endregion
#endregion
#region Metadata table
[System.Data.DataTable]$metadata_progress_table = New-Object -TypeName System.Data.DataTable
$metadata_progress_table.TableName = "PWDM_Metadata"
$metadata_progress_table.Columns.Add("PackageType", "System.String")
$metadata_progress_table.Columns.Add("Exported", "System.Boolean")
foreach ($package_type in $($export_progress_table.Rows.PackageType | Sort-Object -Unique))
{
$new_row = $metadata_progress_table.NewRow()
$new_row.PackageType = $package_type
$new_row.Exported = $false
$metadata_progress_table.Rows.Add($new_row)
}
Write-PWPSLog -Path $log_path -Cmdlet $script_name -Level Info -Message "Total $($export_progress_table.Rows.Count) packages" -Verbose
#endregion
New-XLSXWorkbook -InputTables $export_progress_table, $metadata_progress_table -OutputFileName "$progress_path\pwdm_export_progress.xlsx"
}
catch
{
Write-PWPSLog -Path $log_path -Cmdlet $script_name -Level Error -Message "Failed to build tables" -Verbose
Write-PWPSLog -Path $log_path -Cmdlet $script_name -Level Error -Message $_ -Verbose
Break
}
}
#endregion
#region Load Latest Progress
Write-PWPSLog -Path $log_path -Cmdlet $script_name -Level Info -Message "Loading latest progress" -Verbose
$progress_files = Get-ChildItem -LiteralPath $progress_path
$latest_progress_file = $progress_files | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1
$latest_progress = Get-TablesFromXLSXWorkbook $latest_progress_file.FullName
$latest_files = $latest_progress | Where-Object {$PSItem.TableName -eq "PWDM_Packages"}
$latest_files_count = ($latest_files.Rows | Measure-Object).Count
$latest_metadata = $latest_progress | Where-Object {$PSItem.TableName -eq "PWDM_Metadata"}
$latest_metadata_count = ($latest_metadata.Rows | Measure-Object).Count
$latest_remaining_files = $latest_files.Rows | Where-Object {$PSItem.Exported -eq $false}
$latest_remaining_files_count = ($latest_remaining_files | Measure-Object).Count
$latest_complete_files = $latest_files.Rows | Where-Object {$PSItem.Exported -eq $true}
$latest_complete_files_count = ($latest_complete_files | Measure-Object).Count
$latest_remaining_metadata = $latest_metadata.Rows | Where-Object {$PSItem.Exported -eq $false}
$latest_remaining_metadata_count = ($latest_remaining_metadata | Measure-Object).Count
$latest_complete_metadata = $latest_metadata.Rows | Where-Object {$PSItem.Exported -eq $true}
$latest_complete_metadata_count = ($latest_complete_metadata | Measure-Object).Count
Write-PWPSLog -Path $log_path -Cmdlet $script_name -Level Info -Message "$latest_files_count total packages" -Verbose
Write-PWPSLog -Path $log_path -Cmdlet $script_name -Level Info -Message "$latest_complete_files_count completed packages" -Verbose
Write-PWPSLog -Path $log_path -Cmdlet $script_name -Level Info -Message "$latest_remaining_files_count remaining packages" -Verbose
Write-PWPSLog -Path $log_path -Cmdlet $script_name -Level Info -Message "$latest_metadata_count total metadata files" -Verbose
Write-PWPSLog -Path $log_path -Cmdlet $script_name -Level Info -Message "$latest_complete_metadata_count completed metadata files" -Verbose
Write-PWPSLog -Path $log_path -Cmdlet $script_name -Level Info -Message "$latest_remaining_metadata_count remaining metadata files" -Verbose
#endregion
#region Export Packages
$updated_files_count = 0
$i = 1
foreach ($package_id in $latest_remaining_files)
{
Write-PWPSLog -Path $log_path -Cmdlet $script_name -Level Info -Message "Package Id: $($package_id.PackageId) ($i/$latest_remaining_files_count)" -Verbose
Write-PWPSLog -Path $log_path -Cmdlet $script_name -Level Info -Message "Package Type: $($package_id.PackageType)" -Verbose
$i++
try
{
switch ($package_id.PackageType)
{
'Submittal'
{
$package = Get-DMSubmittalByPackageID -ConnectedProjectId $connected_project_id -IMSToken $token -PackageID $package_id.PackageId
Export-DMSubmittalsFiles -SubmittalPackages $package -ExportPath $export_path -ExportInProjectTree
}
'Transmittal'
{
$package = Get-DMTransmittalByPackageID -ConnectedProjectId $connected_project_id -IMSToken $token -PackageID $package_id.PackageId
Export-DMTransmittalsFiles -TransmittalPackages $package -ExportPath $export_path -ExportInProjectTree
}
'IncomingRFI'
{
$package = Get-DMIncomingRFIByPackageID -ConnectedProjectId $connected_project_id -IMSToken $token -PackageID $package_id.PackageId
Export-DMIncomingRFIsFiles -IncomingRFIPackages $package -ExportPath $export_path -ExportInProjectTree
}
'OutgoingRFI'
{
$package = Get-DMOutgoingRFIByPackageID -ConnectedProjectId $connected_project_id -IMSToken $token -PackageID $package_id.PackageId
Export-DMOutgoingRFIsFiles -OutgoingRFIPackages $package -ExportPath $export_path -ExportInProjectTree
}
'IncomingCorrespondence'
{
$package = Get-DMIncomingCorrespondenceByPackageID -ConnectedProjectId $connected_project_id -IMSToken $token -PackageID $package_id.PackageId
Export-DMIncomingCorrespondenceFiles -IncomingCorrespondencePackages $package -ExportPath $export_path -ExportInProjectTree
}
'OutgoingCorrespondence'
{
$package = Get-DMOutgoingCorrespondenceByPackageID -ConnectedProjectId $connected_project_id -IMSToken $token -PackageID $package_id.PackageId
Export-DMOutgoingCorrespondenceFiles -OutgoingCorrespondencePackages $package -ExportPath $export_path -ExportInProjectTree
}
Default {
Write-PWPSLog -Path $log_path -Cmdlet $script_name -Level Warn -Message "Invalid package type" -Verbose
}
}
($latest_files.Rows | Where-Object {$PSItem.PackageId -eq $package_id.PackageId}).Exported = $true
$updated_files_count++
}
catch
{
Write-PWPSLog -Path $log_path -Cmdlet $script_name -Level Error -Message "Failed to export $($package_id.PackageId)" -Verbose
Write-PWPSLog -Path $log_path -Cmdlet $script_name -Level Error -Message $_ -Verbose
Continue
}
}
#endregion
#region Export Metadata
$updated_metadata_count = 0
$i = 1
foreach ($package_id in $latest_remaining_metadata)
{
Write-PWPSLog -Path $log_path -Cmdlet $script_name -Level Info -Message "Metadata Type: $($package_id.PackageType) ($i/$latest_remaining_metadata_count)" -Verbose
$i++
try
{
switch ($package_id.PackageType)
{
'Submittal'
{
Export-DMSubmittalsReport -ConnectedProjectId $connected_project_id -IMSToken $token -SubmittalIds $(($latest_files.Rows | Where-Object {$PSItem.PackageType -eq $package_id.PackageType}).PackageId) -ExportPath $export_path -ExportInProjectTree
}
'Transmittal'
{
Export-DMTransmittalsReport -ConnectedProjectId $connected_project_id -IMSToken $token -TransmittalIds $(($latest_files.Rows | Where-Object {$PSItem.PackageType -eq $package_id.PackageType}).PackageId) -ExportPath $export_path -ExportInProjectTree
}
'IncomingRFI'
{
Export-DMIncomingRFIsReport -ConnectedProjectId $connected_project_id -IMSToken $token -IncomingRFIIds $(($latest_files.Rows | Where-Object {$PSItem.PackageType -eq $package_id.PackageType}).PackageId) -ExportPath $export_path -ExportInProjectTree
}
'OutgoingRFI'
{
Export-DMOutgoingRFIsReport -ConnectedProjectId $connected_project_id -IMSToken $token -OutgoingRFIIds $(($latest_files.Rows | Where-Object {$PSItem.PackageType -eq $package_id.PackageType}).PackageId) -ExportPath $export_path -ExportInProjectTree
}
'IncomingCorrespondence'
{
Export-DMIncomingCorrespondenceReport -ConnectedProjectId $connected_project_id -IMSToken $token -IncomingCorrespondenceIds $(($latest_files.Rows | Where-Object {$PSItem.PackageType -eq $package_id.PackageType}).PackageId) -ExportPath $export_path -ExportInProjectTree
}
'OutgoingCorrespondence'
{
Export-DMOutgoingCorrespondenceReport -ConnectedProjectId $connected_project_id -IMSToken $token -OutgoingCorrespondenceIds $(($latest_files.Rows | Where-Object {$PSItem.PackageType -eq $package_id.PackageType}).PackageId) -ExportPath $export_path -ExportInProjectTree
}
Default {
Write-PWPSLog -Path $log_path -Cmdlet $script_name -Level Warn -Message "Invalid package type" -Verbose
}
}
($latest_metadata.Rows | Where-Object {$PSItem.PackageType -eq $package_id.PackageType}).Exported = $true
$updated_metadata_count++
}
catch
{
Write-PWPSLog -Path $log_path -Cmdlet $script_name -Level Error -Message "Failed to export $($package_id.PackageId)" -Verbose
Write-PWPSLog -Path $log_path -Cmdlet $script_name -Level Error -Message $_ -Verbose
Continue
}
}
#endregion
#region Update Progress
if (($updated_files_count -gt 0) -or ($updated_metadata_count -gt 0))
{
Write-PWPSLog -Path $log_path -Cmdlet $script_name -Level Info -Message "Updating Progress" -Verbose
New-XLSXWorkbook -InputTables $latest_files,$latest_metadata -OutputFileName "$progress_path\pwdm_export_progress_$(Get-Date -Format "yyyy-MM-dd-HH-mm-ss").xlsx"
}
else
{
Write-PWPSLog -Path $log_path -Cmdlet $script_name -Level Info -Message "Export for $connected_project_id is complete" -Verbose
}
#endregion
# Check if it's the last item
if ($connected_project_id -eq $projectIds[-1]) {
Write-Host "All projects exported."
}
}
}
else {
Write-Host "Project IDs file not found."
}