itunes - RPC_E_SERVERCALL_RETRYLATER during powershell automation -


i'm using powershell automate itunes find error handling / waiting com objects handling less optimal.

example code

#cause rpc error $itunes = new-object -comobject itunes.application $librarysource = $itunes.librarysource # "playlist" objects main sections foreach ($plist in $librarysource.playlists) {   if($plist.name -eq "library") {     $library = $plist   } } {   write-host -foregroundcolor green "running loop"   foreach ($track in $library.tracks)   {      foreach ($foundtrack in $library.search("$track.name", 5)) {        # nothing... don't care...        write-host "." -nonewline      }   } } while(1)  #end  

go itunes , makes pop message - in case go party shuffle , banner "party shuffle automatically blah blah...." "do not display" message.

at point if running script repeatedly:

+      foreach ($foundtrack in $library.search( <<<< "$track.name", 5)) { exception calling "search" "2" argument(s): "the message filter indicated application busy. (exception hresult: 0x8001010a (rpc_e_server call_retrylater))" @ c:\documents , settings\me\my documents\example.ps1:17 char:45 +      foreach ($foundtrack in $library.search( <<<< "$track.name", 5)) { exception calling "search" "2" argument(s): "the message filter indicated application busy. (exception hresult: 0x8001010a (rpc_e_server call_retrylater))" @ c:\documents , settings\me\my documents\example.ps1:17 char:45 

if waited until you had dialog box before running example instead you'll repeatedly:

running loop cannot call method on null-valued expression. @ c:\documents , settings\me\example.ps1:17 char:45 +      foreach ($foundtrack in $library.search( <<<< "$track.name", 5)) { 

that'll because $library handle invalid.

if example doing important - converting tracks , deleting old ones, not handling error correctly fatal tracks in itunes. want harden code handles itunes being busy , silently retry until has success. suggestions?

here's function retry operations, pausing in between failures:

function retry( [scriptblock]$action, [int]$wait=2, [int]$maxretries=100 ) {   $results = $null    $currentretry = 0   $success = $false   while( -not $success ) {     trap {       # set status variables @ function scope.       set-variable -scope 1 success $false       set-variable -scope 1 currentretry ($currentretry + 1)        if( $currentretry -gt $maxretries ) { break }        if( $wait ) { start-sleep $wait }       continue     }      $success = $true     $results = . $action   }    return $results } 

for first error in example, change inner foreach loop this:

$foundtracks = retry { $library.search( "$track.name", 5 ) } foreach ($foundtrack in $foundtracks) { ... } 

this uses default values $wait , $maxretries, attempt call $library.search 100 times, waiting 2 seconds between each try. if retries fail, last error propagate outer scope. can set $erroractionpreference stop prevent script executing further statements.


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -