3

I'm trying to run the script from https://gallery.technet.microsoft.com/Password-Expiry-Email-177c3e27 I've corrected the variables for my environment and I'm able to run it from my workstation and the server just fine when running it manually but when I try to schedule it with Task Scheduler the task seems to run fine but nothing is sent out.

I've check my event logs and do not see errors popping up and the task history says the instance comes back with return code 0.

I've checked these - two questions and other from Googling but adding extra arguments other than -file C:\path\script.ps1 seems to give long numbered return codes or just keeps running.

I'm running the task as administrator, running whether the user is logged on or not, running with highest privileges, and trying to have it run every night at midnight.

Is there anything else I'm missing?

jonathanwash
  • 115
  • 1
  • 8
  • 1
    Note that the script you reference in your link is not certified(not tested) on W2K12. How have you setup the command to run in your scheduled task config ? – krisFR May 14 '15 at 21:20
  • Very simple: Program = PowerShell.exe and add arguments = - file C:\path\script.ps1 It seems to work just fine W2K12 as running straight (Right Click>Run with PS) allows it to work and sends me the messages to the test email. – jonathanwash May 14 '15 at 21:46
  • 1
    What exactly is not working ? The final email is not sent ? Is the csv logfile written ? – krisFR May 14 '15 at 21:49
  • Technically both but the logging is another matter. The emails are not being sent to the test email. – jonathanwash May 14 '15 at 22:20

1 Answers1

1

It sounds to me like the user assigned to run this task has not been given the right to Logon as Batch.

This is a user right that is not automatically assigned to any user, even Administrators.

If you are on a member server you can set it in local policy. Fire up SecPol.msc and locate User Rights Assignment, make sure the user running the task is represented in the logon as batch group.

If you are doing this on a DC then Default Domain Policy usually defines this, edit via the usual GPO managmeent tools and again make sure your running user is represented in the logon as batch there.

Patrick
  • 1,280
  • 1
  • 15
  • 36
  • Just checked the User Rights Assignment in GPO and found the policy and our Administrator security group was included but I added the actual admin account directly and tried to run the script again through Task Scheduler but no change. – jonathanwash May 18 '15 at 17:47
  • 1
    Task Scheduler has a history tab for each job. Check that. Additionally, make the first line of your script "Get-Date | out-file .\START.TXT", if you don't get the start.txt generated your script is not executing. Are you sure you have correctly called powershell.exe with the script as an argument? I struggled for ages once before I realised all I had done was call powershell and had forgotten to append the script path. – Patrick May 19 '15 at 07:08
  • I added the Get-Date and ran it outside of the scheduler and it ran and dropped the text file with the date just fine. Had scheduler run the script but did not drop the text file. http://imgur.com/0Lhy0Et I've read a couple places that the argument needs -file before the path and have the path with encased with quotes but none have worked. – jonathanwash May 19 '15 at 18:38
  • 1
    If the date wasn't output by the scheduled task it didn't run. Can you check the history? The -file argument is new to me, I don't use it in my environment. You can test how to execute it from command prompt, just run 'PowerShell c:\path.ps1' and it should work. This is all the task does. Try putting full path to PowerShell.exe? Everything looks fine in your image. Get that history info,it should tell you everything. – Patrick May 20 '15 at 07:03
  • 1
    1. Check your execution policy. Set it to unrestricted and try again. 2. In the task scheduler actions panel add '>output.txt' after the path to your script (so it reads C:\script.ps1 >output.txt), make sure to fill in the working dir too. This will capture any output the script generates (errors etc) This is what a task that works for me looks like : http://i.imgur.com/ddDdgqc.png – Patrick May 20 '15 at 09:12
  • Finally got it working!!! I had my execution policy set to RemoteSigned so I switched it to Unrestricted, I added the >output.txt to my arguments, and added C:\Scripts to the Start in line and everything worked. The output.txt did not have anything in it. History of the failed runs and the successful runs showed exactly the same data. So I slowly removed the changes to see what was happening and it looks like added the Start in path was the culprit. – jonathanwash May 20 '15 at 16:31