我有一个powershell脚本,可以将docx转换为pdf。当在Windows10的用户会话中运行时,它运行起来没有任何问题。但是,我需要在windows服务中运行它。
$docx_filename = 'C:\temp\doc2pdf.docx'
$pdf_filename = 'C:\temp\doc2pdf1.pdf'
$word_app = New-Object -ComObject Word.Application
$document = $word_app.Documents.Open($docx_filename)
$document.SaveAs([ref] $pdf_filename, [ref] 17)
$document.Close()
$word_app.Quit()使用以下命令调用该脚本
powershell.exe -executionpolicy Bypass -NoProfile -NoLogo -NonInteractive -WindowStyle Hidden -File -File "docx2pdf.ps1" -docxFilename "docx2pdf.docx" -pdfFilename "docx2pdf.pdf"使用PSSet-DebugPSS2调试-Trace 2时,会记录以下内容
DEBUG: 14+ >>>> $word_app = New-Object -ComObject Word.Application
DEBUG: ! SET $word_app = 'Microsoft.Office.Interop.Word.ApplicationClass'.
DEBUG: 16+ >>>> $document = $word_app.Documents.Open($docxFilename)如何让它在Windows10服务中工作?
谢谢
发布于 2020-01-15 17:59:44
您必须创建一个批处理文件,它可以触发powershell脚本,如下所示:
@echo off
Powershell.exe set-executionpolicy remotesigned -File ScriptPath将其另存为“startps.bat”
然后,您可以使用.sc命令创建一个windows服务,方法是如下所示的批处理文件路径:
Sc create PSScriptService Displayname= "What you like" binpath= "Startps.bat" start= auto发布于 2020-01-15 21:14:42
您必须在service's Log On tab上设置帐户。请确保指定一个帐户,您可以在该帐户中以交互方式登录并成功运行Powershell脚本(即安装Word的位置)。
但是,即使有了这样的更改,您仍然可能会遇到麻烦,因为Office是not entirely suitable for operation in a Windows Service。有些东西能用,但有些东西不行。试一试,让我们知道你的进展如何。
https://stackoverflow.com/questions/59748723
复制相似问题