From 5af9c4bdbf4375c122717ea86e3817f49667e4ac Mon Sep 17 00:00:00 2001 From: Artur Jankowski Date: Fri, 29 Dec 2023 18:37:43 +0100 Subject: [PATCH] fix: Fix metadata extraction script --- .../Metadata-extraction.ps1 | 35 +++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/examples/Metadata Extraction/Metadata-extraction.ps1 b/examples/Metadata Extraction/Metadata-extraction.ps1 index c1164fa7..7882ead3 100644 --- a/examples/Metadata Extraction/Metadata-extraction.ps1 +++ b/examples/Metadata Extraction/Metadata-extraction.ps1 @@ -228,6 +228,7 @@ Function Start-Metadata-Extraction { } Write-Log "Output $($Entries | Out-String)" + $MetadataTemplatesHashmap = @{} ForEach ($Item in $Entries) { $ItemID = $Item.id @@ -263,13 +264,43 @@ Function Start-Metadata-Extraction { #Loop through each metadata entry to add additional folder info & separate according to metadata template ForEach ($MetadataValue in $Metadata) { + $TemplateKey = $MetadataValue."`$template" + + #Pull MetadataTemplate to get access to all it's fields and put it in a hashmap. + #Next, to the first object being added, we include all missing fields from the metadata template to ensure the corresponding columns are added to the CSV file. + if (!$MetadataTemplatesHashmap.ContainsKey($TemplateKey)) { + Write-Log "Pulling MetadataTemplate for templateKey: $TemplateKey" -output true -color Green + Try { + if (!$UserId) { + $MetadataTemplateResp = (box metadata-templates:get $TemplateKey --json 2>&1) + } else { + $MetadataTemplateResp = (box metadata-templates:get $TemplateKey --as-user=$UserId --json 2>&1) + } + + $MetadataTemplate = $MetadataTemplateResp | ConvertFrom-Json + $MetadataTemplatesHashmap[$TemplateKey] = $MetadataTemplate + + foreach ($MetadataTemplateField in $MetadataTemplatesHashmap[$TemplateKey].fields){ + #To maintain the continuity of fields from the metadata template in the resulting CSV file, we remove and then add them at the end + if ($MetadataValue.PSObject.Properties.Name -contains $($MetadataTemplateField.key)) { + $metadataFieldValue = $MetadataValue.$($MetadataTemplateField.key); + $MetadataValue.PSObject.Properties.Remove($($MetadataTemplateField.key)); + $MetadataValue | Add-Member -NotePropertyName "$($MetadataTemplateField.key)" -NotePropertyValue $metadataFieldValue; + } else { + #We add a field with an empty value so that it's included in the CSV file header, ensuring that subsequent items with this field will also be added + $MetadataValue | Add-Member -NotePropertyName "$($MetadataTemplateField.key)" -NotePropertyValue $null; + } + } + } Catch { + Write-Log "Could not get the metadata template for item. See error log for details." -errorMessage $MetadataTemplateResp -output True -color Red + } + } + #Append Object Info values: Name, Object Id, Type $MetadataValue | Add-Member -NotePropertyName "Name" -NotePropertyValue $Item.name; $MetadataValue | Add-Member -NotePropertyName "Object Id" -NotePropertyValue $Item.id; $MetadataValue | Add-Member -NotePropertyName "Type" -NotePropertyValue $Item.type; - $Templatekey = $MetadataValue."`$template" - #Export metadata values to separate csv's according to metadata template keys $MetadataValue | Export-Csv -Path ./MetadataTemplate_$Templatekey`.csv -Append -Force -NoTypeInformation Write-Log "Metadata saved to: MetadataTemplate_$Templatekey.csv" -output true -color Green