Windows – Running SSH command within a scheduled task

powershellscheduled-taskwindows

I have simple powershell script which gets the offset of a Ubuntu server with the time of the NTP server.

The command I run is

$result = ssh -i C:\temp\privatekey domain\username@servername "ntpdate -q 192.168.10.1"

When I run this script manually the $result variable is populated as expected. When I run as a scheduled task (as the same user) the script does not set the $result variable at all leaving it blank.

Is there further options I need to enter as part of the SSH command or as part of the scheduled task calling the ps script? Any help very much appreciated.

Best Answer

When you run a command from the command line, your entire environment, including your $PATH variable, is available to the command. When it's run from a scheduler, e.g. cron, that's not the case.

So when you write scripts that will be run from any type of schedulare, you should make sure to always include the full path to all commands in the script, or else to start by giving the script a proper $PATH setting.