1

I'm not sure why I'm getting the following error

Copy-Item : A positional parameter cannot be found that accepts argument 'C:\Code\PS\Auths\2.jpg'. At C:\Code\PS\auth-grab.ps1:9 char:12

C:\Code\PS\Auths\2.jpg is the correct path.

(I'm getting one of these for each of the items in the pipeline)

When I echo $rv I get the correct pathand $_ should be right. Where am I going wrong?

oops script below :

function Generate-FileName($fi)
{           
    $rv = "C:\Code\PS\New\"+ $fi.Name
    #echo rv    
}

Get-ChildItem Auths\*.* -include 1.jpg, 2.jpg | 
ForEach-Object {        
    Copy-Item $_ -destination Generate-FileName(Get-ChildItem $_)       
}

Note if i echo $rv I get the path I want

11
  • 1
    You should post the code from that auth-grab.ps1 file Commented Jun 11, 2012 at 14:22
  • totally forgot to post the code facepalm Commented Jun 11, 2012 at 14:24
  • Isn't it some problem with spaces in file names or in your parameters? Commented Jun 11, 2012 at 14:32
  • There's no spaces in the filenames Commented Jun 11, 2012 at 14:38
  • Have you tried echoing out $_ ...whilst you're about it write-host $_.fullname and $_.name just to be sure you're working with the correct path? Commented Jun 11, 2012 at 14:42

2 Answers 2

3

Wrap the function - Generate-FileName in brackets like this -

ForEach-Object {        
    Copy-Item $_ -destination (Generate-FileName(Get-ChildItem $_))      
}

Enclosing it in parenthesis forces the expression.

OR


Copy the return value of the function in a variable and use the variable in the Copy-Item as below -

function Generate-FileName($fi)
{           
    "C:\Code\PS\New\"+ $fi.Name
}

Get-ChildItem Auths\*.* -include 1.jpg, 2.jpg | 
ForEach-Object {   
    $destination =  Generate-FileName(Get-ChildItem $_)           
    Copy-Item $_ -destination $destination
}
Sign up to request clarification or add additional context in comments.

4 Comments

You might not want to keep $rv at all. Just have this & try [I have not tried this part] - "C:\Code\PS\New\"+ $fi.Name
the only reason I had $rv was to echo it.
yeah, I guess for debugging the value. But, in production script, you might not need it. Thats what I meant.
yeah removing $rv and not returning it works. e.g. function Generate-FileName($fi) { "C:\Code\PS\New\"+ $fi.Name }
0

I think your function Generate-FileName doesn't return anything.

I think your script generate this line with copy-item:

Copy-Item C:\Code\PS\Auths\2.jpg -destination 

Try it like this:

function Generate-FileName($fi)
{           
    $rv = "C:\Code\PS\New\"+ $fi.Name
    return $rv # here is the change
}

6 Comments

I don't think so. It is the problem with function evaluation only. I tried the same sample in Powershell ISE.
@AngshumanAgarwal I think that I found the right solution. You added returning in your last edit and that was what I suggested (firstly in comments) and that was what solved the problem...
You may try what you are saying without modifying anything else except what you have suggested and run full code from John in ISE. Additionally, you may read John's comments too - yeah removing $rv and not returning it works. e.g. function Generate-FileName($fi) {"C:\Code\PS\New\"+ $fi.Name } Its not only returning. Its the way you call the function too. Moreover, $rv is not required. That was just for debugging purpose.
Ok. I get it. But I have a question: What is difference between function Generate-FileName($fi) {"C:\Code\PS\New\"+ $fi.Name } and function Generate-FileName($fi) {$rv = "C:\Code\PS\New\"+ $fi.Name; return $rv } ?
We don’t need to use the return keyword like we do in C style function. Whatever expressions and statements that have output will contribute to the output of our function. So, above we just want the output of "C:\Code\PS\New\"+ $fi.Name. For more in-depth understanding read here - rkeithhill.wordpress.com/2007/09/16/… under the heading called - Function Output Consists of Everything That Isn’t Captured
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.