Skip to content

Commit

Permalink
fix: Fix metadata extraction script (#514)
Browse files Browse the repository at this point in the history
  • Loading branch information
arjankowski authored Jan 3, 2024
1 parent f4bf7af commit 2fad540
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions examples/Metadata Extraction/Metadata-extraction.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ Function Start-Metadata-Extraction {
}

Write-Log "Output $($Entries | Out-String)"
$MetadataTemplatesHashmap = @{}

ForEach ($Item in $Entries) {
$ItemID = $Item.id
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2fad540

Please sign in to comment.