I have a PowerShell script (launched through a batch file) that is run through Task Scheduler. When the task is configured to "run only when user is logged on" then it runs fine. When the task is configured to "run whether user is logged on or not" it still runs but PowerShell spits out errors.
The script takes a Word file, makes some formatting changes then exports it as a PDF file.
Here's the relevant batch code:
setlocal enabledelayedexpansion
for /R E:\Originals %%x in (*.doc) do (
powershell.exe -ExecutionPolicy Bypass -Command "& E:\Scripts\FormatReports.ps1 %%x"
)
setlocal disabledelayedexpansion
Here's the relevant PowerShell code:
#start Word
$word = New-Object -comobject Word.Application
#open file
$FilePath = $args[0]
$document = $word.Documents.Open($FilePath)
$word.Visible = $false
#modify orientation and margins
$document.PageSetup.Orientation = 1 #Set to Landscape
$document.PageSetup.TopMargin = $word.InchesToPoints(0.25)
$document.PageSetup.BottomMargin = $word.InchesToPoints(0.25)
$document.PageSetup.LeftMargin = $word.InchesToPoints(0.25)
$document.PageSetup.RightMargin = $word.InchesToPoints(0.25)
#save as a pdf file
$NewFilename = $FilePath.replace(".DOC","")
$document.saveas([ref] "$NewFilename.pdf", [ref] 17)
#close and exit Word
$document.close([ref]$false)
$word.Quit()
Here's the errors:
The property 'Orientation' cannot be found on this object. Verify that the property exists and can be set. At E:\\Scripts\\FormatReports.ps1:43 char:1
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : PropertyNotFound
The property 'TopMargin' cannot be found on this object. Verify that the property exists and can be set. At E:\\Scripts\\FormatReports.ps1:44 char:1
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : PropertyNotFound
The property 'BottomMargin' cannot be found on this object. Verify that the property exists and can be set. At E:\\Scripts\\FormatReports.ps1:45 char:1
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : PropertyNotFound
The property 'LeftMargin' cannot be found on this object. Verify that the property exists and can be set. At E:\\Scripts\\FormatReports.ps1:46 char:1
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : PropertyNotFound
The property 'RightMargin' cannot be found on this object. Verify that the property exists and can be set. At E:\\Scripts\\FormatReports.ps1:47 char:1
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : PropertyNotFound
I have only a basic knowledge of PowerShell scripts, so any help would be greatly appreciated!
It turned out to be the missing folder in the system profile, as described here:
Powershell script run from task scheduler unable to open word document
How to run a Windows 2008 task from the scheduler with "interact with desktop"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.