Reputation: 29657
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
Upvotes: 1
Views: 27815
Reputation: 4866
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
}
Upvotes: 3
Reputation: 4868
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
}
Upvotes: 0