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
$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
).$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.$name = "LMSETL"
This line simply sets a string "Serenic2LMS" to the
$name
variable. This will be the name of the scheduled job.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.$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).$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.
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:
Ensure the module is installed and loaded:
Import-Module ScheduledTasks
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"
Create a trigger to define when the task should run:
$trigger = New-ScheduledTaskTrigger -Weekly -DaysOfWeek Monday, Tuesday, Wednesday, Thursday, Friday -At 6AM
Set up the task to run with elevated permissions:
$principal = New-ScheduledTaskPrincipal -UserId "$env:userdomain\$env:username" -LogonType Interactive -RunLevel Highest
Register the task:
$taskName = "LMSETL" Register-ScheduledTask -TaskName $taskName -Action $action -Trigger $trigger -Principal $principal -Credential $credential
Note:
The
$credential
in theRegister-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.
Subscribe to my newsletter
Read articles from Hooman Pegahmehr directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Hooman Pegahmehr
Hooman Pegahmehr
Hooman Pegahmehr is a performance-driven, analytical, and strategic Technology Management Professional, employing information technology best practices to manage software and web development lifecycle in alignment with client requirements. He builds high-quality, scalable, and reliable software, systems, and architecture while ensuring secure technology service delivery as well as transcending barriers between technology, creativity, and business, aligning each to capture the highest potential of organization resources and technology investments. He offers 8+ years of transferable experience in creating scalable web applications and platforms using JavaScript software stack, including MongoDB, Express, React, and Node, coupled with a focus on back-end development, data wrangling, API design, security, and testing. He utilizes a visionary perspective and innovative mindset to collect and translate technical requirements into functionalities within the application while writing codes and producing production-ready systems for thousands of users. He designs, develops, and maintains fully functioning platforms using modern web-based technologies, including MERN Stack (MongoDB, Express, React, Node). As a dynamic and process-focused IT professional, Hooman leverages cutting-edge technologies to cultivate differentiated solutions and achieve competitive advantages while supporting new systems development lifecycle. He excels in creating in-house solutions, replacing and modernizing legacy systems, and eliminating outsourcing costs. He exhibits verifiable success in building highly responsive full-stack applications and incident management systems using advanced analytical dashboards while translating complex concepts in a simplified manner. Through dedication towards promoting a culture of collaboration, Hooman empowers and motivates diverse personnel to achieve technology-focused business objectives while administering coaching, training, and development initiatives to elevate personnel performance and achieve team synergy. He earned a winning reputation for transforming, revitalizing, streamlining, and optimizing multiple programs and web-based applications to drive consistent communications across cross-functional organization-wide departments. He manages multiple projects from concept to execution, utilizing prioritization and time management capabilities to complete deliverables on time, under budget, and in alignment with requirements.