To set up a task in Windows Task Scheduler using PowerShell to run your script every 10 minutes, follow these steps:
First, save your script to a file, such as
C:\path\to\your\script.ps1
.Run PowerShell as an administrator.
Use the following command to create the job:
$action = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument '-NoProfile -NoLogo -NonInteractive -ExecutionPolicy Bypass -File C:\path\to\your\script.ps1'
$trigger = New-ScheduledTaskTrigger -AtStartup -RepetitionInterval ([TimeSpan]::FromMinutes(10)) -RepetitionDuration ([TimeSpan]::FromDays(36500))
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName "YourTaskName" -Description "Description of your task"
In this code:
-Execute 'powershell.exe'
: specifies to executepowershell.exe
.-Argument
: the arguments that are passed to PowerShell. This tells PowerShell to run your script without an interactive shell and bypass the runtime policy (which may be required for automatic script execution).-AtStartup
: start the job at startup (you can change this to a different point in time if you want).-RepetitionInterval
: repeat the job every 10 minutes.-RepetitionDuration
: the duration of the repetition (this is set to 24 hours, but you can change this or omit it altogether to run the job indefinitely).YourTaskName
: the name of your job in the job scheduler. You can change it to something more appropriate.
After executing this command, a job will be created and will start running every 10 minutes. You can check and manage the task through the Task Scheduler GUI in Windows.
If you want to view all jobs created in the job scheduler, use the command:
Get-ScheduledTask | Format-Table -Property TaskName,State
Deleting a Task
To delete a task, use the command:
Unregister-ScheduledTask -TaskName "YourTaskName" -Confirm:$false
Change Task
If you decide to make changes to an already created task, it is best to delete the old task and create a new one with updated parameters.
Closure
Server monitoring — a critical task to ensure the stability of your IT infrastructure. Using the Windows Job Scheduler in combination with PowerShell, you can automate checks and notifications, quickly tracking down potential problems. However, this approach requires care and a security conscious approach to prevent vulnerabilities.