我需要我的脚本包含“LastWriteTime"-Whatif 输出上的属性 [英] I need my script to include the "LastWriteTime" property on the -Whatif output

查看:11
本文介绍了我需要我的脚本包含“LastWriteTime"-Whatif 输出上的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编辑我在此处找到的脚本,以便我可以首先查看它将删除的文件的报告,包括文件名和路径以及LastWriteTime"属性,以便我可以在执行之前和将其配置为计划任务之前分析脚本的输出几个月:

I need to edit a script that I found here so that I can first see a report of the files it will delete including File name and path along with the "LastWriteTime" property so that I can analyze the output of the script for a couple of months before executing and before I configure it as a scheduled task:

我已经尝试使用 LastWriteTime 对象属性一段时间了,但我不知道还能做什么..

I have tried playing around with the LastWriteTime object property for a while but I dont know what else to do..

代码如下:

$limit = (Get-Date).AddDays(-30)

$del30 = "D:contoso_ftpusers"

$ignore = Get-Content "C:UsersusernameDocumentsScriptsignorelist.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 输出到目前为止的样子:

This is what the -Whatif output looks like so far:

假设:执行删除文件"操作在目标D:contoso_ftpusersftp-contosoaccountcontoso Downloadscontosofile.zip"上.

What if: Performing the operation "Remove File" on target "D:contoso_ftpusersftp-contosoaccountcontoso Downloadscontosofile.zip".

所以.. 如您所见,我需要能够获得LastWriteTime"财产在那里.

So.. as you can see I need to be able to get the "LastWriteTime" property in there.

非常感谢任何帮助或指向文章或文档的指针.

Any help or pointers into articles or documentation is greatly appreciated.

提前致谢,

//伦纳特

推荐答案

如您所见,-WhatIf 的输出相当简洁 - Action: Thing"- 仅此而已.

As you've found, the output from -WhatIf is rather terse - "Action: Thing" - nothing more.

您可以在脚本中通过分离报告和实际删除来解决此问题:

You can solve this in a script by separating reporting and actual removal:

param(
  [string]$Path = "D:anqsoft_ftpusers",

  [datetime]$Limit = $((Get-Date).AddDays(-30)),

  [string[]]$ignore = $(Get-Content "$env:USERPROFILEDocumentsScriptsignorelist.txt"),

  [switch]$Force
)

$targetPaths = Get-ChildItem $del30 -Recurse |
  Where-Object {$_.LastWriteTime -lt $limit } |
  Select-Object -ExpandProperty FullName |
  Select-String -SimpleMatch -Pattern $ignore -NotMatch |
  Select-Object -ExpandProperty Line

# List files that would have been affected + their timestamps
Get-ChildItem $targetPaths -Recurse -File |Select-Object FullName,LastWriteTime

if($Force){
  # If -Force was specified then we remove as well
  $targetPaths |Remove-Item -Recurse -Force
}

现在您可以将其运行为:

Now you can run it as:

PS ~> .script.ps1

... 只用 LastWriteTime 列出文件,一旦你有信心,用 -Force 运行它:

... to just list the files with LastWriteTime, and once you're confident, run it with -Force:

PS ~> .script.ps1 -Force

实际删除文件

这篇关于我需要我的脚本包含“LastWriteTime"-Whatif 输出上的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆