I have several scheduled tasks stored in Windows Server 2016. But none of them are executed at the appointed time. If I run them manually, they will run properly. I have also created some tasks in the SQL server that run manually but do not run according to schedule. What is the problem?
-
Are those tasks Powershell scripts? – Saaransh Garg Jan 31 '22 at 14:22
-
@SaaranshGarg Ok – hmahdavi Feb 01 '22 at 06:06
-
Not sure you got what I said. I was asking whether the tasks you've made were Powershell scripts. – Saaransh Garg Feb 01 '22 at 06:17
-
@SaaranshGarg Yes. See the screenshot – hmahdavi Feb 01 '22 at 06:31
2 Answers
That is happening because of Window's Execution policy, Which does not allow an unsigned powershell script to be run by another program by default. You need to change this policy for your tasks to work.
- Open an elevated Powershell prompt.
- Type
Set-ExecutionPolicy Unrestrictedto set the policy to Unrestricted. - Type
Get-ExecutionPolicyto verify the current settings for the execution policy.
Please note that changing this setting can be a security threat as malicious scripts can also be run by other programs. Instead you can try to write a Batch script spinoff of the same script(if it's possible), and then use that instead of this. Windows exeution policy don't block Batch scripts.
- 2,596
- 14
- 27
-
I checked this on other servers. And I realized that there is no difference between them in this case. There must be another reason for the problem – hmahdavi Feb 01 '22 at 07:06
I know this is old, but you likely need to digitally sign any PowerShell scripts. The advice to set the execution policy to unrestricted is terrible advice, and it's likely your security team would have prevented that from being changed anyway.
get-help Set-AuthenticodeSignature
e.g: Set-AuthenticodeSignature -FilePath .\Path_to_script.ps1 -Certificate (Get-ChildItem -Path Cert:\CurrentUser\My -CodeSigningCert)
(this assumes you have a code signing cert in your personal certificate store)
- 1
