Powershell script does not run via Scheduled Tasks

This topic contains 10 replies, has 4 voices, and was last updated by Profile photo of netskam netskam 1 year, 10 months ago.

  • Author
    Posts
  • #21792
    Profile photo of netskam
    netskam
    Participant

    Quite new to powershell, been using it for 2 months now and really starting to like it.
    I have a problem now when trying to schedule my scripts.

    Task Scheduler history states:

    Task Scheduler successfully completed task "\PowerShell Remove-LNTerminalSessions" , instance "{8a66be96-dd96-40fc-8d79-8d9ab715031f}" , action "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.EXE" with return code 2147942401

    Task Scheduler successfully completed task "\PowerShell Get-LNPSTBackups" , instance "{37aa4fab-3133-415d-b06c-7631feb9ecef}" , action "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.EXE" with return code 2147942401.

    The scripts are run as admin and the error occurs when I'm not logged in. I,ve just set the tasks to run with highest privileges. But this is just a wild guess. Is there anybody who knows what might be happening here ?

    Also these scripts rely on some modules that are loaded in my profile.ps1 file and they are also dot sourced there.

  • #21793
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    If you've checked the box for "do not store password" in the scheduled task's properties, you can try unchecking that and see if it helps. This causes the task to use a full logon token instead of S4U. This is normally only a problem when the task needs to access remote resources, but it's worth a shot.

  • #21795
    Profile photo of netskam
    netskam
    Participant

    Well I haven't checked it, so it shouldn't be an issue. I was more afraid that maybe the profile.ps1 would not get loaded when logged off.
    I'll report back if the high priority makes a difference

    • #21798
      Profile photo of Will Anderson
      Will Anderson
      Keymaster

      Hey there Netskam. Do you happen to have the -ExecutionPolicy in your arguments? If not, you might want to use -ExecutionPolicy Bypass and see if that works.

  • #21805
    Profile photo of netskam
    netskam
    Participant

    Well it seems setting the task to using high priority didn't work.
    No I'm not using ExecutionPolicy in my task scheduler command. For now the ExecutionPolicy on this system is set on Unrestricted, to rule issues related to it out, or would you think it could still make a difference ?
    Also on some blogs they stated that it might be a good idea to define the full path of the powershell executable in the 'start in' textbox (see attachment .png). Anyone who thinks this could make a difference?
    The error number converted to hex gives me 80070001, not really sure what it means, it shows up a lot on topics related to windows update and installers not finding files...

  • #21807
    Profile photo of Will Anderson
    Will Anderson
    Keymaster

    80070001 is an "invalid function" if I recall. The full path should be loaded into your system path information, and from what you have in your error data, it's seeing the full path.

    Are Remove-LNTerminalSessions and Get-LNPSTBackups functions in a script?

  • #21808
    Profile photo of netskam
    netskam
    Participant

    Both are scripts on a remote file share, being dot sourced in my profile file so I can call them as cmdlets in powershell interactive mode. This works perfectly.

    My profile.ps1 contains :

    #Exchange
    Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
    cd $env:ExchangeInstallPath
    .\bin\RemoteExchange.ps1
    
    #TerminalServices
    Unblock-File C:\Users\admin\Documents\WindowsPowerShell\Modules\PSTerminalServices\PSTerminalServices.psd1 -Confirm:$false
    Unblock-File C:\Users\admin\Documents\WindowsPowerShell\Modules\PSTerminalServices\PSTerminalServices.psm1 -Confirm:$false
    Unblock-File C:\Users\admin\Documents\WindowsPowerShell\Modules\PSTerminalServices\Format\PSFanatic.PSTerminalServices.Format.ps1xml -Confirm:$false
    
    . "\\fileshare\scripts\Get-LNPSTBackups.ps1"
    . "\\fileshare\scripts\Remove-LNTerminalSessions.ps1"
    
    cd /
    

    Get-LNPSTBackups.ps1 contains:

    function Get-LNPSTBackups{
    
    # parameters, variable declarations and code here
    }
    

    Remove-LNTerminalSessions.ps1 contains:

    function Remove-LNTerminalSession{
    
    # parameters, variable declarations and code here
    }
    
  • #21809
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Well, to rule out network access problems, you could try defining those functions in your local profile script instead of dot-sourcing them from a network share. If it works then, you know your profile is being loaded properly.

  • #21842
    Profile photo of Nick Chard
    Nick Chard
    Participant

    I run PowerShell scripts from Scheduled Tasks without too much problem. This is how I have mine set up
    Action: Start a program
    Program/script: PowerShell
    Add arguments: -file D:\Scripts\MyScript.ps1 -parameters
    Start in: left blank

    I use the SYSTEM account to run the scheduled task
    I don't use the -command parameter, the -file parameter will work with all PowerShell versions except v1. This webpage gives a good summary of the powershell.exe parameters
    http://technet.microsoft.com/en-gb/library/hh847736.aspx

    It might also be worth checking the location of your profile script. I put mine in c:\Windows\System32\WindowsPowerShell\v1.0 so it applies to all users & all hosts, see http://blogs.technet.com/b/heyscriptingguy/archive/2012/05/21/understanding-the-six-powershell-profiles.aspx

  • #21874
    Profile photo of netskam
    netskam
    Participant

    Dave and Nick, great suggestions! I'll try them, and report back.

  • #22146
    Profile photo of netskam
    netskam
    Participant

    Finally found the time to further investigate this issue. I removed all references to network shares, moved the PSTerminalServices modules in the C:\Windows\System32\WindowsPowerShell\v1.0\Modules\ folder and my profile.ps1 in C:\Windows\System32\WindowsPowerShell\v1.0 folder. I used the -file parameters instead of -scripts and set the SYSTEM account to run the task and now all is fine. Dave and Nick thanks for the essential info!

You must be logged in to reply to this topic.