2020年6月26日 星期五

【Powershell】Create task scheduler entry

用 PowerShell 建 scheduler task

CreateSSLCertificateSwitchSchedule.ps1
$trigger = @(

    $(New-ScheduledTaskTrigger -AtStartup -RandomDelay (New-TimeSpan -Minutes 2)),

    $(New-ScheduledTaskTrigger -Daily -At 3am))

$action = New-ScheduledTaskAction -Execute "D:\bin\SwitchSSLCertificateBack\SwitchSSLCertificateBack.exe"

Register-ScheduledTask -TaskName "SwitchSSLCertificateBack" -Trigger $trigger -User "SYSTEM" -Action $action

Trigger 有每天凌晨3點跟開機後兩分鐘兩種。

CreateSSLCertificateSwitchSchedule.bat,主要是第三列的 ps call
cd /d "%~dp0
Robocopy "." "D:\bin\SwitchSSLCertificateBack"
Powershell.exe -executionpolicy remotesigned -File  CreateSSLCertificateSwitchSchedule.ps1
@echo off
echo The batch has been finished. Press any key to exit.
pause >nul

沒有留言:

張貼留言