This is driving me crazy! My powershell script works from the command line, but not from task scheduler. This is Powershell v1 on Server 2008 R2 Enterprise.
From the command line, I type:
c:\> powershell -File "C:\Program Files\mystuff\foo.ps1"
My one-line powershell script runs fine -- it copies the most recent file named *.bak. Here's the script, in case that's useful:
gci X:\backups\*.bak | Sort-Object LastWriteTime -Descending | Select-Object -first 1 | foreach { xcopy $_ z:\backups\ }
I created a scheduled task, with the same command. The script completes instantly with result code 0 -- but the script didn't actually do anything.
So I tried to see my script's output, and changed my scheduled action to:
cmd /c powershell.exe -File "C:\Program Files\mystuff\foo.ps1" > c:\ps.log
Now I see the output: "0 File(s) copied"
Go back to the command line and try it again -- still works correctly.
The scheduled task is running as the same user as the command line. Can anybody see what is making it behave differently?
Thank you!