解决在 powershell 中将 Destination 参数编写为脚本块的问题

Work around for writing Destination argument as a script block in powershell

我的组织在网络上的不同文件夹中有很多图片文件。 我一直在尝试将它们合并到 "PictureLibrary" 文件夹中,按它们所在的项目文件夹。 由于这些项目文件夹中除了图片文件之外还有其他文件,我不能只移动整个文件夹。 我一直在尝试下面的代码:

    ($Images = gci "Z:\DivisionFolder" -Recurse -file -Include "*.jpg") |
        Foreach-object {
        copy-Item $_.DirectoryName -Destination "Z:\DivisionFolder\PictureLibrary"
        }
$a=0
For ($a; $a -lt $Images.count; $a++){
    move-item $Images[$a].fullname -Destination {join-path -Path "D:\(1) PROJECTS & PORTFOLIOS\PictureLibrary" -ChildPath $Images[$a].Directoryname}
    }

我尝试过各种格式,但出现此错误

Copy-Item : Cannot evaluate parameter 'Destination' because its argument is specified as a script block and there is no input. A script block cannot be evaluated without input.

如果不将目标写成脚本块,我不知道如何写这个

任何帮助将不胜感激

尝试像这样传递目标参数:

-Destination $(. { script_block_body_here })

() 。 { } 是点源运算符,$( ) 是子表达式运算符。

($RougePictures = Get-Childitem -Path "C:\" -recurse -file -Include "*.JPG")|
   Foreach-object {
        copy-Item $_.DirectoryName -Destination "C:\Folders\PictureLibrary"
    }
$MainFolders = get-childitem -Path "D:\(1) PROJECTS & PORTFOLIOS\PictureLibrary"       

$a=0
For ($a; $a -lt $MainFolders.count; $a++) {
    Foreach ($Rouge in $RougePictures){
        if ((compare-object (split-path $Rouge.directoryname -leaf) $MainFolders[$a] -IncludeEqual).sideindicator -eq '=='){
             move-item $Rouge.fullname -destination $MainFolders[$a].fullname 
         }
    }
}

找到了使用脚本块作为目标参数的变通方法。 用过的 if ((compare-object (split-path $Rouge.directoryname -leaf) $MainFolders[$a] -IncludeEqual).sideindicator -eq '==')

在我复制文件夹 copy-Item $_.DirectoryName -Destination "C:\Folders\PictureLibrary" 以找到与图片所在的文件夹相匹配的文件夹之后。如果找到匹配项 '=='move-item 将移动这些文件。 希望这有帮助。