Powershell脚本不会删除旧文件

Powershell脚本不会删除旧文件,powershell,Powershell,我正在创建一个脚本,在这些任务中,删除日志文件的时间超过10天。但是这些文件没有被删除。按照代码片段进行操作 $LOCAL_BKP = 'E:\prod' $QTD_DIAS = "-10" $BACKUP_DATE = Get-Date -UFormat "%Y-%m-%d_%H%M%S" $ARQUIVO_LOG = "$($LOCAL_BKP)\log_backup_$($BACKUP_DATE).log" Get-ChildItem –Path $LOCAL_BKP -Include

我正在创建一个脚本,在这些任务中,删除日志文件的时间超过10天。但是这些文件没有被删除。按照代码片段进行操作

$LOCAL_BKP = 'E:\prod'
$QTD_DIAS = "-10"
$BACKUP_DATE = Get-Date -UFormat "%Y-%m-%d_%H%M%S"
$ARQUIVO_LOG = "$($LOCAL_BKP)\log_backup_$($BACKUP_DATE).log"

Get-ChildItem –Path $LOCAL_BKP -Include *.log | Where-Object {$_.LastWriteTime –lt (Get-Date).AddDays($QTD_DIAS)} | Remove-Item -Recurse | Out-File -Append -NoClobber -filepath $ARQUIVO_LOG

您遇到此问题是因为
-Include
参数对
Get ChildItem
的工作方式。从帮助中:

-Include <String[]>
    Gets only the specified items. The value of this parameter qualifies the Path parameter. Enter a path element or pattern, such as
    "*.txt". Wildcards are permitted.

    **The Include parameter is effective only when the command includes the Recurse parameter or the path leads to the contents of a
    directory, such as C:\Windows\*, where the wildcard character specifies the contents of the C:\Windows directory.**

它应该像预期的那样工作。如果仍然没有,请确保存在超过10天的文件。

首先运行代码,直到管道的这一部分:
Get ChildItem–Path$LOCAL\u BKP–Include*.log | Where Object{$\u.LastWriteTime–lt(Get Date).AddDays($QTD\u DIAS)}
因为我们需要知道是否找到与
Where Object
的过滤器匹配的对象。如果您运行该命令,并且没有任何内容返回到屏幕,则不会删除任何内容。您是正确的,屏幕上不会返回任何内容。如果此文件夹中存在文件,因为我不会被返回?啊,请改为尝试此
Get ChildItem–Path$LOCAL\u BKP\*.log | Where Object{$\u.LastWriteTime–lt(Get Date).AddDays($QTD\u DIAS)}
。我发现
-Include
并不像大多数人想象的那样有效。
Get-ChildItem –Path $LOCAL_BKP\*.log | Where-Object {$_.LastWriteTime –lt (Get-Date).AddDays($QTD_DIAS)}