How do I execute a PowerShell script automatically using Windows task scheduler?

后端 未结 9 1711
甜味超标
甜味超标 2020-11-29 23:24

I have one PowerShell script which sends emails. I want to execute that script automatically, every 1 minute. How can I do it, using task scheduler?

Currently I have

相关标签:
9条回答
  • 2020-11-29 23:58

    You can use the Unblock-File cmdlet to unblock the execution of this specific script. This prevents you doing any permanent policy changes which you may not want due to security concerns.

    Unblock-File path_to_your_script
    

    Source: Unblock-File

    0 讨论(0)
  • 2020-11-30 00:01

    In my case, my script has parameters, so I set:

    Arguments: -Command "& C:\scripts\myscript.ps1 myParam1 myParam2"

    0 讨论(0)
  • 2020-11-30 00:03

    Instead of only using the path to your script in the task scheduler, you should start PowerShell with your script in the task scheduler, e.g.

    C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -NoLogo -NonInteractive -File "C:\Path\To\Your\PS1File.ps1"
    

    See powershell /? for an explanation of those switches.

    If you still get problems you should read this question.

    0 讨论(0)
  • 2020-11-30 00:04

    None of posted solutions worked for me. Workaround, which worked:

    create a run.bat and put inside powershell.exe -file "C:\...\script.ps1"

    then set Action to Program/Script: "C:\...\run.bat"

    0 讨论(0)
  • 2020-11-30 00:06

    I also could not launch scripts, after heavy searching nothing helped. No -ExecutionPolicy, no commands, no files and no difference between "" and ''.

    I simply put the command I ran in powershell in the argument tab: ./scripts.ps1 parameter1 11 parameter2 xx and so on. Now the scheduler works. Program: Powershell.exe Start in: C:/location/of/script/

    0 讨论(0)
  • 2020-11-30 00:08

    Create the scheduled task and set the action to:

    Program/Script: Powershell.exe

    Arguments: -File "C:\Users\MyUser\Documents\ThisisMyFile.ps1"

    0 讨论(0)
提交回复
热议问题