PowerShell script won't execute as a Windows scheduled task

后端 未结 5 1357
难免孤独
难免孤独 2020-12-02 14:12

I have a PowerShell script (that works). In Windows Task Scheduler I created a new task to execute \"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe\"

5条回答
  •  北海茫月
    2020-12-02 15:04

    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 an additional line at the beginning of the script to load the missing assembly:

    Add-Type -AssemblyName "System.ServiceProcess"
    

    And the 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

提交回复
热议问题