Powershell script does not run via Scheduled Tasks

后端 未结 18 755
南方客
南方客 2020-12-02 23:12

I have a small script on my Domain Controller that is setup to email me via SMTP about the latest Security Event 4740.

The script, when executed manually, will run a

相关标签:
18条回答
  • 2020-12-02 23:27

    One more idea that worked. It's really silly, but, apparently, the default target OS setting (bottom right corner of the screen) is Vista / Windows Server 2008. As we're past the 10 year mark, it is likely that your Powershell script will not be compatible to these.

    Changing the target to Windows Server 2016, as shown on the screenshot below, did the trick for me.

    0 讨论(0)
  • 2020-12-02 23:27

    In my case (the same problem) helped to add -NoProfile in task action command arguments and check checkbox "Run with highest privileges", because on my server UAC is on (active).

    More info about it enter link description here

    0 讨论(0)
  • 2020-12-02 23:30

    Found successful workaround that is applicable for my scenario:

    Don't log off, just lock the session!

    Since this script is running on a Domain Controller, I am logging in to the server via the Remote Desktop console and then log off of the server to terminate my session. When setting up the Task in the Task Scheduler, I was using user accounts and local services that did not have access to run in an offline mode, or logon strictly to run a script.

    Thanks to some troubleshooting assistance from Cole, I got to thinking about the RunAs function and decided to try and work around the non-functioning logons.

    Starting in the Task Scheduler, I deleted my manually created Tasks. Using the new function in Server 2008 R2, I navigated to a 4740 Security Event in the Event Viewer, and used the right-click > Attach Task to this Event... and followed the prompts, pointing to my script on the Action page. After the Task was created, I locked my session and terminated my Remote Desktop Console connection. WIth the profile 'Locked' and not logged off, everything works like it should.

    0 讨论(0)
  • 2020-12-02 23:30

    I think the answer to this is relevant too:

    Why is my Scheduled Task updating its 'Last Run Time' correctly, and giving a 'Last Run Result' of '(0x0)', but still not actually working?

    Summary: Windows 2012 Scheduled Tasks do not see the correct environment variables, including PATH, for the account which the task is set to run as. But you can test for this, and if it is happening, and once you understand what is happening, you can work around it.

    0 讨论(0)
  • 2020-12-02 23:31

    My fix to this problem was to ensure I used the full path for all files names in the ps1 file.

    0 讨论(0)
  • 2020-12-02 23:31

    after trying a lot of time...

    task scheduler : powershell.exe -noexit & .\your_script.ps1

    be sure to put your script in this folder : windows\system32

    good luck !

    0 讨论(0)
提交回复
热议问题