Powershell Copy-Item caching

瘦欲@ 提交于 2019-12-11 09:27:01

问题


I am having an issue with Powershell not copying the latest files across servers when using the below code.

$dir="\\MyServer\SQLBackups\SQL Backup*.bak"
$FileLocation = "E:\SQLRestore\SQL Backup Latest.bak"

If (Test-Path $FileLocation){
    Remove-Item $FileLocation
}

If (Test-Path $dir){
    $latest = Get-ChildItem -Path $dir | Sort-Object CreationTime -Descending | Select-Object -First 1
    Copy-Item -Path "$latest" -Destination $FileLocation
}

The code should locate the latest .bak file with the prefix "SQL Backup" and transfer this locally.

This process was working for over a month and with no changes to either servers or the process when suddenly the transfer time dropped from 5 mins to 3 seconds and the same file was being transferred.

Many Thanks


回答1:


Add some logging statements to your script to see what really happens. Like so,

$logfile = "c:\myLogFile.txt"
If (Test-Path $dir){
    $latest = Get-ChildItem -Path $dir | Sort-Object CreationTime -Descending | Select-Object -First 1
    add-content $logfile "Latest file: is $($latest.FullName)"
    Copy-Item -Path "$latest" -Destination $FileLocation
}


来源:https://stackoverflow.com/questions/25602973/powershell-copy-item-caching

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!