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

后端 未结 9 1712
甜味超标
甜味超标 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-30 00:08

    After several hours of test and research over the Internet, I've finally found how to start my PowerShell script with task scheduler, thanks to the video Scheduling a PowerShell Script using Windows Task Scheduler by Jack Fruh @sharepointjack.

    Program/script -> put full path through powershell.exe

    C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe
    

    Add arguments -> Full path to the script, and the script, without any " ".

    Start in (optional) -> The directory where your script resides, without any " ".

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

    Here is an example using PowerShell 3.0 or 4.0 for -RepeatIndefinitely and up:

    # Trigger
    $middayTrigger = New-JobTrigger -Daily -At "12:40 AM"
    $midNightTrigger = New-JobTrigger -Daily -At "12:00 PM"
    $atStartupeveryFiveMinutesTrigger = New-JobTrigger -once -At $(get-date) -RepetitionInterval $([timespan]::FromMinutes("1")) -RepeatIndefinitely
    
    # Options
    $option1 = New-ScheduledJobOption –StartIfIdle
    
    $scriptPath1 = 'C:\Path and file name 1.PS1'
    $scriptPath2 = "C:\Path and file name 2.PS1"
    
    Register-ScheduledJob -Name ResetProdCache -FilePath $scriptPath1 -Trigger  $middayTrigger,$midNightTrigger -ScheduledJobOption $option1
    Register-ScheduledJob -Name TestProdPing -FilePath $scriptPath2 -Trigger $atStartupeveryFiveMinutesTrigger
    
    0 讨论(0)
  • 2020-11-30 00:14
    1. Open the created task scheduler

    2. switch to the “Action” tab and select your created “Action”

    3. In the Edit section, using the browser you could select powershell.exe in your system32\WindowsPowerShell\v1.0 folder.

         Example -C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
      
    4. Next, in the ‘Add arguments’ -File parameter, paste your script file path in your system.

          Example – c:\GetMFAStatus.ps1
      

    This blog might help you to automate your Powershell scripts with windows task scheduler

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