I've got a PowerShell script that pulls the expiry of applications and sends an email. Everything runs smoothly when I execute it manually in PowerShell. I've set up a gMSA account for the task scheduler and it appears to be set up properly, as I see no errors in the Task Scheduler log. However, I'm not receiving the expected CSV file or email output. Here's the code for the scheduled task setup:
```powershell
$Action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-File C:ScriptsAppRegWithExpCertSecrets.ps1"
$Trigger = New-ScheduledTaskTrigger -Weekly -DaysOfWeek Monday -At 9am
Register-ScheduledTask -TaskName "AppExpiringCertsAndSecrets1" -Action $Action -Trigger $Trigger -Principal (New-ScheduledTaskPrincipal -UserId "xxxxgMSA_p_svrinfra$" -LogonType Password -RunLevel Highest) -Description "AppRegistrations_Expiring_CertsAndSecrets weekly at 9 AM"
Start-ScheduledTask -TaskName "AppExpiringCertsAndSecrets1"
```
3 Answers
It looks like your scheduled task might be running under a different user than the one that manually runs the script. This can cause issues related to permissions—specifically, filesystem rights or required environment variables might not be set for that user. You could try testing it by running the task under your original user account to see if it executes without any issues.
Instead of relying on the scheduled task to automate everything, why not just create the task manually? This could help you troubleshoot if there's a specific issue with how the task is being registered through PowerShell. If it works manually, then you can refine your script accordingly.
If both the scheduled task and the script itself work fine independently, maybe try to run the `AppExpiringCertsAndSecrets.ps1` with the same user that the task uses to see if there's any output. Sometimes, specific user permissions prevent the script from accessing certain resources.
Related Questions
Set Wordpress Featured Image Using Javascript
How To Fix PHP Random Being The Same
Why no WebP Support with Wordpress
Replace Wordpress Cron With Linux Cron
Customize Yoast Canonical URL Programmatically
[Centos] Delete All Files And Folders That Contain a String