Windows Task Scheduler Run Powershell Script(Windows计划任务执行powershell脚本)

最近遇到一个需求:把powershell脚本添加到Windows计划任务中每天执行。

刚开始看起来比较简单,但是当直接把ps1文件添加到Task Scheduler时,不能正常运行

首先在powershell中开启运行脚本的权限为无限制的环境:

然后在计划任务中执行任务进行测试,结果没能实现powershell中的功能。查看执行日志,发现运行脚本的实例居然是:

Task Scheduler launch task "\PowerShell\powershell" , instance "C:\Windows\System32\notepad.exe"  with process ID 71824.

所以没有效果也正常。

于是查阅了一些资料,有一种办法是直接让ps1文件能够双击运行,这个没有尝试。

这里用了一个.bat脚本调用powershell脚本,从而实现计划任务执行powershell脚本。

然后设置任务要执行的文件为text.bat

然后执行任务,成功完成powershell中的程序。

test.ps1源码:(调用test.ps1文件)

powershell E:\test.ps1

test.bat源码:(在E盘创建一个Atest的文件夹)

New-Item E:\Atest -type directory
原文地址:https://www.cnblogs.com/tylertang/p/3075352.html