I encountered an issue in a PowerShell script where the PSScheduledJob module is not being loaded correctly, resulting in errors when trying to use cmdlets from this module. Specifically, the New-JobTrigger and Get-ScheduledJob cmdlets are affected.
the lines of code causing the problem and the modifications needed to fix it are as follows:
Line 47:
Trigger = New-JobTrigger -Daily -At $Time
To fix this line, you should add Import-Module PSScheduledJob before using the New-JobTrigger cmdlet, like this:
To reproduce the issue, you can follow these steps:
1: Run the PowerShell script (Install-PackageManager.ps1) that attempts to create a daily task to automatically upgrade Chocolatey packages at 13:00.
2: Observe the error messages related to the PSScheduledJob module not being loaded.
Expected behavior?
When running the PowerShell script, I expected the PSScheduledJob module to be loaded successfully, allowing the script to create a scheduled task and manage scheduled jobs without encountering any errors.
Describe the Bug
I encountered an issue in a PowerShell script where the PSScheduledJob module is not being loaded correctly, resulting in errors when trying to use cmdlets from this module. Specifically, the New-JobTrigger and Get-ScheduledJob cmdlets are affected.
the lines of code causing the problem and the modifications needed to fix it are as follows:
To fix this line, you should add
Import-Module PSScheduledJob
before using theNew-JobTrigger
cmdlet, like this:To fix this line, you should also add
Import-Module PSScheduledJob
before using theGet-ScheduledJob
cmdlet, like this:How can this issue be reproduced?
To reproduce the issue, you can follow these steps:
1: Run the PowerShell script (Install-PackageManager.ps1) that attempts to create a daily task to automatically upgrade Chocolatey packages at 13:00. 2: Observe the error messages related to the PSScheduledJob module not being loaded.
Expected behavior?
When running the PowerShell script, I expected the PSScheduledJob module to be loaded successfully, allowing the script to create a scheduled task and manage scheduled jobs without encountering any errors.
Operating System
Windows 11
Operating System Version
10.0.22621 N/A Build 22621
Screenshots
No response
Logs
No response
Additional Info
No response