The other day I came across a curious function. I had a requirement in a script to send an email. Now, when I wrote the script and tested everything was working fine. However when I ran the script from the test schedule, the script executed fine but no email was sent!
After a few hours of searching the system of the exchange host.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
Log Name: Application Source: MSExchangeTransport Date: 9/5/2016 4:57:57 PM Event ID: 1025 Task Category: SmtpReceive Level: Error Keywords: Classic User: N/A Computer: Server.domain.local Description: SMTP rejected a (P1) mail from '[email protected]' with 'Client SERVER' connector and the user authenticated as 'doamin\importexport'. The Active Directory lookup for the sender address returned validation errors. Microsoft.Exchange.Data.ProviderError Event Xml: <Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event"> <System> <Provider Name="MSExchangeTransport" /> <EventID Qualifiers="49156">1025</EventID> <Level>2</Level> <Task>1</Task> <Keywords>0x80000000000000</Keywords> <TimeCreated SystemTime="2016-09-05T16:57:57.000000000Z" /> <EventRecordID>410601</EventRecordID> <Channel>Application</Channel> <Computer>SERVER.domain.local</Computer> <Security /> </System> <EventData> <Data>importexport@domain.local</Data> <Data>Client SERVER-01</Data> <Data>domain.local\importexport</Data> <Data>P1</Data> <Data>Microsoft.Exchange.Data.ProviderError</Data> </EventData> </Event> |
Hold the phone! under my account it worked perfectly but under the service account running the task scheduler it failed.
after a few failed attempts I noticed that if I mistyped the username and password while passing the credentials to the using the -Credentials $Creds argument.. it actually work… WFT
so I quickly worked out that if I passed “dummy” credentials the script worked.
To c create a dummy credential I first had to create a secure string. Then I could pass this script type into a new credential object… as so:
1 2 3 |
$sPassword = New-Object System.Security.SecureString $creds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "NT AUTHORITY\ANONYMOUS LOGON", $sPassword |
in just included this at the top of the script when declaring variables.
1 |
Send-MailMessage -To "User01 <[email protected]>" -From "ITGroup <[email protected]>" -Cc "User02 <[email protected]>" -bcc "ITMgr <[email protected]>" -Subject "Don't forget today's meeting!" -Credential $creds |
WORKS!