我正在创建一个脚本,在任务中删除超过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
答案 0 :(得分:1)
由于-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.**
注意第二部分。 -Include
仅在您指定-Recurse
或为目录路径指定*时才起作用。
如果您将命令行更改为:
Get-ChildItem –Path $LOCAL_BKP\*.log | Where-Object {$_.LastWriteTime –lt (Get-Date).AddDays($QTD_DIAS)}
它应该按预期工作。如果仍然没有,请确保您的文件存在超过10天。