Hey Guys -
I’m an avid user of Plex which hosts / streams local media to many of my devices. Unfortunately, it’s internal mechanism for updating its libraries isn’t too robust. It has an option to trigger library updates when changes are detected, however; my media resides on a mapped drive therefore does not work. As it includes a CLI utility which can trigger library updates using parameters, I thought I’d just write a solution myself.
At the end of this post is the test script I started which should recursively monitor a folder for new .mp4 files. Upon finding one, it sets it to $path then does the following:
- Determines the library to update based on the path
- Executes the update
- Logs to a file
- Displays a notification in Windows
Before proceeding, I wanted to ask a couple of questions, please…
- My Movies path has almost 5,500 folders - each with ~25 files - a lot to monitor. Would the monitoring script I have planned be efficient enough and/or use too many resources if so?
- If acceptable or if a better solution is suggested, what would be the best way to have it monitor multiple paths and specific file extensions?
Test Script
# Monitor Path for New RAR Files $folder = "D:\- Media -\Movies" $filter = "*.mp4" $Watcher = New-Object IO.FileSystemWatcher $folder, $filter -Property @{ IncludeSubdirectories = $true NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite' } $onCreated = Register-ObjectEvent $Watcher Created -SourceIdentifier MovieMonitor -Action { $path = $Event.SourceEventArgs.FullPath#Variables
$plexdatafolderroot = “D:\Plex Media Server”
$database = “$plexdatafolderroot\Plug-in Support\Databases\com.plexapp.plugins.library.db”
$plexscanner = “C:\Program Files (x86)\Plex\Plex Media Server\Plex Media Scanner.exe”
$logfile = “D:\App Logs\Sonarr\Script Import\Sonarr-PlexScan.log”
$tvmain = “*TV Shows*”
$moviemain = “*Movies*”#Determine Library to Update
If ($path -like $tvmain ) { $libraryid = 2 }
If ($path -like $moviemain ) { $libraryid = 6 }#Execute Library Scan for Show Path
& “$plexscanner” --scan --refresh --section $libraryid --directory $path#Write to Log File
write-output “$path” “$libraryid” | add-content $logfile#Display Notification
Add-Type -AssemblyName System.Windows.Forms
$global:balloon = New-Object System.Windows.Forms.NotifyIcon
$path = (Get-Process -id $pid).Path
$balloon.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path)
$balloon.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::Warning
$balloon.BalloonTipText = “Plex has started scanning $path!”
$balloon.BalloonTipTitle = “Plex Scan Triggered!”
$balloon.Visible = $true
$balloon.ShowBalloonTip(5000)
}