Why isn’t my scheduled PowerShell task producing any output?

0
4
Asked By CuriousCoder77 On

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

Answered By PowerPal69 On

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.

Answered By WiseOne42 On

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.

Answered By TechieTommy123 On

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

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.