PowerShell Tiny Project 9 - Scheduling Jobs

PowerShell Tiny Project 9 - Scheduling Jobs

Your script is functional and meets expectations, yet the essence of scripting lies in its automation—triggered by schedules or specific events. Whether for data extraction or administrative tasks, the goal is to set it and forget it. This tiny project emphasizes just that: executing your script autonomously, without your constant oversight.

$msg = "Provide your credential for running your desired job"
$credential = $Host.UI.PromptForCredential("Task username and password", $msg, "$env:userdomain\$env:username", $env:userdomain)
$scriptPath = "C:\users\$env:USERNAME\Downloads\LMSETL.ps1"
$name = "LMSETL"
$arguments = "-NoProfile -ExecutionPolicy Bypass -File $scriptPath"
$opt = New-ScheduledJobOption -RunElevated
$trigger = New-JobTrigger -Weekly -DaysOfWeek Monday, Tuesday, Wednesday, Thursday, Friday -At "6:00AM" 
Register-ScheduledJob -Name $name -FilePath "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -Argument $arguments -Trigger $trigger -ScheduledJobOption $opt -Credential $credential
  1. $credential = $Host.UI.PromptForCredential("Task username and password", $msg, "$env:userdomain\$env:username", $env:userdomain)

    This line prompts the user for credentials (a username and a password) and then stores them in the $credential variable. The prompt title is "Task username and password", and by default, the username field will show the domain and username of the currently logged-in user ($env:userdomain\$env:username).

  2. $scriptPath = "C:\users\$env:USERNAME\Downloads\LMSETL.ps1"

    This sets the path to the PowerShell script that you want to schedule. In this case, it points to a file named LMSETL.ps1, located in the current user's Downloads folder.

  3. $name = "LMSETL"

    This line simply sets a string "Serenic2LMS" to the $name variable. This will be the name of the scheduled job.

  4. Executing a script without loading the user profile can be useful in various scenarios. When using the -NoProfile switch with PowerShell, the profile scripts (profile.ps1, Microsoft.PowerShell_profile.ps1, etc.) are not loaded during the session's start-up. In scheduled tasks, especially those that are system-level or administrative tasks, it's common to use the -NoProfile switch to ensure the task runs consistently and quickly, without being influenced by user-specific settings or customizations.

  5. $opt = New-ScheduledJobOption -RunElevated

    This creates new job options for the scheduled job. The -RunElevated switch indicates that the job should run with elevated permissions (i.e., as an administrator).

  6. $trigger = New-JobTrigger -Weekly -DaysOfWeek Monday, Tuesday, Wednesday, Thursday, Friday -At "6:00AM"

    This line creates a new job trigger. It specifies that the job should run weekly on weekdays (Monday to Friday) at 6:00 AM.

  7. Register-ScheduledJob -Name $name -FilePath "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -Argument $arguments -Trigger $trigger -ScheduledJobOption $opt -Credential $credential

    This is the final command that registers the scheduled job with all the previously set properties:

    • -Name $name: Name of the scheduled job.

    • The arguments you'll pass to the PowerShell executable are constructed in $arguments. This ensures the script is executed without loading the user profile, bypasses the execution policy (this might be necessary if the script isn't signed or if the local policy restricts script execution), and specifies the script to be executed.

    • -Trigger $trigger: When the job should run.

    • -ScheduledJobOption $opt: The options for the job (e.g., run with elevated permissions).

    • -Credential $credential: The user credentials under which the job should run.

Once executed, the script will create a scheduled task named "LMSETL" that runs the specified PowerShell script every weekday at 6:00 AM using the provided credentials.

If it felt too much to you, you're right as it was a lot to unpack here. Here is an alternative approach that you might find easier:

The ScheduledTasks module in PowerShell provides a more comprehensive set of cmdlets to manage scheduled tasks in Windows. This is especially useful if you need to take advantage of more advanced features of Windows Task Scheduler that aren't easily accessible via the PSScheduledJob module.

Here's how you could rewrite the script using the ScheduledTasks module:

  1. Ensure the module is installed and loaded:

     Import-Module ScheduledTasks
    
  2. Create a new scheduled task action to run the PowerShell script:

     $scriptPath = "C:\users\$env:USERNAME\Downloads\LMSETL.ps1"
     $action = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument "-NoProfile -ExecutionPolicy Bypass -File `$scriptPath"
    
  3. Create a trigger to define when the task should run:

     $trigger = New-ScheduledTaskTrigger -Weekly -DaysOfWeek Monday, Tuesday, Wednesday, Thursday, Friday -At 6AM
    
  4. Set up the task to run with elevated permissions:

     $principal = New-ScheduledTaskPrincipal -UserId "$env:userdomain\$env:username" -LogonType Interactive -RunLevel Highest
    
  5. Register the task:

     $taskName = "LMSETL"
     Register-ScheduledTask -TaskName $taskName -Action $action -Trigger $trigger -Principal $principal -Credential $credential
    

Note:

  • The $credential in the Register-ScheduledTask command is assumed to be gathered the same way as in your original script.

  • The ScheduledTasks module is more native to Windows Task Scheduler and provides more granular control over task settings, making it a good choice if you need that flexibility.

  • The -LogonType Interactive ensures that the user is logged on interactively, which might be necessary for certain tasks.

Using the ScheduledTasks module gives you a closer alignment with the underlying Windows Task Scheduler, so you can use this approach if you want a more detailed setup or if you're more comfortable with the Windows Task Scheduler's paradigms.

Did you find this article valuable?

Support Application Support by becoming a sponsor. Any amount is appreciated!