[英]PowerShell script failing when run from the Task Scheduler
在任務計划程序中運行時,從.bat文件調用的XML文件轉換為PDF的PowerShell腳本失敗,但在命令提示符下工作正常。
Word2PDF.ps1
:
$File = $args[0]
$Word = New-Object -ComObject Word.Application
$Doc = $Word.Documents.Open($File)
$Name = ($Doc.FullName).Replace(".xml", ".pdf")
# Save this File as a PDF in Word
$Doc.SaveAs([ref]$Name, 17)
$Doc.Close()
$Word.Quit()
ReplaceXMLAttachemtnswithPDF.bat
:
@echo off
rem Convert XML files and attachments to PDFs
setlocal EnableExtensions
setlocal EnableDelayedExpansion
cd /D "D:\FtxData\ebixprod\ER\Output"
set WORKFILE=C:\Temp\workfile.txt
dir /B *.xml > files.txt
for /F %%f in ('type files.txt') do (
echo %%f | ssed s/.xml/.pdf/ > !WORKFILE!
for /F %%g in ('type !WORKFILE!') do set newfile=%%g
powershell D:\Ebix\Fintechnix\Bin\Word2Pdf.ps1 D:\FtxData\!DB!\ER\Output\%%f
)
這應該將D:\\ FtxData \\ ebixprod \\ ER \\ Output中的所有.xml文件轉換為.pdf文件。
如果我在命令提示符中運行.bat文件,則工作正常。 當.bat文件在任務計划程序中運行時 - 當它遇到PowerShell調用時掛起
錯誤是:
您無法在空值表達式上調用方法。
這在使用PowerShell 2的先前服務器上運行良好。這是Windows 2016,因此PowerShell 5也是如此。
具有最高權限的運行任務沒有任何區別,因為不需要管理員運行。
請嘗試改變它。 ps1抱怨你沒有傳入參數。
Param(
$File
)
$Word=NEW-OBJECT -COMOBJECT WORD.APPLICATION
# open a Word document, filename from the directory
$Doc=$Word.Documents.Open($File)
# Swap out XML with PDF in the Filename
$Name=($Doc.Fullname).replace(".xml",".pdf")
# write-host $Name
# Save this File as a PDF in Word
$Doc.saveas([ref]$Name, 17)
$Doc.close()
$Word.Quit()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.