From b900fdb984345c0fdfeb09e531f6a358ad8c3b8e Mon Sep 17 00:00:00 2001 From: Artur Jankowski Date: Thu, 1 Feb 2024 09:24:49 +0100 Subject: [PATCH] feat: Add additional `UseDisplayName` flag to metadata extraction script (#515) --- .../Metadata-extraction.ps1 | 33 +++++++++++-------- examples/Metadata Extraction/README.md | 2 ++ 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/examples/Metadata Extraction/Metadata-extraction.ps1 b/examples/Metadata Extraction/Metadata-extraction.ps1 index 7882ead3..7a142d99 100644 --- a/examples/Metadata Extraction/Metadata-extraction.ps1 +++ b/examples/Metadata Extraction/Metadata-extraction.ps1 @@ -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 ) ############################################################################# @@ -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 @@ -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 { @@ -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; diff --git a/examples/Metadata Extraction/README.md b/examples/Metadata Extraction/README.md index d58d02e8..258948d1 100644 --- a/examples/Metadata Extraction/README.md +++ b/examples/Metadata Extraction/README.md @@ -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 @@ -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