Timeout in a loop

Hi all. I have next loop (it's a part of script):

$word = New-Object -ComObject Word.Application
$word.Visible = $false
$number = 0 
foreach ($file in $docs)
{
    # м  м м 
    $name = $file.Name
    ++$number
    Write-Verbose "$name `t`t ( $number  $count)" -Verbose
    $saveFormat = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], "wdFormatFilteredHTML");

    # м 
    $doc = $word.Documents.Open($file.FullName)
    
    # м м    html 
    $pdf_file = $path_html + "\" + $doc.Name
    
    # м  (  pdf-  м doc)
    $pdf_file = [System.IO.Path]::ChangeExtension($pdf_file, '.htm')
    
    # м
    $doc.saveas([ref]"$pdf_file", [ref]$saveFormat);        
    
    # м 
    $doc.Close()
}

It converts .doc (.docx) files to .html using Word. Sometimes the file is not converted and script hangs. How can I add maybe timeout to this loop if step does not end for 30 seconds -> close file and begin next step(file). Or maybe own version. Thank you very much for help.

March 31st, 2015 7:22am

Hi

Perhaps can you try to do the job in a powershell JOB, wait a few second and abort it if it didn't complete.

You'll find an example base on Start-Job and Remove-Job here :

http://stackoverflow.com/questions/21176487/adding-a-timeout-to-batch-powershell

hope it helps

Free Windows Admin Tool Kit Click here and download it now
March 31st, 2015 7:38am

Look at WorkFlow activity timeout here: help about_ActivityCommonParameters
March 31st, 2015 8:21am

$word = New-Object -ComObject Word.Application
$word.Visible = $false
$number = 0 
foreach ($file in $docs)
{
$job = Start-Job -ScriptBlock  {
Write-Verbose "$name `t`t ( $number  $count)" -Verbose
    # м  м м 
    $name = $file.Name
    ++$number
    Write-Verbose "$name `t`t ( $number  $count)" -Verbose
    $saveFormat = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], "wdFormatFilteredHTML");

    # м 
    $doc = $word.Documents.Open($file.FullName)
    
    # м м    html 
    $pdf_file = $path_html + "\" + $doc.Name
    
    # м  (  pdf-  м doc)
    $pdf_file = [System.IO.Path]::ChangeExtension($pdf_file, '.htm')
    
    # м
     $doc.saveas([ref]"$pdf_file", [ref]$saveFormat);
    
    
    # м 
    $doc.Close() 
    }
    
Write-Verbose "$name `t`t ( $number  $count)" -Verbose
   
    Wait-Job $job -Timeout $timeout
   Stop-Job $job
    Remove-Job $job
}

In this case Job is completed but the souce code in $job does not perform! First and second Write-Verbose I don't see, only last.. Why? Where am I wrong?
Free Windows Admin Tool Kit Click here and download it now
March 31st, 2015 8:38am

I suggest that your job is this single line :

$doc.saveas([ref]"$pdf_file", [ref]$saveFormat);

because it seems to be the operation that can hang...

Another thing : I can't see the value you give to $timeout. Did you forget it or did you define it outside the code extract you shawn here ?

Sbastien


March 31st, 2015 9:02am

Hi Sbastien. Yes, I forgot this $timeout. Well I reconfigure Job only with $doc.saveas([ref]"$pdf_file", [ref]$saveFormat); but situation the same - no files were saved.  Also I enclosed the screenshot. As you can see first I had only one Verbose message but it source 2.. Verbose goes fisrt and then Job completed message. But in code Job set before second Verbose message.. Cannot understand( Thank for help, Alex.
  • Edited by gconnect1 17 hours 50 minutes ago
Free Windows Admin Tool Kit Click here and download it now
March 31st, 2015 9:45am

Sorry, last message was my. )
March 31st, 2015 9:54am

Okay, I think I see where the problem is.

first of all, don't worry about console messages order : while you're running jobs that write concurrently with your main script, messages can be interleaved (due to the exclusive access to screen output, they have to wait until it's free)

secondly, I suggest you don't close the word document before the save operation is over (or timed out). You can rewrite the end like that :

$job = Start-Job {...} Wait-Job $job -timeout 30 $doc.Close()

Stop-Job $job
...


Doing so, you'll give it a chance to do the convertion during the 30 seconds, before closing the doc.

Hope it helps

Sbastien

Free Windows Admin Tool Kit Click here and download it now
March 31st, 2015 10:04am

Sorry, It didn't help :(
March 31st, 2015 10:19am

Sorry to hear that, Alex.

The $doc.save method probably failed in the job. As you jobs have the property HasMoreData set to True, could you get and post the results here please ?

you can get it by receive-job (but you have to do it before remove-job)

Sbastien

Free Windows Admin Tool Kit Click here and download it now
March 31st, 2015 11:05am

Hi Sbastien. Thanks for help, here please the result receive-Job. Variable does not exist.. It's about $job??

April 1st, 2015 2:51am

OK, when I put  

$saveFormat = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], "wdFormatFilteredHTML"); 

to Start-Job{..} I got another error:

Unable to find type [Microsoft.Office.Interop.Word.WdSaveFormat] make sure the assembly containing this type..

is  this Job not transparent?


  • Edited by 5 minutes ago
Free Windows Admin Tool Kit Click here and download it now
April 1st, 2015 3:35am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics