it is likely to be a permissions issue - as a test, run the task using the same credentials as when it is run at the command line...
Powershell's Invoke-Command not working when called from Windows Task Scheduler
-
27-06-2022 - |
質問
I have a Powershell script, which is working just fine, when called from command line, but only partially fine when executed by the Windows Task Scheduler. The script looks as follows:
# Do things, which are always working.
$session = new-pssession -computername SRV
Invoke-Command -session $session -scriptblock { D:\script.bat }
# Do things, which are always working.
The task defined in the Task Scheduler is completed without errors. As you see, all parts before and after Invoke-Command
are working, also when called by the Task Scheduler. Only the Invoke-Command
itself is only working when called from the command line.
My only guess is, that the Powershell script is exiting prematurely, but I didn't find any way to confirm this or even solve the issue.
解決
他のヒント
I had the same issue with Invoke-Command
when I called a script from Task Scheduler to run certain scriptblocks on multiple computers. I don't know why, but setting the "Start in" folder of the action to the folder of the script solved it.