1

我不确定为什么会出现以下错误

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 是正确的路径。

(我正在为管道中的每个项目获取其中一个)

当我回显 $rv 时,我得到了正确的路径并且 $_ 应该是正确的。我哪里错了?

哎呀脚本如下:

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 $_)       
}

注意如果我回显 $rv 我得到了我想要的路径

4

2 回答 2

3

包装函数 - Generate-FileName像这样用括号括起来 -

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

将其括在括号中会强制表达式.

或者


将函数的返回值复制到一个变量中,并在 Copy-Item 中使用该变量,如下所示 -

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
}
于 2012-06-11T14:56:33.523 回答
0

我认为您的函数Generate-FileName不会返回任何内容。

我认为您的脚本使用复制项生成这一行:

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

试试这样:

function Generate-FileName($fi)
{           
    $rv = "C:\Code\PS\New\"+ $fi.Name
    return $rv # here is the change
}
于 2012-06-11T15:31:22.213 回答