需要将此循环

时间:2019-05-21 15:57:15

标签: powershell

我有一段代码可以工作,但是我不太优雅,它查看的目录充满了备份,删除了除最后修改的所有内容。

$path = "C:\Users\test"

Get-ChildItem "$path\*.*" -include *.data* | 
   Sort-Object -Descending -Property LastWriteTime | 
      Select-Object -Skip 1 |Remove-Item -Force

Get-ChildItem "$path\*.*" -include *.enroll* | 
   Sort-Object -Descending -Property LastWriteTime | 
       Select-Object -Skip 1 |Remove-Item -Force

Get-ChildItem "$path\*.*" -include *.govern* | 
   Sort-Object -Descending -Property LastWriteTime | 
       Select-Object -Skip 1 |Remove-Item -Force

我尝试了这个,但是没有用,

$sites = @("data","govern","enroll")
$path = "C:\Users\test"

for ($i=0; $i -lt $sites.Length; $i++) {
    Get-ChildItem "$path\*.*" -include "*.$sites[$i]*" | 
        Sort-Object -Descending -Property LastWriteTime | 
            Select-Object -Skip 1 |Remove-Item -Force
}

我想要的只是一个优雅的循环。

0 个答案:

没有答案