复制项目几乎可以工作,但只是缺少一些明显的东西

Copy-item almost works but just missing something obvious

下面几乎可以工作,但我遗漏了一些明显的东西,在 SOURCE 文件夹 ODS 中,我有一个名为 Archive 的子文件夹。但是,当我 运行 代码时,它似乎没有 on.ly 在目标 ODS 文件夹中创建另一个 ODS 文件夹,但 none 文件夹中存储的 csv 文件是副本,我错误地认为-过滤器将确保只复制 csv。


## The location/filename where the Logs will be stored
$varfullpath = "C:\Users\Simon.Evans\Documents\ReferenceData__logfile.txt"        
## The location/filename of the Source to copy from                                    
$sourceDirectory  = "C:\Users\Simon.Evans\Documents\Source Data\ODS\"
## The location/filename of the Destination to copy to  
$destinationDirectory = "I:\Dev\BI\Projects\Powershell\Test Area\Source Data\ODS\"

## Attempts to copy a file fron Source to Destination and record the event, if the Copy-item fails the script is halted and the error messages are captured in the Log 
## Possibly only 1 error is needed and or applicable, so remove as necessary.
try{
    Copy-item -Force  -Verbose $sourceDirectory -Filter ".csv" -Recurse   -Destination $destinationDirectory  -ErrorAction Stop  
    Write-Log -Message "Copy from $sourceDirectory to $destinationDirectory suceeded"  -path $varfullpath             
}
catch{
    $Error[0] | Write-Log -path $varfullpath                                                                            
    Write-log -Message "Copy from $sourceDirectory to $destinationDirectory Failed"  -Level Error -path $varfullpath     
} 
Start-Process notepad $varfullpath  ## Opens the file immediately for review

如评论中所述,解决方案是将 try 块替换为:

try{
Get-Childitem -Path $sourceDirectory -File -Filter "*.csv" | Copy-Item -Destination $destinationDirectory -Force -Verbose -ErrorAction Stop
}

希望对您有所帮助! BR