我正在使用 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"
}
有谁知道为什么它会找到并写入主机要删除的正确文件名,但 Remove-Item 没有删除它们?
我正在研究删除一个有点不同的例子,但一切看起来都很好。