We I run the command from the task scheduler, It seems to end normally but when I run the command get-job I can see that the state is in status failed.
I don’t understand why? Any help would be appreciated.
If I do the same with a command like get-process, it works fine even in the scheduled task so I think it is a problem related to exchange and the command get-mailbox.
You’re letting the scheduled job default to run as the SYSTEM account. That doesn’t have permissions off the local machine, and can’t connect to Exchange.
I didn’t see the domain account on the Register-ScheduledJob command, sorry. That’s something you might double-check.
There could be several things wrong. Permissions come to mind first. It’s also possible something in the command is expecting a full user profile to be loaded, which doesn’t happen when Task Scheduler runs the command. Are you running the scheduled task with the same domain account that you use to test? Is the scheduled task running on the same machine you test on? Can you create a scheduled task that simply runs { Get-Command Get-Mailbox } and see if that succeeds (if it doesn’t, then Task Scheduler is having problems finding the Get-Mailbox command, which is your problem). It’s a little hard to really troubleshoot when I’m not in your environment with you, but in general I try to think of all the differences between what I’m running, and what Task Scheduler does, and then try and test each of those things individually.