Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix metadata extraction script #514

Merged
merged 1 commit into from
Jan 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading