Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- file named - Itunes_Restore-R-PC-SC.ps1
- what it does
- - reads rating, playedcount, skippedcount values stored in comment field
- - compares to itunes values, writes 'em to itunes if different
- currently runs 1000 tracks in ~ 4 minutes if they all need to be changed
- currently runs 1000 tracks in ~ 30 seconds if none need to be changed
- that seems to be fast enuf to auto-run daily with task scheduler
- #>
- # function list start
- # write to console/file/both
- function WriteTo($what, [String]$where, [String]$filename)
- {
- if($where -ne "console" -and $filename -eq "")
- {
- $filename = -join ($Env:Temp, "\", "PowerShell_LogFile_", (get-date).ToString("yyyy-MM-dd"), ".log")
- }
- switch($where.ToLower())
- {
- "console" {$what | Write-Host}
- "file" {Add-Content -path $filename -value $what}
- "both" {Add-Content -path $filename -value $what -passthru}
- default {$what | Write-Host}
- }
- }
- # function list end
- $ItunesAlreadyRunning = (Get-Process | Where-Object {$_.ProcessName -eq "iTunes"}) -match "iTunes"
- $iTunesApp = New-Object -comObject iTunes.Application
- $iMainLibrary = $iTunesApp.LibraryPlaylist
- $iTracks = $iMainLibrary.Tracks
- $NumTracks = $iTracks.Count
- # itunes no longer uses "1" to represent ONLY the audio file "kind".
- # - that also is used for books and possibly other file "kind" info.
- # - switched to using "KindAsString" instead of "Kind" value for media type test.
- $AudioTrack = "MPEG audio file"
- $LogFile = -join ($Env:Temp, "\", $myinvocation.mycommand.name, "_", (get-date).ToString("yyyy-MM-dd"), ".log")
- $StartTime = Get-Date
- # swap these two when you wanna test just a few tracks
- $x = $NumTracks
- #$x = 10
- $ProcessedCount = 0
- $ChangedCount = 0
- while($x -gt 0)
- {
- $ProcessedCount ++
- $iCurrentItem = $iTracks.Item($x)
- if($iCurrentItem.KindAsString -eq $AudioTrack)
- {
- $Rating = $iCurrentItem.Rating
- $PlayedCount = $iCurrentItem.PlayedCount
- $SkippedCount = $iCurrentItem.SkippedCount
- $Location = $iCurrentItem.Location
- $Comment = $iCurrentItem.Comment
- $Changed = $False
- # get the stored rating
- if($Comment -match "rating=(\d{1,3})")
- {
- $ComRating = [int] $Matches[1]
- }
- else
- {
- $ComRating = $Null
- }
- # get the stored playedcount
- if($Comment -match "playedcount=(\d{1,})")
- {
- $ComPlayedCount = [int] $Matches[1]
- }
- else
- {
- $ComPlayedCount = $Null
- }
- # get the stored skippedcount
- if($Comment -match "skippedcount=(\d{1,})")
- {
- $ComSkippedCount = [int] $Matches[1]
- }
- else
- {
- $ComSkippedCount = $Null
- }
- # restore 'em if they aint the same
- if($ComRating -ne $Null -and $Rating -ne $ComRating)
- {
- $iCurrentItem.Rating = $ComRating
- $Changed = $True
- }
- if($ComPlayedCount -ne $Null -and $PlayedCount -ne $ComPlayedCount)
- {
- $iCurrentItem.PlayedCount = $ComPlayedCount
- $Changed = $True
- }
- if($ComSkippedCount -ne $Null -and $SkippedCount -ne $ComSkippedCount)
- {
- $iCurrentItem.SkippedCount = $ComSkippedCount
- $Changed = $True
- }
- if($Changed)
- {
- $ChangedCount ++
- }
- #show what was done
- "track index = $x"
- Write-Host ""
- "OLD track rating = $Rating"
- "OLD track playedcount = $PlayedCount"
- "OLD track skippedcount = $SkippedCount"
- Write-Host ""
- "stored rating = $ComRating"
- "stored playedcount = $ComPlayedCount"
- "stored skippedcount = $ComSkippedCount"
- Write-Host ""
- Write-Host ("itunes rating =", $iCurrentItem.Rating)
- Write-Host ("itunes playedcount =", $iCurrentItem.PlayedCount)
- Write-Host ("itunes skippedcount =", $iCurrentItem.SkippedCount)
- "track comment = $Comment"
- "track location = $Location"
- Write-Host "******************************"
- Write-Host ""
- }
- $x --
- }
- # if itunes was NOT already running
- # and itunes is NOT currently playing
- # then we started it and we are not using it, so shut it down
- if((-not $ItunesAlreadyRunning) -and ($itunesApp.Playerstate -eq "0"))
- {
- $iTunesApp.Quit()
- }
- $StopTime = Get-Date
- $TotalTime = $StopTime - $StartTime
- WriteTo (-join ("started at = ", $StartTime)) "both" $LogFile
- WriteTo (-join ("stopped at = ", $StopTime)) "both" $LogFile
- WriteTo (-join ("total time = ", $TotalTime.ToString())) "both" $LogFile
- WriteTo (-join ("processed = ", $ProcessedCount)) "both" $LogFile
- WriteTo (-join ("changed = ", $ChangedCount)) "both" $LogFile
- WriteTo (-join ("log file = ", $LogFile)) "both" $LogFile
- WriteTo ("") "both" $LogFile
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement