PowerShell for PMM Assets

This script will copy posters and backgrounds from your Plex movie folders to the PMM Assets folder.

Assumtions:
– Movies are in folders named matching Plex Standards
– The following settings should be in your config.yml under settings:

asset_directory:
config/assets/collections
config/assets/movies
asset_folders: true
asset_depth: 0
create_asset_folders: true
prioritize_assets: true
dimensional_asset_rename: false
download_url_assets: false
show_missing_season_assets: false
show_missing_episode_assets: false
show_asset_not_needed: true



$SubDB = @(
	[pscustomobject]@{DataFolder="/mnt/Storage/Staging-MV"},
	[pscustomobject]@{DataFolder="/mnt/Storage/4K_Movies"},
	[pscustomobject]@{DataFolder="/mnt/Storage/007_Movies"},
        [pscustomobject]@{DataFolder="/mnt/Storage/ActionDrama"},
	[pscustomobject]@{DataFolder="/mnt/Storage/Comedy"},
	[pscustomobject]@{DataFolder="/mnt/Storage/Ani_Movies"},
	[pscustomobject]@{DataFolder="/mnt/Storage/Documentaries"},
	[pscustomobject]@{DataFolder="/mnt/Storage/Foreign_films"},
	[pscustomobject]@{DataFolder="/mnt/Storage/MusicVideos"},
	[pscustomobject]@{DataFolder="/mnt/Storage/SciFiFantasy"}
	)

	$Log = "/mnt/Storage/PlexScripts/Logs/assets.log"
	$stream = [System.IO.StreamWriter] $Log

	$AssetsFolder = "/mnt/Media01B/Plex-Meta-Manager/config/assets/movies/"
	$startTime = Get-Date
	$Counter = 0
	$Updated = 0

$SubDB | ForEach-object {
	$DataFolder = $_.DataFolder
	$Files = Get-ChildItem -Path $DataFolder -Directory 
		
    $Files | foreach-object {
		$FullName = $_.FullName
		$DirectoryName = $_.Parent
		$BaseName= $_.BaseName
		$Counter++
		$FullName = $FullName.tostring()
		$DirectoryName = $DirectoryName.tostring()
		$MovieFolder = $AssetsFolder + $BaseName

		$OldFanartJpg = $DirectoryName + "/" +  $BaseName + "/fanart.jpg"
		$OldPosterJpg = $DirectoryName +  "/" +  $BaseName + "/poster.jpg"
		$OldFanartPng = $DirectoryName + "/" +  $BaseName + "/fanart.png"
		$OldPosterPng = $DirectoryName +  "/" +  $BaseName + "/poster.png"
					
		if (Test-Path -Path $OldFanartJpg){
			$OldFanart = $OldFanartJpg
			$NewBackground = $MovieFolder + "/background.jpg"}

		if (Test-Path -Path $OldPosterJpg){
			$OldPoster = $OldPosterJpg
			$Newposter = $MovieFolder + "/poster.jpg"}

		if (Test-Path -Path $OldFanartPng){
			$OldFanart = $OldFanartPng
			$NewBackground = $MovieFolder + "/background.png"}
	
		if (Test-Path -Path $OldPosterPng){
			$OldPoster = $OldPosterPng
			$Newposter = $MovieFolder + "/poster.png"}
	
	Write-Host "====================================================="
	Write-Host $("Movie Name: "+$BaseName)
	$stream.WriteLine("=====================================================")
	$stream.WriteLine("Movie Name: "+$BaseName)

		If  (Test-Path -Path $MovieFolder) {
			$stream.WriteLine("Folder Exists")
		}ELSE{
			# Create Movie folder in assets
			Write-Host "Create Folder" -ForeGroundColor Green
			$stream.WriteLine("Create Folder")
			New-Item -Path $("$AssetsFolder") -Name $($BaseName) -ItemType Directory -Force
			$Updated++
		}

		# Onnly runs if Debug switched on
		Write-Debug "Debug Info"
		Write-Debug "================================"
		Write-Debug $OldPoster 
		Write-Debug $OldFanart 
		Write-Debug $NewPoster
		Write-Debug $NewBackground 
		Write-Debug $MovieFolder

###################################################################################################
	# Check if background exists in Movie folder
		if (Test-Path -Path $OldFanart){
			#Check if background exists in assets folder
            If (Test-Path -Path $($NewBackground)) {
			# If background does exist, compare Movie and assets background. if they dont match,
			# copy Movie background to assests
				$stream.WriteLine("Background Exists")
				$BackgroundHashSrc = Get-FileHash $OldfanArt -Algorithm "SHA256"
				$BackgroundHashDest = Get-FileHash $NewBackground -Algorithm "SHA256"
					If ($BackgroundHashSrc.hash -ne $BackgroundHashDest.hash){
						Write-Host "Updating Background" -ForeGroundColor Green
						$stream.WriteLine("Updating Background")
						cp $OldFanart $NewBackground
						$Updated++
					}ELSE{Out-file -FilePath $log -append -inputobject "Background Matches"}

			}ELSE{
				# if background is not in assets, copy it from Movie to assets
				Write-Host "Copying BackGround" -ForeGroundColor Green
				$stream.WriteLine("Copying Background")
				cp $OldFanart $NewBackground
				$Updated++}
		}ELSE{$stream.WriteLine("Movie Background does not exist")
		Write-Host "Movie Background does not exist" -ForeGroundColor Red}
###################################################################################################
	# Check if poster exists in Movie folder
		if (Test-Path -Path $OldPoster){
			#Check if poster exists in assets folder
            If (Test-Path -Path $Newposter) {
			# If poster does exist, compare Movie and assets poster. if they dont match,
			# copy Movie poster to assests
				$stream.WriteLine("Poster Exists")
				$PosterHashSrc = Get-FileHash $OldPoster -Algorithm "SHA256"
				$PosterHashDest = Get-FileHash $NewPoster -Algorithm "SHA256"
					If ($PosterHashSrc.hash -ne $PosterHashDest.hash){
						Write-Host "Updating Poster" -ForeGroundColor Green
						$stream.WriteLine("Updating Poster")
						cp $OldPoster $Newposter
						$Updated++
					}ELSE{$stream.WriteLine("Poster Matches")}

			}ELSE{
				# if poster is not in assets, copy it from Movie to assets
				Write-Host "Copying Poster" -ForeGroundColor Green
				$stream.WriteLine("Copying Poster")
				cp $OldPoster $Newposter
				$Updated++}
		}ELSE{$stream.WriteLine("Movie Poster does not exist")
		Write-Host "Movie Poster does not exist" -ForeGroundColor Red}
###################################################################################################
	$stream.WriteLine("Directory Name:"+$DirectoryName)
	$stream.WriteLine("Full Name:"+$MovieFolder)
	$stream.WriteLine($OldFanart)
	$stream.WriteLine($NewBackground)
	$stream.WriteLine($OldPoster)
	$stream.WriteLine($Newposter)
	} # Files
} # ForEach Object
	$endTime = Get-Date
	$executionTime = $endTime - $startTime
	Write-Host "====================================================="
	Write-Host "Script execution time: "$executionTime
	Write-Host "Total Processed:"$Counter
	Write-Host "Total Updated: "$Updated
	$stream.WriteLine("=====================================================")
	$stream.WriteLine("Script execution time: "+$executionTime)
	$stream.WriteLine("Total Processed:"+$Counter)
	$stream.WriteLine("Total Updated: "+$Updated)
	$stream.close()

Leave a Reply

Your email address will not be published. Required fields are marked *