hopefully somebody can help me fathom this as it's doing my head in a little! [I'm new, so please be gentle :) ]

I recently set up a PS script to run by Windows Scheduled Task at startup, via batch file to run with the -ExecutionPolicy Bypass switch. The objective is to send a list of Automatic Services not running to our sysadmin distribution list so that we can intervene if necessary.

The task has been deployed by Group Policy Oject to a number of Windows 2008, 2008 R2, and Windows 2012 R2 servers. The task runs as SYSTEM with highest privileges, and our forest/domain is functioning at Server 2012 R2 level. The script runs from a UNC on our SAN, and involves and localhost output to C:\temp (a simple notepad.txt file)

Here's the script: [Admittedly, I'm fairly novice with PowerShell so I cobbled it together from various articles from here and there and hammered it together until I got it working to an acceptable standard!]

#PowerScript task for reporting the name of any service with automatic startup mode that are not are running. 
$Results = @()
$hst = hostname
$msgbdy = "This machine has restarted.
Check the attached file for any services with Automatic startup configuration that are not running. 
[NB: Services configured to startup in Automatic (Delayed Start) are ignored]"
$Results += Get-WmiObject -Class Win32_Service -Filter {State != 'Running' and StartMode = 'Auto'} |
ForEach-Object {Get-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\$($_.Name)" |
Where-Object {$_.Start -eq 2 -and $_.DelayedAutoStart -ne 1}} |
Select-Object -Property @{label='ServiceName';expression={$_.PSChildName}}
$Results | Out-File "c:\temp\$hst.txt"
Start-Sleep -s 2
send-mailmessage -from "[email protected]" -to "[email protected]" -subject "$hst Automatic Services Check" -Attachments "c:\temp\$hst.txt" -body $msgbdy -priority High -smtpServer server.ourdomain.co.uk

Now, the script works if it's run directly in the PowerShell prompt; it works if you run the batch file to trigger it, as you would with a scheduled task; and it works when triggered as a Scheduled Task....though it turns out this is only the case for around 75% of our machines.

For the other 25%, the only part of the script that doesn't work is the send-mailmessage part. The text file is created in C:\temp with the 'hostname' etc. to identify which machine it is from, and the contents are accurate. They just won't finish the job and send the email report (which is sort of the point of the whole exercise :)

What is more frustrating is that these "problem machines" will send the email if I trigger the process by either running the batch file or the PowerShell script directly when logged on to the server desktop with my useraccount, so I know they are capable of it!

Any help or time spent considering would be appreciated!


  • When it fails does the schedule task report an error in the event log at all? Does the task show as completing successfully? Given the script I am guessing you have this set to run after the computer is restarted or something? Is the network fully up at the time the script runs? Does it need some more time to fully get online or something?
    – Zoredache
    Aug 31, 2018 at 16:43
  • When task runs automatically it's using the system account right? GPO on the mail server could be blocking access from some of the computers. Aug 31, 2018 at 18:21
  • There are no errors in the task scheduler events. As far as the task scheduler is concerned it was successful, though the task is only concerned with run the batch file to start the script, so....
    – Jimbobbily
    Aug 31, 2018 at 18:42
  • ...there is a TIMEOUT delay on the batch file to count 600 seconds before running the ps1 script to actually do the job. When I design it I wanted a 10minute delay after startup, but 2012 R2 on give 5 mins or 30mins (windows 10 seems to allow a lot more freedom....should’ve tested on a windows 7/8 machine!). As I mentioned, the servers that don’t send the email WILL send an email if the script is run directly from the PS prompt or if the batch is run by a user, just not from the Task Scheduler as NT AUTHORITY\SYSTEM. I’ll have to check out the policy on the mail server, but I doubt it’s that
    – Jimbobbily
    Aug 31, 2018 at 18:46
  • Research and add start-transcript and stop-transcript to the powershell script and use that for logging the activities of the scripts when they are run by Task Scheduler. You should get something in the transcripts about why the send-mailmessage failed. One likely guess is that the SMTP relay you are using is not accessible or configured to allow relay by the servers having the problem. You could also try/catch the send-mailmessage but that hasn't worked right for me. Aug 31, 2018 at 19:01

1 Answer 1


I seem to have fixed it. It was neither authentication required or secure connection. I narrowed the issue down to the error "The account 'DOMAIN\COMPUTER$' provided valid credentials, but it does not have submit permissions on SMTP Receive connector 'Default Frontend MAILSERVER; failing authentication." in the Exchange 2013 Server logs. Though the server was in the IP Range, for some reason simply ticking the Permission groups/Exchange users checkbox allowed it. I am non-the-wiser as to why this was necessary for only some of the servers, of how it is relevant,but it works and that's enough! Our Exchange environment is mid migration and quite a mess at the moment, so it makes send that the answer lies there.

Thanks @Todd Wilcox for the pointers. Cheers

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .