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]"
#GET AUTOMATIC (NOT DELAYED-START) SERVICES
$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}}
#OUTPUT TO TXT
$Results | Out-File "c:\temp\$hst.txt"
#PAUSE FOR 2 SECONDS
Start-Sleep -s 2
#SEND OUTPUT FILE BY EMAIL
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!
Thanks,James
start-transcript
andstop-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 thesend-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 alsotry
/catch
thesend-mailmessage
but that hasn't worked right for me.