Windows Powershell表达式,作为作业抛出错误,但执行其他操作
我不熟悉Powershell脚本。我正在尝试运行一个脚本,将Excel电子表格转换为PDF文件。这是我正在使用的脚本:Windows Powershell表达式,作为作业抛出错误,但执行其他操作,windows,powershell,Windows,Powershell,我不熟悉Powershell脚本。我正在尝试运行一个脚本,将Excel电子表格转换为PDF文件。这是我正在使用的脚本: $excelInputPath = <Path1>; $pdfOutputPath = <Path2>; $xlFixedFormat = "Microsoft.Office.Interop.Excel.xlFixedFormatType" -As [type]; $objExcel = New-Object -ComObject excel.appli
$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()
这里另一个令人困惑的事情是,作业状态被反映为已完成,即使它不执行转换并抛出此错误消息
谢谢大家!!除了解释为什么会发生这种情况外,我非常感谢大家对我如何更好地完成这项任务的意见 脚本块不知道变量
$workbook
,$xlFixedFormat::xlTypePDF
和$pdfoutput路径
。
您需要使用-ArgumentList
参数将这些作为参数发送
尝试:
谢谢你,西奥。这就解释了我收到的错误消息。不幸的是,我现在收到另一条错误消息:方法调用失败,因为[Deserialized.System.\uuuComObject{000208da-0000-0000-c000-0000000000 46}]不包含名为“ExportAsFixedFormat”的方法。@Anupam我现在无法测试,但您可以尝试
参数([ref]$工作簿,…
并使用-ArgumentList[ref]调用吗$workbook,…
?感谢您的建议-完全有道理。我尝试过,但出现以下错误:无法处理参数“workbook”上的参数转换。参数中应为引用类型。我检查了如何传递引用以启动作业。下面回答中的注释说明了这可能不可能可悲的。
$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()}
}
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
$scriptBlock = {
param($workbook, $format, $outPath)
$workbook.ExportAsFixedFormat($format, $outPath)
}
$job = Start-Job -ScriptBlock $scriptBlock -ArgumentList $workbook, $xlFixedFormat::xlTypePDF, $pdfOutputPath