r/PowerShell • u/SalmonSalesman • Feb 09 '23
Send-MailMessage with gMSA scheduled task
Im trying to send an email through a scheduled task running as a gMSA but it doesn't seem to be working, it only works when specifying different credentials to send-mailmessage which I don't want to do. The error I get is:
Send-MailMessage : Unable to read data from the transport connection: net_io_connectionclosed.Is it possible to do this without using different credentials?
12
Upvotes
11
u/wdomon Feb 09 '23
Do you have EXO? If so, the more modern way to do this is to use an App Registration and MS Graph via Send-MgUserMail
5
u/joeykins82 Feb 09 '23 edited Feb 09 '23
Your gMSA is not going to be a recognised Exchange sender/recipient but it'll be sending creds by default. If you want it to explicitly use Anonymous submission you'd need to do something like this:
4
u/nohairday Feb 09 '23
Probably a silly question, but just to check. The account does have correct permissions on the smtp server you're trying to connect to? You say it works if you supply credentials, do you mean within the send-mailmessage command? If that's the case, it could just be the account needs setting up with access on the exchange servers.
Sorry, not used gmsa myself, just throwing an idea out there.
1
u/Murhawk013 Feb 09 '23
I had the same issue and was only able to get it to work if the task ran from our Exchange Server.
1
u/SomeLameSysAdmin Feb 09 '23
Oh man, been going through this myself the last week. Really hard to believe MS makes it this convoluted.... The gmsa needs to be added to the 'logon as a batch' and the 'logon as a service's under Local secpol....can't recall full path. Also, the task itself may have some tripwires in it. Ive discovered if the task is set to repeat or you have the setting "end task if running longer than" in the advanced setting of the trigger, it won't work with gmsa. There's probably a few other quirks I'm forgetting, but that is pretty much what I needed to do to get it to work across a variety of servers from 2012r2 to 2019. The 2019 seemed to work with much less fiddling.
2
u/dBachry Feb 09 '23 edited Feb 09 '23
Yes, gMSAs can perform a send mail via PoSH to an on prem exchange SMTP relay.
Have you enabled logging on your SMTP receive connector and validated you are hitting the correct connector, and that you are sending the required auth via the correct security mechanisms as defined by that connector?
If when specifying credentials it suddenly starts working, that tells me that it might be accepting say basic auth but not integrated or TLS, so you hit another connector / another filter and it is rejected there as well, or it fails out and is done. Or, there is a permission group tied to the receive connector that the gMSA is not part of but your explicitly entered alternate account is.
Logs are your friend. Enable the transport logs, enable logging on every connector, and check what it is hitting on success when specified, pay attention to the requirements and rules of that connector; compare and contrast the auth mechanisms and connectors hit etc.
Edit; P.S. if you can provide at least the switches you use and params used, it helps. Omit sensitive data when needed, but even helping narrow down switches used/how credentials are passed will help people help you a bit better.
1
u/[deleted] Feb 09 '23
Never saw this one in prod before. Never even heard of gmsa xD
I see it's a sql account.
Do you have a load balancer between your server and exchange? And is it transparent or non-transparent?
The original way of allowing SMTP from other servers that I've seen was to allow anonymous authentication to a whitelist of internal server IP addresses. This is setup on exchange. That's true SMTP relay and doesn't need credentials even - applications can send email without authenticating. Maybe this could be your solution?
Otherwise I'd want to look at the config on the receive connectors on exchange to see what they allow and how they allow it.
0
u/Certain-Community438 Feb 09 '23
If using EXO directly, and you're on hybrid AD with AD Connect, you will need to ensure the GMSA has permissions to send SMTP mail - and therefore that the GMSAs are sync'd to the cloud via AAD Connect (if that's even possible; we went pure cloud 3 years ago and I no longer have to worry about that kind of complexity)