Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么PS1脚本中的PS1调用在Powershell中执行时运行,而在作为Windows计划任务运行时不执行?_Windows_Powershell_Scheduled Tasks_Call - Fatal编程技术网

为什么PS1脚本中的PS1调用在Powershell中执行时运行,而在作为Windows计划任务运行时不执行?

为什么PS1脚本中的PS1调用在Powershell中执行时运行,而在作为Windows计划任务运行时不执行?,windows,powershell,scheduled-tasks,call,Windows,Powershell,Scheduled Tasks,Call,我正在使用Powershell v4.0(来自Get-Host cmdlet)运行Powershell脚本(TaskScript.ps1),文件版本:6.3.9600.17415(Windows资源管理器文件属性)。我正在尝试从Windows Server 2012 R2上的定期计划任务运行此脚本;TaskScript.ps1执行以下操作: & .\OtherScript1.ps1 #... log messages to LogFile ... & .\OtherScript2

我正在使用Powershell v4.0(来自Get-Host cmdlet)运行Powershell脚本(TaskScript.ps1),文件版本:6.3.9600.17415(Windows资源管理器文件属性)。我正在尝试从Windows Server 2012 R2上的定期计划任务运行此脚本;TaskScript.ps1执行以下操作:

& .\OtherScript1.ps1
#... log messages to LogFile ...
& .\OtherScript2.ps1
#... log more messages to LogFile ..
在Powershell中手动调用时,整个TaskScript.ps1(包括对OtherScript1.ps1和OtherScript2.ps1的调用)将按预期执行,并按预期执行对日志文件的记录;也就是说,在完成OtherScript.ps1和OtherScript2.ps1之后

但是,当从Windows计划任务运行TaskScript.ps1时,将运行脚本并立即更新日志文件,而无需调用OtherScript1或OtherScript2

我已经检查过了:

  • 在手动运行脚本和手动执行任务之前,日志文件已清除
  • 手动运行脚本会按以下方式执行所有包含的脚本: 预期(每个下标都有自己的日志记录,已检查)
  • 当手动执行时,日志文件被写入(在适当的时间)(我已经观察了输出的过程)
  • 计划任务正在运行并调用TaskScript.ps1(每当任务运行时,日志文件都会更新)
  • 运行任务时,TaskScript.ps1未执行其他脚本(无论任务是手动运行还是计划运行)--OtherScript1.ps1和OtherScript2.ps1的日志记录在任务运行时不会触发,但在直接从Powershell中手动运行TaskScript.ps1时会触发<代码>ie.powershell.exe。\TaskScript.ps1
  • 日志文件在通过任务执行时立即写入。其他两个脚本没有延迟;在Powershell中执行时会发生这种情况
我尝试了几种方法来调用TaskScript.ps1中的脚本,包括上面的调用运算符语法、Start Process cmdlet和点源(在这种情况下可以工作,但不是首选)

我还尝试使用任务中的
-ExecutionPolicy Bypass
参数临时调整计划任务上的执行策略,认为可能由于执行策略的原因,无法从脚本中调用调用

某些用户遇到Windows任务计划程序不喜欢本地驱动器上的脚本文件的问题。如果相关,my TaskScript.ps1、OtherScript1.ps1和OtherScript.ps2脚本(以及其他脚本)位于本地VHD(虚拟磁盘)上的P:\PathTo\scripts\TaskScript.ps1

我的任务是这样的:

Program/script: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Add arguments (optional): -NoProfile -NoLogo -NonInteractive -ExecutionPolicy Bypass -Command "& Path\To\TaskScript.ps1"

如果TaskScript.ps1是从Windows计划任务运行的,但如果在Powershell中手动调用,则运行正常,为什么OtherScript1.ps1和Otherscript2.ps1不会执行?

您是否将任务的工作目录(或“启动目录”)设置为包含
OtherScript1.ps1
的文件夹?啊!为无知道歉。我以为“开始”是指别的。这里的单词选择不是很好,但我现在已经设置了目录,下次有机会运行它时应该可以工作了。将报告并关闭此项(如果是)。谢谢