1
votes

I have a script that uses Get-ChildItem like so:

$files = Get-ChildItem -Path $directories -Recurse -File -Include "package.json"

that works perfectly fine when run from ANY powershell prompt, EXCEPT when run from the task scheduler.

My task scheduler action is:

powershell -version 3.0 -noprofile -nolog -noninteractive -file somescript.ps1 someargs

From the task scheduler I get the error: a parameter cannot be found that matches parameter name 'file' for Get-ChildItem.

What I know:

  • This error is usually caused because the 'File' attribute was not added until Powershell v3.0
  • I am supposed to be able to force Powershell to run in version 3.0 using the -version 3.0 parameter. However, this doesn't appear to be making any difference from the task scheduler.

Any ideas regarding what I need to change?

1
True, I just recently added noexit to test from Windows > Run. I'll remove that from above. - Ayo I
you could try removing -file and adding this at the end: ... | ? {!$_.psiscontainer} - Anthony Stringer
What version of PS is installed? There should be no reason to use the -version parameter for this. Either your version of PS supports the parameter or it doesn't, but in both cases downgrading the version can't help. - Ryan Bemrose
You are right. It looks like the problem was that the path I am passing to the -Path parameter is not available is it is a mapped drive. Powershell didn't complain explicitly about the mapped drive, instead it complained about the seemingly unrelated -File parameter. I was able to discover this by writing a script that only contained Get-ChildItem and some logging and scheduling the simplified file. - Ayo I
TechNet seems to think that -File isn't valid at all for Get-ChildItem.. not to mention, that switch doesn't have anything after it variable-wise except for another switch (-Include "package.json") - gravity

1 Answers

0
votes

Interestingly, the error reported is misleading. In this case the error is because I was referring to a mapped drive that is only available when my user is physically logged in to the box.

So though the reported error is: Error: Parameter cannot be found…'File', the actual issue is that I am attempting to pass a mapped drive that is unavailable, to Get-ChildItem.

I was able to confirm this by writing a very simply script that only contained Get-ChildItem and attempting to run it as a scheduled task both with and without "run even when user is not logged in".

It only failed when that box was checked. Testing with the network path, instead of the mapped drive, fixed the issue.

Hopefully this helps others in the future.