MediaValet.DAM.psm1
function Import-MvDamFilenameListFromCsv { [CmdletBinding()] param ( [parameter(Mandatory=$true,Position=0)] [string]$Path ) PROCESS { $csv = Import-Csv -Path $Path $filenameArray = $csv | Foreach {$_.Filename} return $filenameArray } } function Export-MvDamAssetInfo { [CmdletBinding()] param ( [parameter(Mandatory=$true,Position=0,ParameterSetName="From Filename List")] [string[]]$FilenameList, [parameter(Mandatory=$true,Position=0,ParameterSetName="From Asset ID List")] [string[]]$AssetIdList, [parameter(Mandatory=$true,Position=1)] [string]$Path ) PROCESS { $assets = @() if ($FilenameList) { $assets = Get-MvDamAssetInfo -FilenameList $FilenameList } if ($AssetIdList) { $assets = Get-MvDamAssetInfo -IdList $AssetIdList } $assets | Select -Property Id, FileName, Title, Description, @{N='Keywords';E={$_.Keywords -join ","}} | Export-Csv $Path -NoTypeInformation return $assets } } function Export-MvDamAssetAttribute { [CmdletBinding()] param ( [parameter(Mandatory=$true,Position=0)] [object[]]$AssetInfoList, [parameter(Mandatory=$false,Position=1)] [string[]]$AttributeList, [parameter(Mandatory=$true,Position=2)] [string]$Path ) PROCESS { if ($AssetInfoList.GetType().Name -ne "AssetInfoModel[]" -or $AssetInfoList.Count -eq 0) { Write-Error -Message "Please pass an AssetInfoModel array with one or more items to the `$AssetInfoList parameter" } #Get AttributeList from first AssetInfo object if AttributeList is not specified if ($AttributeList -eq $null) { $AttributeList = $AssetInfoList[0].Attributes | Where {-not $_.IsSystemProperty} | Foreach { $_.AttributeName} } else #validate the AttributeList against the first object in the list { $objAttrNames = $AssetInfoList[0].Attributes | Where {-not $_.IsSystemProperty} | Foreach { $_.AttributeName} $AttributeList | Foreach { if (-not $objAttrNames.Contains($_)) {Write-Error "Invalid attribute name $_ specified"} } } $assets = @() Foreach ($assetInfo in $AssetInfoList) { $asset = New-Object -TypeName PSObject $asset | Add-Member -MemberType NoteProperty -Name 'System.Id' -Value $assetInfo.Id $asset | Add-Member -MemberType NoteProperty -Name 'System.FileName' -Value $assetInfo.FileName $asset | Add-Member -MemberType NoteProperty -Name 'System.Title' -Value $assetInfo.Title $asset | Add-Member -MemberType NoteProperty -Name 'System.Description' -Value $assetInfo.Description Foreach($attributeName in $AttributeList) { $attrValue = $assetInfo.Attributes | Where {$_.AttributeName -eq $attributeName} | Select -Property AttributeValue $asset | Add-Member -MemberType NoteProperty -Name "$attributeName" -Value $attrValue.AttributeValue } $assets += $asset } $assets | Export-Csv -Path $Path -NoTypeInformation return $assets } } function Import-MvDamAssetAttributesFromCsv { [CmdletBinding()] param ( [parameter(Mandatory=$true,Position=0)] [string]$SourcePath, [parameter(Position=1)] [string]$LogPath ) PROCESS { $csv = @() $import = Import-Csv -Path $SourcePath if ($import.GetType().ToString() -eq "System.Object[]") { $csv = $import } else { $csv = @($import) } for($i=0;$i -lt $csv.Length; $i++) { $line = $csv[$i] $assetId = $line.('System.Id') $percentComplete = ($i/$csv.Length)*100 Write-Progress -Activity "Importing Asset Attributes" -Status "Updating attributes for Asset Id $assetId" -PercentComplete $percentComplete $txnInfo = New-MvDamTxnInfo -StartTime (Get-Date) -Id $assetId -Status "InProgress" -TxnType "Update-MvDamAssetAttribute" $attrKvps = @{} $line | Get-Member | Where {$_.MemberType -eq "NoteProperty"} | Foreach {if ($line.($_.Name) -ne "") {$attrKvps.Add($_.Name, $line.($_.Name))}} $errorInfo = $null $txnResult = Update-MvDamAssetAttribute -AssetId $assetId -Attributes $attrKvps -ErrorAction Continue -ErrorVariable $errorInfo $txnInfo.EndTime = Get-Date if ($errorInfo) { $txnInfo.Status = "Failed" $txnInfo.Notes = $errorInfo.Message } else { $txnInfo.Status = "Succeeded" } if ($LogPath) { $txnInfo | Export-Csv -Path $LogPath -Append } } Write-Progress -Activity "Importing Asset Attributes" -Completed return } } function Test-MvDamAssetAttributeCsv { [CmdletBinding()] param ( [parameter(Mandatory=$true,Position=0)] [string]$SourcePath, [parameter(Mandatory=$true,Position=1)] [string]$ResultPath ) PROCESS { $csv = @() $import = Import-Csv -Path $SourcePath if ($import.GetType().ToString() -eq "System.Object[]") { $csv = $import } else { $csv = @($import) } $assetsWithErrors = 0 $totalErrors = 0 for($i=0;$i -lt $csv.Length; $i++) { $line = $csv[$i] $assetId = $line.('System.Id') $percentComplete = ($i/$csv.Length)*100 Write-Progress -Activity "Validating Asset Attributes" -Status "Testing attributes for Asset Id $assetId" -PercentComplete $percentComplete $attrKvps = @{} $line | Get-Member | Where {$_.MemberType -eq "NoteProperty"} | Foreach {if ($line.($_.Name) -ne "") {$attrKvps.Add($_.Name, $line.($_.Name))}} $errorInfo = $null $txnResult = Test-MvDamAssetAttribute -AssetId $assetId -Attributes $attrKvps -ErrorAction Continue -ErrorVariable $errorInfo if ($txnResult.Errors -eq 0) { $line | Add-Member -MemberType NoteProperty -Name 'Errors' -Value "" } else { $assetsWithErrors++ $totalErrors += $txnResult.Errors $errorMsgs = ($txnResult.AttributeValidatonResults | Foreach ValidationResult) -join "; " $line | Add-Member -MemberType NoteProperty -Name 'Errors' -Value $errorMsgs } if ($i -eq 0) { $line | Export-Csv -Path $ResultPath -NoTypeInformation } else { $line | Export-Csv -Path $ResultPath -Append -NoTypeInformation } } Write-Progress -Activity "Validating Asset Attributes" -Completed Write-Host "Total Assets Validated:"$csv.Length Write-Host "Assets with Error(s):"$assetsWithErrors Write-Host "Total Errors Detected"$totalErrors if ($totalErrors -eq 0) { Write-Host "`r`nCONGRATULATIONS! Your asset attribute CSV file is ready to import." } return } } function Import-MvDamAssetKeyword { [CmdletBinding()] param ( [parameter(Mandatory=$true,Position=0)] [string]$Path ) PROCESS { $Assets = Import-Csv -Path $Path | Select Id, Keywords "Validating Ids" $Count = 0 foreach ($id in ($Assets | select Id)) { if ($id -match '<<[a-z]*>>') { $Count += 1; } } if ($Count -gt 0) { throw "$Count issues were detected with the Asset Ids provided. Please resolve this before continuing." } "Ids are valid" "Starting keyword import" $err = @() $lastErrorCount = 0 $errorLogPath = ".\UpdateKeywordErrors.csv" foreach ($asset in $Assets) { "AssetId: $asset.Id" if(!$asset.Keywords.Equals("")) { "Keywords: $asset.Keywords" $keywords = $asset.Keywords.Split(",") Update-MvDamAssetKeyword -AssetId $asset.Id -Keywords $keywords -ErrorAction Continue -ErrorVariable +err if ($err.Count -gt $lastErrorCount) { $updateKeywordError = New-Object -TypeName PSObject $updateKeywordError | Add-Member -NotePropertyName "Id" -NotePropertyValue $asset.Id $updateKeywordError | Add-Member -NotePropertyName "Keywords" -NotePropertyValue $asset.Keywords $updateKeywordError | Add-Member -NotePropertyName "Error" -NotePropertyValue $err[$lastErrorCount] $updateKeywordError | Export-Csv -Path $errorLogPath -NoTypeInformation -Append $lastErrorCount += 1 } } } "Import complete" } } |