Last active
March 11, 2020 15:30
-
-
Save RobsonAutomator/ce64075fb0d20a34ad7fb20f722e5bf0 to your computer and use it in GitHub Desktop.
Update value in the Size field in Sitecore media items.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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