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:24am

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:37am

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

March 31st, 2015 7:40am

Look at WorkFlow activity timeout here: help about_ActivityCommonParameters
Free Windows Admin Tool Kit Click here and download it now
March 31st, 2015 8:22am

$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?
March 31st, 2015 8:39am

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


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

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 18 minutes ago
March 31st, 2015 9:44am

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.
Free Windows Admin Tool Kit Click here and download it now
March 31st, 2015 9:47am

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

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:06am

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

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:07am

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

March 31st, 2015 11:36am

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 11:36am

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 Tuesday, March 31, 2015 1:50 PM
March 31st, 2015 1:43pm

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 Tuesday, March 31, 2015 1:50 PM
Free Windows Admin Tool Kit Click here and download it now
March 31st, 2015 1:43pm

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

April 1st, 2015 2:53am

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 23 hours 32 minutes ago
Free Windows Admin Tool Kit Click here and download it now
April 1st, 2015 3:34am

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 23 hours 24 minutes ago
April 1st, 2015 3:37am

Hi Alex,

I don't think it's about $job, but more probably about $saveFormat...

As you can see, the write-verbose you placed inside the scriptblock are now showing strings... without the variable values !
in fact, your job as its own scope, and can't see variables that exist in parent scope.

this change may resolve the problem :

$job= Start-Job -ScriptBlock { param($d, $nam, $num, $ct, $path, $sf)
Write-Verbose "$nam `t`t ( n $num of $ct)" -Verbose
$d.saveas([ref]"$path", [ref]$sf);
} -ArgumentList $doc, $name, $number, $count, $pdf_file, $saveFormat

please try it and let me know

Sbastien

Free Windows Admin Tool Kit Click here and download it now
April 1st, 2015 3:48am

Thanks for help. Well in this case I got no error but as early no converted files.. As you can see on screenshot I got Verbose on Russian (it goes after Remove-Job), but inside Start-Job this message is in English now. I have feeling source code inside Start-Job does not perform.. Maybe another way to set timeout? I am disappointed :(
  • Edited by 23 hours 3 minutes ago
April 1st, 2015 4:04am

Thanks for help. Well in this case I got no error but as early no converted files.. As you can see on screenshot I got Verbose on Russian (it goes after Remove-Job), but inside Start-Job this message is in English now. I have feeling source code inside Start-Job does not perform.. Maybe another way to set timeout? I am disappointed :(
  • Edited by 22 hours 55 minutes ago
Free Windows Admin Tool Kit Click here and download it now
April 1st, 2015 4:07am

I decided to try it by myself...

this works for me, please try and tell me if OK for you too :

$number = 0 
$count = $docs.Count
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");

       
    # м м    html 
    $pdf_file = $path_html + "\" + $name
    
    # м  (  pdf-  м doc)
    $pdf_file = [System.IO.Path]::ChangeExtension($pdf_file, '.htm')
   

$job= Start-Job -ScriptBlock { param($f, $nam, $num, $ct, $path, $sf)
	Write-Verbose "$nam `t`t ( n $num of $ct)" -Verbose
	$word = New-Object -ComObject Word.Application
	$word.Visible = $false
	$doc = $word.Documents.Open($f.FullName)
	$doc.SaveAs("$path", $sf);
	$doc.Close()
} -ArgumentList $file, $name, $number, $count, $pdf_file, $saveFormat
    

    Wait-Job $job -Timeout 30
    Receive-Job $job
    #$doc.close()
    remove-job $job
    Write-Verbose "end for $name ($number of $count)" -Verbose
}

April 1st, 2015 4:50am

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 Wednesday, April 01, 2015 7:35 AM
Free Windows Admin Tool Kit Click here and download it now
April 1st, 2015 7:33am

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 Wednesday, April 01, 2015 7:35 AM
April 1st, 2015 7:33am

Thanks for answer. I  have changed source as you wrote and in this time seems Job worked. I got error about Type Mismatch in SaveAs function and no converted files but the source in Start-Job{..} works. Thank you very much again.

Free Windows Admin Tool Kit Click here and download it now
April 1st, 2015 7:55am

Thanks for help. Well in this case I got no error but as early no converted files.. As you can see on screenshot I got Verbose on Russian (it goes after Remove-Job), but inside Start-Job this message is in English now. I have feeling source code inside Start-Job does not perform.. Maybe another way to set timeout? I am disappointed :(
  • Edited by Wednesday, April 01, 2015 8:04 AM
April 1st, 2015 8:03am

Thanks for help. Well in this case I got no error but as early no converted files.. As you can see on screenshot I got Verbose on Russian (it goes after Remove-Job), but inside Start-Job this message is in English now. I have feeling source code inside Start-Job does not perform.. Maybe another way to set timeout? I am disappointed :(
  • Edited by Wednesday, April 01, 2015 8:04 AM
Free Windows Admin Tool Kit Click here and download it now
April 1st, 2015 8:03am

I decided to try it by myself...

this works for me, please try and tell me if OK for you too :

$number = 0 
$count = $docs.Count
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");

       
    # м м    html 
    $pdf_file = $path_html + "\" + $name
    
    # м  (  pdf-  м doc)
    $pdf_file = [System.IO.Path]::ChangeExtension($pdf_file, '.htm')
   

$job= Start-Job -ScriptBlock { param($f, $nam, $num, $ct, $path, $sf)
	Write-Verbose "$nam `t`t ( n $num of $ct)" -Verbose
	$word = New-Object -ComObject Word.Application
	$word.Visible = $false
	$doc = $word.Documents.Open($f.FullName)
	$doc.SaveAs("$path", $sf);
	$doc.Close()
} -ArgumentList $file, $name, $number, $count, $pdf_file, $saveFormat
    

    Wait-Job $job -Timeout 30
    Receive-Job $job
    #$doc.close()
    remove-job $job
    Write-Verbose "end for $name ($number of $count)" -Verbose
}

  • Marked as answer by 23 hours 44 minutes ago
April 1st, 2015 8:46am

I decided to try it by myself...

this works for me, please try and tell me if OK for you too :

$number = 0 
$count = $docs.Count
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");

       
    # м м    html 
    $pdf_file = $path_html + "\" + $name
    
    # м  (  pdf-  м doc)
    $pdf_file = [System.IO.Path]::ChangeExtension($pdf_file, '.htm')
   

$job= Start-Job -ScriptBlock { param($f, $nam, $num, $ct, $path, $sf)
	Write-Verbose "$nam `t`t ( n $num of $ct)" -Verbose
	$word = New-Object -ComObject Word.Application
	$word.Visible = $false
	$doc = $word.Documents.Open($f.FullName)
	$doc.SaveAs("$path", $sf);
	$doc.Close()
} -ArgumentList $file, $name, $number, $count, $pdf_file, $saveFormat
    

    Wait-Job $job -Timeout 30
    Receive-Job $job
    #$doc.close()
    remove-job $job
    Write-Verbose "end for $name ($number of $count)" -Verbose
}

  • Marked as answer by Thursday, April 02, 2015 7:23 AM
Free Windows Admin Tool Kit Click here and download it now
April 1st, 2015 8:46am

I decided to try it by myself...

this works for me, please try and tell me if OK for you too :

$number = 0 
$count = $docs.Count
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");

       
    # м м    html 
    $pdf_file = $path_html + "\" + $name
    
    # м  (  pdf-  м doc)
    $pdf_file = [System.IO.Path]::ChangeExtension($pdf_file, '.htm')
   

$job= Start-Job -ScriptBlock { param($f, $nam, $num, $ct, $path, $sf)
	Write-Verbose "$nam `t`t ( n $num of $ct)" -Verbose
	$word = New-Object -ComObject Word.Application
	$word.Visible = $false
	$doc = $word.Documents.Open($f.FullName)
	$doc.SaveAs("$path", $sf);
	$doc.Close()
} -ArgumentList $file, $name, $number, $count, $pdf_file, $saveFormat
    

    Wait-Job $job -Timeout 30
    Receive-Job $job
    #$doc.close()
    remove-job $job
    Write-Verbose "end for $name ($number of $count)" -Verbose
}

  • Marked as answer by Thursday, April 02, 2015 7:23 AM
April 1st, 2015 8:46am

Alex,

quite surprising, it works for me... your call to SaveAs is quite different of mine, I don't use [ref]... could you try  with $doc.SaveAs("$path", $sf);

regards,

Sbastien

Free Windows Admin Tool Kit Click here and download it now
April 1st, 2015 11:56am

Hi Sbastien. Yes I tried to call as you and got error smth like "Use [ref]  blablabla". Then I remove

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

and $doc.saveas([ref]$path,[ref]$sf); change to $doc.saveas([ref]$path,[ref]10); 

Success, It works. I don't know where was my fault, I don't have a lot experience in PowerShell.. But now script looks like this and it works for me.

$number = 0 
foreach ($file in $docs)
{
    # м  м м 
    $name = $file.Name
    ++$number
    
    #$saveFormat = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], "wdFormatFilteredHTML");
    # м 
   
    
    # м м    html 
    $pdf_file = $path_html + "\" + $name
    # м  (  pdf-  м doc)
    $pdf_file = [System.IO.Path]::ChangeExtension($pdf_file, '.htm')
    
    # м
   $job= Start-Job -ScriptBlock { param($f, $nam, $num, $ct, $path)
        
    Write-Verbose "$nam `t`t ( $path, $input)" -Verbose
   $word = New-Object -ComObject Word.Application
    $word.Visible = $false
     $doc = $word.Documents.Open($f.FullName)    
     $doc.saveas([ref]$path, [ref]10);
    $doc.Close() 
    $word.Quit() 
    } -InputObject $saveformat -ArgumentList $file, $name, $number, $count, $pdf_file

     

Thank you for help.

April 2nd, 2015 2:45am

Hi Alex,

Glad to hear that it works for you now. Perhaps can you mark your question as answered.

Have a nice day.

Sbastien

Free Windows Admin Tool Kit Click here and download it now
April 2nd, 2015 3:02am

Hi Sbastien. Yes I tried to call as you and got error smth like "Use [ref]  blablabla". Then I remove

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

and $doc.saveas([ref]$path,[ref]$sf); change to $doc.saveas([ref]$path,[ref]10); 

Success, It works. I don't know where was my fault, I don't have a lot experience in PowerShell.. But now script looks like this and it works for me.

$number = 0 
foreach ($file in $docs)
{
    # м  м м 
    $name = $file.Name
    ++$number
    
    #$saveFormat = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], "wdFormatFilteredHTML");
    # м 
   
    
    # м м    html 
    $pdf_file = $path_html + "\" + $name
    # м  (  pdf-  м doc)
    $pdf_file = [System.IO.Path]::ChangeExtension($pdf_file, '.htm')
    
    # м
   $job= Start-Job -ScriptBlock { param($f, $nam, $num, $ct, $path)
        
    Write-Verbose "$nam `t`t ( $path, $input)" -Verbose
   $word = New-Object -ComObject Word.Application
    $word.Visible = $false
     $doc = $word.Documents.Open($f.FullName)    
     $doc.saveas([ref]$path, [ref]10);
    $doc.Close() 
    $word.Quit() 
    } -InputObject $saveformat -ArgumentList $file, $name, $number, $count, $pdf_file

     

Thank you for help.

  • Marked as answer by 23 hours 44 minutes ago
April 2nd, 2015 6:41am

Hi Sbastien. Yes I tried to call as you and got error smth like "Use [ref]  blablabla". Then I remove

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

and $doc.saveas([ref]$path,[ref]$sf); change to $doc.saveas([ref]$path,[ref]10); 

Success, It works. I don't know where was my fault, I don't have a lot experience in PowerShell.. But now script looks like this and it works for me.

$number = 0 
foreach ($file in $docs)
{
    # м  м м 
    $name = $file.Name
    ++$number
    
    #$saveFormat = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], "wdFormatFilteredHTML");
    # м 
   
    
    # м м    html 
    $pdf_file = $path_html + "\" + $name
    # м  (  pdf-  м doc)
    $pdf_file = [System.IO.Path]::ChangeExtension($pdf_file, '.htm')
    
    # м
   $job= Start-Job -ScriptBlock { param($f, $nam, $num, $ct, $path)
        
    Write-Verbose "$nam `t`t ( $path, $input)" -Verbose
   $word = New-Object -ComObject Word.Application
    $word.Visible = $false
     $doc = $word.Documents.Open($f.FullName)    
     $doc.saveas([ref]$path, [ref]10);
    $doc.Close() 
    $word.Quit() 
    } -InputObject $saveformat -ArgumentList $file, $name, $number, $count, $pdf_file

     

Thank you for help.

  • Marked as answer by Thursday, April 02, 2015 7:23 AM
Free Windows Admin Tool Kit Click here and download it now
April 2nd, 2015 6:41am

Hi Sbastien. Yes I tried to call as you and got error smth like "Use [ref]  blablabla". Then I remove

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

and $doc.saveas([ref]$path,[ref]$sf); change to $doc.saveas([ref]$path,[ref]10); 

Success, It works. I don't know where was my fault, I don't have a lot experience in PowerShell.. But now script looks like this and it works for me.

$number = 0 
foreach ($file in $docs)
{
    # м  м м 
    $name = $file.Name
    ++$number
    
    #$saveFormat = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], "wdFormatFilteredHTML");
    # м 
   
    
    # м м    html 
    $pdf_file = $path_html + "\" + $name
    # м  (  pdf-  м doc)
    $pdf_file = [System.IO.Path]::ChangeExtension($pdf_file, '.htm')
    
    # м
   $job= Start-Job -ScriptBlock { param($f, $nam, $num, $ct, $path)
        
    Write-Verbose "$nam `t`t ( $path, $input)" -Verbose
   $word = New-Object -ComObject Word.Application
    $word.Visible = $false
     $doc = $word.Documents.Open($f.FullName)    
     $doc.saveas([ref]$path, [ref]10);
    $doc.Close() 
    $word.Quit() 
    } -InputObject $saveformat -ArgumentList $file, $name, $number, $count, $pdf_file

     

Thank you for help.

  • Marked as answer by Thursday, April 02, 2015 7:23 AM
April 2nd, 2015 6:41am

FYI - thisis the correct way to get the enum:

$saveFormat=[Microsoft.Office.Interop.Word.WdSaveFormat]'wdFormatFilteredHTML'

OR

$saveFormat=[Microsoft.Office.Interop.Word.WdSaveFormat]::wdFormatFilteredHTML

You can use tab expansion to list all enums.

You can also list5 all names defined in an enum like this:

[enum]::GetNames([Microsoft.Office.Interop.Word.WdSaveFormat])

Example with other object:

[enum]::GetNames([stringsplitoptions])

Free Windows Admin Tool Kit Click here and download it now
April 2nd, 2015 12:26pm

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

Other recent topics Other recent topics