我编写了一个Powershell脚本,它执行以下步骤。
这个_DeletedFiles文件夹的要点是,即使有人删除了一个文件,我们也希望在某个地方保存至少60天。
现在,这个脚本稍微复杂一些,包括编写日志文件、测试路径和第一次运行if语句等等。
除了要将服务器上删除的文件从BackUp复制到新文件夹的步骤外,所有文件似乎都正常工作。
此步骤与以下步骤类似:
$testfolder = Test-Path "$UsbDisk\$backupFolder"
# If a Backup folder already exist then Compare files and see if any changes have been made
if ( $testfolder -eq $true ) { #IF_2
# Copy deleted files to BackUp Folder
MyLog "Check for Deleted Files on E:\" 0
$source = "E:\"
$sourcelist = Get-ChildItem $source -Recurse
$destination = "$UsbDisk\$backupFolder\Data_01\_DeletedFiles"
foreach ($file in $sourcelist){
$result = test-path -path "E:\*" -include $file
if ($result -like "False"){
Copy-Item $file -Destination "$destination"
}
}
# Start Synchronizing E:\
MyLog "Start Synchronizing E:\" 0
Robocopy "E:\" "$UsbDisk\$backupFolder\Data_01" /mir /r:2 /w:3 /M /XD VM_*
MyLog "E:\ is up to Date" 0
# Copy deleted files to BackUp Folder
MyLog "Check for Deleted Files on F:\" 0
$source = "F:\"
$sourcelist = Get-ChildItem $source -Recurse
$destination = "$UsbDisk\$backupFolder\Data_02\_DeletedFiles"
foreach ($file in $sourcelist){
$result = test-path -path "F:\*" -include $file
if ($result -like "False"){
Copy-Item $file -Destination "$destination"
# Then Delete it from the BackUp
}
}
# Start Synchronizing F:\
MyLog "Start Synchronizing F:\" 0
Robocopy "F:\" "$UsbDisk\$backupFolder\Data_02" /mir /r:2 /w:3 /M /XD VM_*
MyLog "F:\ is up to Date" 0
}我得到的错误--即文件不能被复制,因为它们不存在于目的地;但是,它试图复制不应该被复制的文件。
我想知道是否有人想解决这个更优雅的问题,或者如何修复我的代码片段。
发布于 2012-09-30 01:45:17
我认为问题可能在测试路径命令中。我会用include $file代替include $file.Name。include参数需要一个字符串,而不是一个对象。
为了代码的可维护性,我也会用(-not $result代替(-not $result)。这是因为$result将包含一个布尔值($true或$false),而不是字符串。
https://stackoverflow.com/questions/12648272
复制相似问题