Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Update value in the Size field in Sitecore media items.
#
# Fix value '123 x 123' in Size field to the proper image size (bytes).
# Author: Robert Senktas
#
$sitecorePath = "master:/sitecore/media library"
$processedItems = Get-ChildItem -Path $sitecorePath -Recurse | ForEach-Object {
if ( $_.Size -match "x" ) {
$mediaItem = New-Object "Sitecore.Data.Items.MediaItem" $_
[Sitecore.Resources.Media.Media] $media = [Sitecore.Resources.Media.MediaManager]::GetMedia($mediaItem);
$stream = $media.GetStream()
$reportItem = [PSCustomObject]@{
ID = $_.ID
Path = $_.Paths.Path
Size = $_.Size
Length = $stream.Length
}
$_.Editing.BeginEdit()
$_["Size"] = $stream.Length
$_.Editing.EndEdit()
$_ | Publish-Item -PublishMode SingleItem -Verbose
return $reportItem
}
}
# Generate a report
Import-Function -Name ConvertTo-Xlsx
[byte[]]$outobject = $processedItems |
Select-Object -Property ID, Path, Size, Length |
ConvertTo-Xlsx
Out-Download -Name "report-change-image-size-.xlsx" -InputObject $outobject
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment