0

I am trying to execute a PowerShell script with parameters as a scheduled task. On the Start a program screen I have

Program/script

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

and in Add arguments

-Command "& C:\Test\MoveFiles.ps1 -destinationRoot \\OB-VM-ME-Data\ME-Data\Archived\Test"

What am I doing incorrectly?

EDIT: Attached is the script in question

Param (
    [Parameter(Mandatory=$true)][string]$destinationRoot
)

$path = (Get-Item -Path ".\").FullName

Get-ChildItem $path\* -Include *.bmp, *.svg | Where-Object {
    $_.LastWriteTime -lt (Get-Date).AddDays(-30)
} | ForEach-Object {
    $content = $path + "\" + $_.Name

    $year = (Get-Item $content).LastWriteTime.Year.ToString()
    $monthNumber = (Get-Item $content).LastWriteTime.Month
    $month = (Get-Culture).DateTimeFormat.GetMonthName($monthNumber)

    $destination = $destinationRoot + "\" + $year + "\" + $month 

    New-Item -ItemType Directory -Force -Path $destination

    Move-Item -Path $content -Destination $destination -Force
}
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Philip Loyer
  • 738
  • 2
  • 7
  • 22

1 Answers1

1

Don't use -Command if you want to execute a PowerShell script (with or without parameters). Use -File instead. Change the argument list of the scheduled task into something like this:

-File "C:\Test\MoveFiles.ps1" -destinationRoot "\\OB-VM-ME-Data\ME-Data\Archived\Test"

Edit:

I don't see anything inherently wrong with your script. The only thing sticking out that might prove problematic is that it tries to read files from the current working directory (Get-Item -Path ".\"), which may or may not be what you think it is. You can configure the working directory in the scheduled task settings, though, to remove this variable from the equation.

Since scheduled tasks are notoriously difficult to debug, and it's not even clear what the actual problem is or what causes it, your best bet is probably to follow the debugging steps I outlined in an answer to a similar question.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • Doesnt seem to work, i tried it from the command prompt to test it, it looks like it processes but the action of the script isnt working. However if i run it from within powershell it works as is supposed to. – Philip Loyer Nov 26 '18 at 19:45
  • @PhilipLoyer Then you need to show the content of the script for further debugging (particularly the parameter definition). Also, is the scheduled task configured to run as a particular user whether or not that user is logged in? You most likely need that since your script seems to be accessing a remote share. With that said, scheduled tasks are well-known for being a pain in the rear to [debug](https://stackoverflow.com/a/41635982/1630171). "Isn't working" is not an adequate problem description. – Ansgar Wiechers Nov 26 '18 at 20:59
  • I have added the contents of the script in question – Philip Loyer Nov 26 '18 at 21:05
  • @PhilipLoyer I don't see anything inherently wrong with your script. Please follow the debugging steps outlined in the answer linked in my previous comment. Of particular interest are the current working directory of the script when run (is it what you think it is) and whether access to the remote share is actually working. Add logging statements to the script to find out what is actually happening when the scheduled task is running, and where it fails. – Ansgar Wiechers Nov 26 '18 at 21:14
  • The echo idea was brilliant and revealed my issue. The $path variable assumed it was running from just C:\ and not C:\Test so it wasnt finding the files to transfer. Much thanks! – Philip Loyer Nov 26 '18 at 21:30