Powershell script does not run via Scheduled Tasks

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

提交回复
热议问题