[英]PowerShell doesn't run as scheduled task when “Run whether user is logged on or not” is set
I have a PowerShell script that I want to schedule.我有一个想要安排的 PowerShell 脚本。 Although it appears to run in Task Scheduler, it doesn't actually run my script.
尽管它似乎在 Task Scheduler 中运行,但实际上并没有运行我的脚本。 I have discovered that this is due to the fact that I have set the task to "Run whether user is logged on or not".
我发现这是因为我已将任务设置为“无论用户是否登录都运行”。 If I turn this off, the task runs the script.
如果我关闭它,任务将运行脚本。
I found an article last week that suggested this was because I was using user-based variables but didn't suggest how I might get round it.上周我发现一篇文章表明这是因为我使用了基于用户的变量,但没有建议我如何解决它。
Can anyone help with my script below?任何人都可以帮助我下面的脚本吗?
#UPDATE DATA IN EXCEL FILES
#THEN CREATE PDF FILE
[string]$path = "\\server\folder\" #Path to Excel spreadsheets to save to PDF
[string]$savepath = "\\server\folder\Reports\"
[string]$dToday = Get-Date -Format "yyyyMMdd"
$xlFixedFormat = "Microsoft.Office.Interop.Excel.xlFixedFormatType" -as [type]
$excelFiles = Get-ChildItem -Path $path -include *.xls, *.xlsx -recurse
# Create the Excel application object
$objExcel = New-Object -ComObject excel.application
$objExcel.visible = $false #Do not open individual windows
foreach($wb in $excelFiles)
{
# Path to new PDF with date
$filepath = Join-Path -Path $savepath -ChildPath ($wb.BaseName + "_" + $dtoday + ".pdf")
# Open workbook - 3 refreshes links
$workbook = $objExcel.workbooks.open($wb.fullname, 3)
$workbook.RefreshAll()
# Give delay to save
Start-Sleep -s 5
# Save Workbook
$workbook.Saved = $true
"saving $filepath"
#Export as PDF
$workbook.ExportAsFixedFormat($xlFixedFormat::xlTypePDF, $filepath)
$objExcel.Workbooks.close()
}
$objExcel.Quit()
I've had some problems while using the Microsoft.Office.Interop.Excel
through a Scheduled Task
too in the past.过去,我在通过
Scheduled Task
使用Microsoft.Office.Interop.Excel
时也遇到了一些问题。 So we switched to using the following module:所以我们切换到使用以下模块:
This module makes use of the EPPLUS DLL which can easily be used from within a scheduled task as there is no need for an installed version of Microsoft Excel
, which is in any case not a desirable solution on a server anyhow.该模块使用EPPLUS DLL ,它可以在计划任务中轻松使用,因为不需要安装
Microsoft Excel
版本,无论如何,这在任何情况下都不是服务器上的理想解决方案。
So i suggest you to have a look at this as it will surely help you.所以我建议你看看这个,因为它肯定会对你有所帮助。
If you still wish to use the Microsoft.Office.Interop.Excel
object, you need to read this so you know that you need to manually create the following 2 folders:如果您仍希望使用
Microsoft.Office.Interop.Excel
对象,则需要阅读本文,以便您知道需要手动创建以下 2 个文件夹:
C:\Windows\System32\config\systemprofile\Desktop
C:\Windows\SysWOW64\config\systemprofile\Desktop
When these folders are available on your system, the Scheduled Task
will most likely run correctly.当这些文件夹在您的系统上可用时,
Scheduled Task
很可能会正确运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.