I am new to Powershell scripting. I am trying to run a script to convert an Excel spreadsheet into a PDF file. This is the script I am using:
$excelInputPath = <Path1>;
$pdfOutputPath = <Path2>;
$xlFixedFormat = "Microsoft.Office.Interop.Excel.xlFixedFormatType" -As [type];
$objExcel = New-Object -ComObject excel.application;
$objExcel.visible = $False;
$workbook = $objExcel.workbooks.open($excelInputPath, 3);
$workbook.ExportAsFixedFormat($xlFixedFormat::xlTypePDF, $pdfOutputPath);
$objExcel.Workbooks.close();
$objExcel.Quit()
On its own, it executes perfectly. I now wish to add a timeout and so I am trying to run it as a job. However, this job throws an execution error which I catch via the Recieve-Job command:
$excelInputPath = <Path1>;
$pdfOutputPath = <Path2>;
$xlFixedFormat = "Microsoft.Office.Interop.Excel.xlFixedFormatType" -As [type];
$objExcel = New-Object -ComObject excel.application;
$objExcel.visible = $False;
$workbook = $objExcel.workbooks.open($excelInputPath, 3);
$job = Start-Job -ScriptBlock {$workbook.ExportAsFixedFormat($xlFixedFormat::xlTypePDF, $pdfOutputPath);}
$job | Wait-Job -Timeout 10;
If ($job.State -eq 'Running')
{$job.StopJob();
$objExcel.Workbooks.close();
$objExcel.Quit()
throw "Error encountered: Operation timed out"}
else
{if($job.Childjobs[0].Error)
{$objExcel.Workbooks.close();
$objExcel.Quit()
$job | Receive-Job}
else
{$job | Receive-Job;
$objExcel.Workbooks.close();
$objExcel.Quit()}
}
The output I receive is quoted below. The message is in German and roughly translates to: "It is not possible to execute a method for an expression that has a NULL".
Id Name PSJobTypeName State HasMoreData Location Command
-- ---- ------------- ----- ----------- -------- -------
57 Job57 BackgroundJob Completed True localhost $workbook.ExportAsFixe...
Es ist nicht möglich, eine Methode für einen Ausdruck aufzurufen, der den NULL hat.
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
+ PSComputerName : localhost
Another confusing thing here is that the Job -State is reflected as complete even when it doesn't perform the conversion and throws this error message.
Thank you! And apart from an explanation on why this is happening, I would appreciate any input on how I can perform this task better.