Wednesday, May 17, 2023
HomeSoftware TestingGrasp Powershell Scheduled Duties in Minutes: The Final Information

Grasp Powershell Scheduled Duties in Minutes: The Final Information


With PowerShell, you may simply automate varied duties and scripts by creating and managing scheduled duties. Through the use of PowerShell to handle your scheduled duties, it can save you time and be sure that your duties are executed constantly and reliably.

On this activity, we’ll discover the totally different strategies for creating and managing a PowerShell scheduled activity, together with the usage of cmdlets and the Home windows Process Scheduler GUI, in addition to greatest practices and troubleshooting ideas.

System Necessities to Make a PowerShell Scheduled Process

There are three necessities you have to in case you are making an attempt to create a scheduled activity in PowerShell – 

  1. A PC operating on Home windows 10 or a more recent model of the Working System (OS).
  2. Home windows PowerShell; any model after model 3.0 can be utilized for this
  3. A textual content line editor; We suggest utilizing Visible Studio Code or NotePad++.

Making a New Scheduled Process

In PowerShell, when making a scheduled activity, you’ve gotten the power to specify a number of key parts: the motion, set off, principal, and settings.

  • The motion specifies what ought to occur when the duty runs, similar to operating a PowerShell script, launching a program, or sending an e mail.
  • The set off determines when the duty ought to be executed, similar to at a selected time, when the pc begins, or when a selected occasion happens.
  • The principal defines the safety context beneath which the duty ought to run, similar to a selected consumer account or the system account.
  • The settings help you specify further choices for the duty, similar to whether or not to cease the duty if it runs for too lengthy, whether or not to run the duty even when the consumer shouldn’t be logged on, and extra.

Making a Process Motion

To create a activity motion in PowerShell, use the New-ScheduledTaskAction cmdlet to specify the motion kind and any further settings. For instance, to run a PowerShell script, use the -Execute parameter adopted by the trail to the script file. You may as well embrace arguments or specify the working listing. Right here is an instance:

$motion = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-File C:Scriptsbackup.ps1" -WorkingDirectory "C:Scripts"
Making a Task Action

As soon as the duty motion is outlined, use it as a parameter when creating the scheduled activity with New-ScheduledTaskTrigger and Register-ScheduledTask.

Together with a Set off

In PowerShell, a scheduled activity set off specifies when the duty ought to run. You may add a set off to a scheduled activity utilizing the New-ScheduledTaskTrigger cmdlet, which lets you specify the set off kind and related settings.

For instance, if you wish to run the duty each day at a selected time, you need to use the -Day by day parameter adopted by the -At parameter to specify the time of day. You may as well specify further settings for the set off, similar to the beginning time, finish time, and repetition interval.

As soon as the set off is outlined, you need to use it as a parameter when creating the scheduled activity with the Register-ScheduledTask cmdlet. Right here is an instance of learn how to add a set off to a scheduled activity:

$set off = New-ScheduledTaskTrigger -Day by day -At "6:00 AM" Register-ScheduledTask -TaskName "MyTask" -Motion $motion -Set off $set off -Person "DOMAINUserName" -Password "MyPassword"
Including a Trigger

This creates a set off that runs the duty each day at 6:00 AM and registers the duty with the title “MyTask”, utilizing the beforehand outlined motion and the required consumer credentials.

Process Registration

After defining the duty motion and set off, the ultimate step is to register the duty utilizing the Register-ScheduledTask cmdlet. This cmdlet takes a lot of parameters that help you specify varied settings for the scheduled activity, similar to the duty title, consumer credentials, and safety choices.

For instance, to register a scheduled activity with the title “MyTask” and consumer credentials “DOMAINUserName” and “MyPassword”, you need to use the next command:

Register-ScheduledTask -TaskName "MyTask" -Motion $motion -Set off $set off -Person "DOMAINUserName" -Password "MyPassword"

This command registers the scheduled activity with the required motion and set off and units the consumer credentials to run the duty. As soon as the duty is registered, it should seem within the Process Scheduler interface and could be managed like some other scheduled activity.

Executing your Process

As soon as a scheduled activity has been created and registered utilizing PowerShell, it should robotically run in accordance with the outlined set off. Nonetheless, you might also wish to manually run the duty outdoors of its scheduled time or confirm that it’s operating as anticipated.

To manually run a scheduled activity in PowerShell, you need to use the Begin-ScheduledTask cmdlet. This cmdlet permits you to specify the title of the duty and any further parameters which may be required by the duty itself.

For instance, to manually begin the duty “MyTask”, you need to use the next command:

Begin-ScheduledTask -TaskName "MyTask"

This command begins the required activity and executes its outlined motion. You may as well use the -Verbose parameter to show detailed details about the duty’s execution.

Managing a Pre-Current Scheduled Process

Managing your Person Account, Safety Choices, and Compatibility Settings

When creating and managing scheduled duties in PowerShell, you will need to think about varied consumer account, safety, and compatibility settings which will have an effect on the duty’s conduct.

For consumer account settings, you may specify which consumer account the duty ought to run beneath and whether or not or to not retailer the consumer’s password securely. You may as well specify whether or not the duty ought to run whether or not the consumer is logged on or not, and whether or not to run with the very best privileges.

For safety settings, you may specify whether or not the duty ought to be run with a restricted token and which safety principal the duty ought to be run as. You may as well specify the extent of safety required for the duty, similar to operating in a restricted mode or with enhanced safety.

For compatibility settings, you may specify which model of PowerShell to make use of when operating the duty, and whether or not the duty ought to be run in 32-bit mode or 64-bit mode.

All of those settings could be configured utilizing the Register-ScheduledTask cmdlet when creating or updating a scheduled activity. For instance, to create a scheduled activity that runs beneath the consumer account “DOMAINUserName” with the very best privileges and requires enhanced safety, you need to use the next command:

Register-ScheduledTask -TaskName "MyTask" -Motion $motion -Set off $set off -Person "DOMAINUserName" -RunLevel Highest -SecurityDescriptor $secDesc
Managing your User Account, Security Options, and Compatibility Settings

This command registers the scheduled activity with the required motion and set off, units the consumer account to run beneath, and configures the duty to run with the very best privileges and enhanced safety.

Adjusting the Process Set off

To switch the set off of an current scheduled activity, you first must retrieve the duty object utilizing the Get-ScheduledTask cmdlet. It will return a ScheduledTask object which you can then modify utilizing the Set-ScheduledTaskTrigger cmdlet.

For instance, to alter the set off of a scheduled activity named “MyTask” to run each day at 3:00 PM, you need to use the next instructions:

$activity = Get-ScheduledTask -TaskName "MyTask" $set off = New-ScheduledTaskTrigger -Day by day -At 3:00pm Set-ScheduledTaskTrigger -Process $activity -Set off $set off
Adjusting the Task Trigger

This retrieves the duty object for “MyTask”, creates a brand new each day set off that runs at 3:00 PM utilizing the New-ScheduledTaskTrigger cmdlet, after which units this new set off utilizing the Set-ScheduledTaskTrigger cmdlet.

You may as well modify different properties of the set off, similar to the beginning time, finish time, and repetition interval, by utilizing the suitable parameters with the New-ScheduledTaskTrigger cmdlet.

Deleting a Scheduled Process

Making a Scheduled Process Backup

Backing up a scheduled activity in PowerShell includes exporting its settings to an XML file. This may be helpful if you wish to create a backup of a activity’s configuration or switch it to a different pc.

To export a scheduled activity, you need to use the Export-ScheduledTask cmdlet. For instance, to export a activity named “MyTask” to a file named “MyTask.xml” within the present listing, you need to use the next command:

Export-ScheduledTask -TaskName "MyTask" -XmlFilePath ".MyTask.xml"
Making a Scheduled Task Backup

It will export the duty’s settings to an XML file within the present listing.

To import a activity from an XML file, you need to use the Register-ScheduledTask cmdlet with the -Xml parameter. For instance, to import a activity from a file named “MyTask.xml”, you need to use the next command:

Register-ScheduledTask -Xml (Get-Content material ".MyTask.xml" | Out-String) -TaskName "MyTaskCopy"
Making a Scheduled Task Backup

It will import the duty’s settings from the XML file and create a brand new activity named “MyTaskCopy”.

General, backing up a scheduled activity by exporting its settings to an XML file could be a helpful manner to make sure that you’ve gotten a replica of the duty’s configuration in case it must be restored or transferred to a different pc.

Eradicating a Scheduled Process

Deleting a scheduled activity in PowerShell is a simple course of that may be performed shortly and simply. To delete a scheduled activity in PowerShell, you need to use the Unregister-ScheduledTask cmdlet. The syntax is straightforward:

Unregister-ScheduledTask -TaskName "TaskName"

Substitute “TaskName” with the title of the duty that you just wish to delete.

Restoring a deleted Scheduled Process

If in case you have by chance deleted a scheduled activity, you may restore it by importing a backup of the duty.

To revive a deleted scheduled activity in PowerShell, you need to use the Import-ScheduledTask cmdlet. The syntax is:

Import-ScheduledTask -TaskName "TaskName" -Xml "C:pathtobackupTaskName.xml"

Substitute “TaskName” with the title of the duty that you just wish to restore, and “C:pathtobackupTaskName.xml” with the trail to the backup XML file.

After you have imported the scheduled activity, you need to be capable to see it within the Process Scheduler library and handle it as you usually would.

By now, you need to have been in a position to schedule the duty you need in your PC. For additional info, you may go to the scheduled activity web page on the Microsoft PowerShell webpage.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments