Skip to content

Commit

Permalink
feat: Add additional UseDisplayName flag to metadata extraction scr…
Browse files Browse the repository at this point in the history
…ipt (#515)
  • Loading branch information
arjankowski authored Feb 1, 2024
1 parent 2fad540 commit b900fdb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
33 changes: 19 additions & 14 deletions examples/Metadata Extraction/Metadata-extraction.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ param (
[string]$FolderID = "",

# Set to a specific Box user id if you would like to pull metadata as a specific user instead of the current user
[string]$UserId = ""
[string]$UserId = "",

# If enabled, the "displayName" of the metadata template field will be used as the header instead of the "key"
[switch]$UseDisplayName = $false
)

#############################################################################
Expand Down Expand Up @@ -229,6 +232,7 @@ Function Start-Metadata-Extraction {

Write-Log "Output $($Entries | Out-String)"
$MetadataTemplatesHashmap = @{}
$HeaderFieldName = If ($UseDisplayName) { 'displayName' } Else { 'key' }

ForEach ($Item in $Entries) {
$ItemID = $Item.id
Expand Down Expand Up @@ -267,7 +271,6 @@ Function Start-Metadata-Extraction {
$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 {
Expand All @@ -279,23 +282,25 @@ Function Start-Metadata-Extraction {

$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
}
}

#Prepare the MetadataValue object by setting its fields name based on the passed UseDisplayName flag,
#which determines the name of the field used in the variable HeaderFieldName.
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.$HeaderFieldName)" -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.$HeaderFieldName)" -NotePropertyValue $null;
}
}

#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;
Expand Down
2 changes: 2 additions & 0 deletions examples/Metadata Extraction/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Extracts metadata details for all the files and folders in any Box folder and sa
## 1. Script Parameters
1. Set the [folderID][folderID-param] you want the script to scan for metadata details.
2. Optional: To run the script as another user, set [userId][UserId-param] parameter.
3. Optional: Use the [-UseDisplayName][UseDisplayName-param] switch when running the script to ensure that the headers of the resulting file correspond to the `displayName` field instead of the `key` in the metadata template.


## 2. Run the script
Expand Down Expand Up @@ -76,3 +77,4 @@ This project is a collection of open source examples and should not be treated a
[oauth-guide]: https://developer.box.com/guides/cli/quick-start/
[FolderID-param]: /examples/Metadata%20Extraction/Metadata-extraction.ps1#L11
[UserID-param]: /examples/Metadata%20Extraction/Metadata-extraction.ps1#L14
[UseDisplayName-param]: /examples/Metadata%20Extraction/Metadata-extraction.ps1#L17

0 comments on commit b900fdb

Please sign in to comment.