我正在使用powershell脚本稀疏我的备份文件,我知道我有正确的文件名,但由于某些原因,当我使用remove-item时,该项目不会被删除,也不会抛出任何异常。这就是它的样子:
try{
$Drive = "E:\temp\"
$deleteTime = -42
$limit = (Get-Date).AddDays($deleteTime) #files older than 6 weeks
#get files in folder older than deleteTime and with signature of *junk.vhd* (to be changed later)
$temp1 = Get-ChildItem -Path $Drive -filter "*junk.vhd*" | Where-Object {$_.LastWriteTime -lt $limit} | Select -Expand Name #this has 5 files in list
#will delete every other file
for($i=$temp1.GetLowerBound(0);$i -le $temp1.GetUpperBound(0);$i+=2) {
$name = $temp1[$i]
Write-Host "removing $name" #prints correct file names to screen
Get-ChildItem -Path $Drive -include $name | Remove-Item -recurse -force #this is handling correct files but they aren't deleted for some reason
}
}#try
Catch [Exception] {
#nothing is caught
Write-Host "here"
}
有没有人有任何想法,为什么它找到并写入托管正确的文件名删除,但删除项目不删除它们?
我在看removal a little different示例,但一切看起来都不错。
答案 0 :(得分:0)
尝试替换此行:
Get-ChildItem -Path $Drive -include $name | Remove-Item -recurse -force #this is handling correct files but they aren't deleted for some reason
用这个:
Remove-Item $temp1[i].FullName -force -recurse
由于您已经获得了该文件的完整内容,我觉得不必再次调用Get-ChilItem
并将其传递给管道,而只需使用FUllName属性提供Remove-Item,即该文件的完整路径。