就像不同的Powershell版本上的行为一样

Sun*_*her 6 powershell powershell-2.0 powershell-4.0 powershell-5.0

此代码在Powershell 5+中运行良好,但在Powershell 4.0和2.0中不起作用:

$DaysToDelete = 2

$targets = "profile/Default",
           "profile/Profile 1",
           "profile/Profile 2",
           "profile/Profile 3",
           "profile/Profile 4"

$special = @("chromium", "64")

$profiles = Get-ChildItem "C:\" -Directory -Force |
    Where-Object Name -In $special |
    Select-Object -ExpandProperty FullName

$chromeDir = "C:\Users\*\AppData\Local\Google\Chrome\User Data\Default"
$chromeSetDir = "C:\Users\*\Local Settings\Application Data\Google\Chrome\User Data\Default"

$Items = @("*Archived History*",
            "*Cache*",
            "*Cookies*",
            "*History*",
            "*Top Sites*",
            "*Visited Links*",
            "*Web Data*")

$profiles | ForEach-Object {
    foreach($target in $targets) {
        $profile = Join-Path $_ $target

        $items | ForEach-Object {
        $item = $_ 
        Get-ChildItem $profile, $chromeDir, $chromeSetDir -Recurse -Force -ErrorAction SilentlyContinue |
            Where-Object { ($_.CreationTime -lt $(Get-Date).AddDays(-$DaysToDelete))  -and $_.Directory -like $item} | ForEach-Object { 
            $path = Join-Path $_.DirectoryName $_
            Remove-Item $path -force -Verbose -recurse -ErrorAction SilentlyContinue }
         }

    }
}
Run Code Online (Sandbox Code Playgroud)

我透露,打破执行的那块是

-and $_.Directory -like $item
Run Code Online (Sandbox Code Playgroud)

它在PS 5+(Windows 10)上工作正常,但在PS 4(Windows 7)上没有发现类似的模式.Chrome版本及其目录层次结构在两台计算机上都相同:59.0.3071.115(官方构建版)(64位).

在Win10上启动脚本,版本规范相似

powershell.exe -Version 4.0
Run Code Online (Sandbox Code Playgroud)

什么都没有,它运行得很好.我对Powershell版本的细节不太熟悉,欢迎大师提出任何建议.如何使脚本版本独立

更新: 这是完整的代码,但它没有任何价值.我验证了所有的地方,并确切地说明了上面的问题行.

另一个有趣的时刻:我发现,问题不是在like条款本身,而在组合like$_.CreationTime检查:

$_.CreationTime -lt $(Get-Date).AddDays(-$DaysToDelete) -and $_.Directory -like $item
Run Code Online (Sandbox Code Playgroud)

如果我自己放置这些条件中的任何一个,一切都工作正常,但如果我将它们组合成单一复合条件,则不返回任何内容,尽管文件夹满足这两个条件.

我无法以任何方式解释这一点.

gms*_*man 4

我在评论中提到了这一点;我不确定它是否足够清楚,以及这是否是整个问题。

根据您原来的问题,这条线不起作用。这不依赖于 PowerShell 版本。

$_.Directory -like $item
Run Code Online (Sandbox Code Playgroud)
  • Get-ChildItem找到文件时,它将返回一个System.IO.FileInfo对象。

    • Directory是类的属性FileInfo,因此文件将具有此属性。
    • Directory属于 类型DirectoryInfo.
  • Get-ChildItem找到文件夹/目录时,它将返回一个System.IO.DirectoryInfo对象。

    • DirectoryInfo对象没有Directory属性。
    • 他们有Parent,它也返回一个System.IO.DirectoryInfo对象
  • 无论哪种情况,您都在处理一个对象并将其与字符串进行比较。当你真的可能想将文件夹名称与字符串进行比较时。
    编辑:适用于运行 Windows 8.1 的情况v5.1.14409.1005;预计旧版操作系统也是如此。
    对于运行 PS 的 Windows 10 为 False v5.1.143993.1480;期望在 Server 2016 上是相同的。不确定Name/FullName属性是如何自动评估的......


Get-ChildItem -File      | Where {$_.Directory      -like "*test*"}   # not ok: comparing object to string.
Get-ChildItem -Directory | Where {$_.Directory.Name -like "*test*"}   # not ok: DirectoryInfo object does not have Directory property

Get-ChildItem -File      | Where {$_.Directory.Name -like "*test*"}   # ok
Get-ChildItem -Directory | Where {$_.Parent.Name    -like "*test*"}   # ok
Run Code Online (Sandbox Code Playgroud)