How to ensure that there is always just one open document when running scripts

For powerJobs 19 or newer:

Open coolOrange.Applications.psm1 located in "C:\ProgramData\coolOrange\powerJobs\Modules"

For powerJobs 18 or earlier:

Open Setup_Job.ps1 located in "C:\ProgramData\coolOrange\powerJobs"

Add following line of code under the Register-Application cmdlet if you are using Inventor:

$powerJobs.Applications['Inventor'].RestartOptions.DocumentCloseCounts=1
If you are using InventorServer add this line of code under the Register-Application cmdlet:
$powerJobs.Applications['InventorServer'].RestartOptions.DocumentCloseCounts=1
This sets the Close-Document counter to 1, that means every time a Close-Document is executed, the application restarts and therefore you should always have just one open document. Note that Close-Document has to be executed for this to work. To ensure that it will always be executed there are 3 possibilities:
  1. Add Close-Document to the Setup_Job.ps1 which gets executed before every job
  2. Add Close-Document at the beginning of your job
  3. If the job crashes, Close-Document will not be executed. In that case you can catch the exception with a try catch and add Close-Document in the catch block:
try {
<your code>
} catch {
Close-Document
}
Remarks

This can affect the speed of the jobs being executed as the application is restarted after every Close-Document

See also