简体   繁体   中英

PowerShell script that modifies Word file doesn't work with Task Scheduler when user isn't logged on

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

  • $document.PageSetup.Orientation = 1 #Set to Landscape
  •  + 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

  • $document.PageSetup.TopMargin = $word.InchesToPoints(0.25)
  •  + 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

  • $document.PageSetup.BottomMargin = $word.InchesToPoints(0.25)
  •  + 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

  • $document.PageSetup.LeftMargin = $word.InchesToPoints(0.25)
  •  + 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

  • $document.PageSetup.RightMargin = $word.InchesToPoints(0.25)
  •  + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : PropertyNotFound

I have only a basic knowledge of PowerShell scripts, so any help would be greatly appreciated!

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM