【发布时间】:2021-02-11 03:29:52
【问题描述】:
我需要编辑我在此处找到的脚本,以便我可以首先查看它将删除的文件的报告,包括文件名和路径以及“LastWriteTime”属性,以便我可以分析脚本的输出执行前几个月,在我将其配置为计划任务之前:
我曾尝试使用 LastWriteTime 对象属性一段时间,但我不知道还能做什么..
下面是代码:
$limit = (Get-Date).AddDays(-30)
$del30 = "D:\contoso_ftp\users"
$ignore = Get-Content "C:\Users\username\Documents\Scripts\ignorelist.txt"
Get-ChildItem $del30 -Recurse |
Where-Object {$_.LastWriteTime -lt $limit } |
Select-Object -ExpandProperty FullName |
Select-String -SimpleMatch -Pattern $ignore -NotMatch |
Select-Object -ExpandProperty Line |
Remove-Item -Recurse -WhatIf
这是目前 -Whatif 输出的样子:
如果:对目标“D:\contoso_ftp\users\ftp-contosoaccount\contoso Downloads\contosofile.zip”执行“删除文件”操作。
所以.. 如您所见,我需要能够在其中获取“LastWriteTime”属性。
非常感谢任何有关文章或文档的帮助或提示。
提前致谢,
//伦纳特
【问题讨论】:
-
当使用
-WhatIf运行时,您无法修改Remove-Item表面的参数 - 但您可以 将|Remove-Item ...替换为|Get-ChildItem -Recurse |Select FullName,LastWriteTime -
大家好!非常感谢你的帮助!!!当我进行 Mathias 告诉我的更改时,它为我提供了我正在寻找的信息,但我必须重新编辑代码才能实际执行文件的删除,对吗?
-
嗯我想知道其他人的回复发生了什么?我删除了“Get-ChildItem”部分并添加了他的建议,但我只得到以下输出:如果:使用 LastWriteTime 在目标“”上执行“删除文件”操作:我怀疑我必须在这些括号内编辑某些内容?谢谢, //Lennart
-
是的,当您准备好删除文件时,您必须将脚本修改回
| Remove-Item -Recurse -
抱歉,我遗漏了 Get-Item
| Get-ChildItem -Recurse |% {Write-host "What if: Performing the operation ""Remove File"" on target ""$($_.FullName)"" with LastWriteTime: $($_.LastWriteTime) "}(对所有的编辑感到抱歉。我现在就下车回去睡觉...)
标签: powershell automation