locked
Another "PS script won't run under Task Scheduler" problem RRS feed

  • Question

  • Good afternoon,

    I'm trying to run some scripts under Task Scheduler on Server 2012 R2 but no joy; the script runs until it times out at one hour. When I run the scripts manually they finish successfully within 10 seconds. Here's one of my scripts that refuses to cooperate:

    Get-Service -ComputerName srv-ServerName -Name SQLWriter | Restart-Service -Verbose

    In Task Scheduler I have powershell.exe in the Program/script field and C:\Downloads\VSS\VSSwriterAppy.ps1 in the Add arguments field. I thought maybe the path to the script was wrong but I did a cut and paste from Task Scheduler and it ran fine. I ran set-executionpolicy unrestricted but no joy and I've tried adding -executionpolicy bypass and -file to the Add arguments field but still no joy. Any ideas, suggestions, or recommendations?

    Thanks,

    Joe B

    Thursday, February 20, 2020 9:31 PM

All replies

  • Oh yeah, I'm running these scripts under the Domain Admin account which is the same account I'm using to manually run the scripts.

    Thanks,

    Joe B

    Thursday, February 20, 2020 9:34 PM
  • This has to be run under an account that has network access and that means you must store credentials in TS.

    You can also add logging to the script to determine if it is actually executing.  I also recommend that you set the default folder to a known writable directory.

    You can also check the PowerShell log in the Event Viewer and the history of the task to see the task execution events.


    \_(ツ)_/

    Thursday, February 20, 2020 9:36 PM
  • This has to be run under an account that has network access and that means you must store credentials in TS.

    You can also add logging to the script to determine if it is actually executing.  I also recommend that you set the default folder to a known writable directory.

    You can also check the PowerShell log in the Event Viewer and the history of the task to see the task execution events.


    \_(ツ)_/

    Hmmm...

    1) I thought that configuring the task to run using the Domain Admin account would mean that the credentials would be handled at a higher level and would be passed down to the executing task. I tried putting a couple of these scripts on the actual servers and they ran fine so there must be a network aspect to my problem.

    2) I looked for a checkbox to activate logging but I couldn't find where to do this. I will Google that and see if I can determine where the scheduled script is giving up the ghost.

    3) The history wasn't very helpful as all it said was that the task was ended after its timeout period.

    Thanks for the suggestions. I'm still relatively new to PowerShell and will take whatever knowledge I can glean from people.

    Thanks again,

    Joe B

    Thursday, February 20, 2020 10:49 PM
  • To clarify - the issue is not a scripting issue it is a task scheduler.  You have to use full credentials with TS or, even a domain account will not access the network.

    The Event log has a PowerShell log and it is enabled by default.

    get-winevent -LogName Microsoft-Windows-PowerShell/Operational -Max 20


    \_(ツ)_/

    Thursday, February 20, 2020 11:00 PM
  • Generate a transcript to see what your script is doing.

    Start-Transcript c:\temp\script.log 
    Get-Service -ComputerName srv-ServerName -Name SQLWriter | Restart-Service -Verbose
    Stop-Transcript

    **********************
    Windows PowerShell transcript start
    Start time: 20200220204347
    Username: SLICK\Dave
    RunAs User: SLICK\Dave
    Configuration Name:
    Machine: SLICK (Microsoft Windows NT 10.0.18363.0)
    Host Application: C:\Windows\System32\WindowsPowerShell\v1.0\powershell_ise.exe
    Process ID: 9728
    PSVersion: 5.1.18362.628
    PSEdition: Desktop
    PSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.18362.628
    BuildVersion: 10.0.18362.628
    CLRVersion: 4.0.30319.42000
    WSManStackVersion: 3.0
    PSRemotingProtocolVersion: 2.3
    SerializationVersion: 1.1.0.1
    **********************
    Transcript started, output file is c:\temp\script.log
    Get-Service : Cannot find any service with service name 'SQLWriter'.
    At line:2 char:1
    + Get-Service -ComputerName srv-ServerName -Name SQLWriter | Restart-Se ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (SQLWriter:String) [Get-Service], ServiceCommandException
        + FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.GetServiceCommand
    Get-Service : Cannot find any service with service name 'SQLWriter'.
    At line:2 char:1
    + Get-Service -ComputerName srv-ServerName -Name SQLWriter | Restart-Se ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (SQLWriter:String) [Get-Service], ServiceCommandException
        + FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.GetServiceCommand
    **********************
    Windows PowerShell transcript end
    End time: 20200220204352
    **********************

    Friday, February 21, 2020 1:48 AM
  • Well THAT'S a little frustrating; now it's working fine -- all I see is the service restarting. I guess I shouldn't be too frustrated though; I do like the "Transcript" PS scripts.

    Thanks everyone,

    Joe B

    Friday, February 21, 2020 5:52 PM