简体   繁体   English

设置“无论用户是否登录都运行”时,PowerShell 不会作为计划任务运行

[英]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.

相关问题 Powershell:将计划任务设置为在用户未登录时运行 - Powershell: Set a Scheduled Task to run when user isn't logged in Powershell Set-ADUser 不在计划任务中运行 - Powershell Set-ADUser doesn't run in scheduled task 无法使用具有选项“仅在用户登录时运行”的 powershell 创建计划任务 - Cannot create Scheduled task with powershell that has option “run only when user is logged on” 使用PowerShell使用“运行是否用户已登录”选项来安排任务创建 - Schedule task creation with option “”Run whether user is logged in or not“” using powershell 如何Powershell任务管理器的脚本“无论用户是否登录都运行” - How to Powershell a script for Task Manager to "run whether a user is logged on or not" 在“运行是否登录用户”模式下,使用任务计划程序运行Powershell脚本会冻结 - Running Powershell script using Task Scheduler freezes when in “Run whether user is logged in or not” mode 无法设置“运行用户是否登录” - Unable to set “Run whether user is logged on or not” PowerShell 计划任务:打开 PowerShell 和要运行的脚本但不执行脚本 - PowerShell scheduled task: opens PowerShell and script to be run but doesn't execute script powershell脚本可在run.exe中运行,但不能作为计划任务运行 - powershell script works in run.exe but doesn't work as scheduled task 使用任务计划程序计划的Powershell脚本无法运行…脚本运行正常 - Powershell script scheduled using Task scheduler doesn't run…script runs fine
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM