Forum Discussion
dmarquesgn
Aug 18, 2022Iron Contributor
Running a powershell "MessageTrace" command within a scheduled task
Hi,
I need to run a powershell script 4 times a day, to return the number of emails sent from a specific mailbox. I've got my code running fine and returing an email with the count of sent emails until the actual time.
Now I need to run my powershell code from a scheduled task, so I can run it everyday. The problem is that I'm using this command to connect to Exchange Online:
Connect-ExchangeOnline -UserPrincipalName email address removed for privacy reasons
So, running from a schedule task, I'm not there to authenticate, so the script will not be succedeed. Of course I'm not going to insert my credentials on the powershell script.
So I can I authenticate to Exchange Online when running the powershell from a schedule task?
Thanks
- There are two ways mentioned in that article, one with a PFX password and one using a certificate thumbprint which is from the certificate in the user certificate store from the account that will run the scheduled account (https://adamtheautomator.com/exchange-online-v2/#Authenticating_Using_Certificate_Thumbprint) I use that method too in on of my scripts for 365 Health status too https://powershellisfun.com/2022/05/23/get-365-service-health-status-using-powershell/
7 Replies
Sort By
- You can use the Secrets Management module for that https://devblogs.microsoft.com/powershell/secretmanagement-and-secretstore-are-generally-available/ ?
- dmarquesgnIron ContributorHi,
Thanks for the quick reply. I've read the article and configured my Vault and my secret.
But as I'm quite new to Powershell, now how can I use that secret to authenticate to Exchange?
Thanks- You can store and retrieve credentials in that, there are quite a few articles about that. But perhaps this is also a solution for you? https://adamtheautomator.com/exchange-online-v2/ (App registration and certificate)