CodexBloom - Programming Q&A Platform

PowerShell 7.3 - implementing Scheduled Task Execution Context and User Profile Loading

👀 Views: 359 💬 Answers: 1 📅 Created: 2025-06-12
powershell scheduled-tasks user-profile PowerShell

I'm having trouble with I'm relatively new to this, so bear with me... I'm experimenting with I'm updating my dependencies and I'm following best practices but I've encountered a strange issue with This might be a silly question, but I'm stuck on something that should probably be simple... I'm trying to create a scheduled task that runs a PowerShell script using the `New-ScheduledTask` cmdlet in PowerShell 7.3. The script is supposed to access user-specific resources, but when the task runs, it fails to load the user's profile, resulting in the behavior: `The term 'Get-UserSpecificData' is not recognized as a cmdlet, function, script file, or operable program.` I have set the task to run under the specific user account and selected 'Run only when the user is logged on'. However, the task still seems to run in a different context. Here's the code I used to create the scheduled task: ```powershell $action = New-ScheduledTaskAction -Execute 'pwsh.exe' -Argument '-File C:\Scripts\UserData.ps1' $trigger = New-ScheduledTaskTrigger -AtLogon $principal = New-ScheduledTaskPrincipal -UserId 'DOMAIN\username' -LogonType Interactive Register-ScheduledTask -Action $action -Trigger $trigger -Principal $principal -TaskName 'UserDataTask' -Description 'Task to get user specific data' ``` I’ve also tried running the script manually in the same context, and it works fine, but it throws the behavior when executed via the task. I have verified the path and permissions, and everything seems to be in order. Any ideas on how to ensure the script runs under the correct user context and can access user-specific resources? I'm working on a web app that needs to handle this. This is part of a larger application I'm building. Thanks in advance! This issue appeared after updating to Powershell 3.11. Has anyone dealt with something similar? My development environment is Ubuntu 22.04. For reference, this is a production microservice. I'd love to hear your thoughts on this. This is my first time working with Powershell 3.9. This is happening in both development and production on Ubuntu 22.04. What's the correct way to implement this?