Powershell script does not run via Scheduled Tasks

后端 未结 18 756
南方客
南方客 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:32

    To achieve "Run as Administrator" functionality I implemented the flag:

    -ExecutionPolicy Bypass
    

    Which only seems to take effect when executing Powershell files. So I dropped my command into .ps1 file, run it with -ExecutionPolicy Bypass, and now my scheduled task is behaving as expected.

    Program: Powershell.exe
    Add Arguments: -ExecutionPolicy Bypass -File C:\pscommandFile.ps1
    
    0 讨论(0)
  • 2020-12-02 23:41

    I was having almost the same problem as this but slightly different on Server 2012 R2. I have a powershell script in Task Scheduler that copies 3 files from one location to another. If I run the script manually from powershell, it works like a charm. But when run from Task Scheduler, it only copies the first 2 small files, then hang on the 3rd (large file). And I was also getting a result of "The operator or administrator has refused the request". And I have done almost everything in this forum.

    Here is the scenario and how I fixed it for me. May not work for others, but just in case it will:

    Scenario: 1. Powershell script in Task Scheduler 2. Ran using a domain account which is a local admin on the server 3. Selected 'Run whether user is logged on or not" 4. Run with highest priviledges

    Fix: 1. I had to login to the server using the domain account so that it created a local profile in C:\Users. 2. Checked and made user that the user has access to all the drives I referred to on my script

    I believe #1 is the main fix for me. I hope this works for others out there.

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

    NOTE: Please ensure that you select Create a Basic task Action and NOT the Create Task Action.

    I found the following solution:

    1) Make powershell.exe run as administrator for this

    1. right-click on the powershell.exe icon
    2. click on properties under the shortcut key menu
    3. click on the advance button; check that "run as administrator" is checked.

    2) in the task scheduler window under the action pane add the following script as a new command

    %SystemRoot%\syswow64\WindowsPowerShell\v1.0\powershell.exe -NoLogo -NonInteractive -ExecutionPolicy Bypass -noexit -File "C:\ps1\BackUp.ps1"
    

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

    I have another solution for this problem that might apply to some of you.

    After I created my power shell (xyz.ps1) script, I opened it in notepad for subsequent editing. Hence Windows made an association between my xyz.ps1 file with notepad.exe and Scheduler was trying to run my power shell script (xyz.ps1) with notepad.exe in the background instead of executing it in Powershell. I found this problem by paying close attention to "Display all running tasks" section in the scheduler, which showed that notepad.exe was being used to run the xyz.ps1 script. To verify this, I right clicked on my xyz.ps1 file in windows explorer, went to "Properties", and it showed Notepad against the "Opens With" section. Then I changed the "Opens With" to %SystemRoot%\SysWOW64\WindowsPowerShell\v1.0\powershell.exe. This did the trick. Now the scheduler would execute my xyz.ps1 using powershell.exe and gave me the desired results.

    To locate your powershell.exe, refer to this article: https://www.powershelladmin.com/wiki/PowerShell_Executables_File_System_Locations

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

    In addition to advices from above I was getting error and found solution on following link http://blog.vanmeeuwen-online.nl/2012/12/error-value-2147942523-on-scheduled.html.

    Also this can help:

    In task scheduler, click on the scheduled job properties, then settings.

    In the last listed option: "if the task is already running, the following rule applies:" Select "stop the existing instance" from the drop down list.

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

    If you don't have any error messages and don't know what the problem is - why PowerShell scripts don't want to start from a Scheduled Task do the following steps to get the answer:

    1. Run CMD as a user who has been set for Scheduled Task to execute the PowerShell script
    2. Browse to the folder where the PowerShell script is located
    3. Execute the PowerShell script (remove all statements that block the error notifications if any exists inside of the script like $ErrorActionPreference= 'silentlycontinue')

    You should be able to see all error notifications.

    In case of one of my script it was:

    "Unable to find type [System.ServiceProcess.ServiceController]. Make sure that the assembly that contains this type is loaded."

    And in this case I have to add additional line at the begining of the script to load the missing assembly:

    Add-Type -AssemblyName "System.ServiceProcess"

    And next errors:

    Exception calling "GetServices" with "1" argument(s): "Cannot open Service Control Manager on computer ''. This operation might require other privileges."

    select : The property cannot be processed because the property "Database Name" already exists

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