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

查看:42
本文介绍了我需要我的脚本包含"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_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输出到目前为止的样子:

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

如果出现以下情况该怎么办:在目标"D:\ contoso_ftp \ users \ ftp-contosoaccount \ contoso Downloads \ contosofile.zip"上.

What if: Performing the operation "Remove File" on target "D:\contoso_ftp\users\ftp-contosoaccount\contoso Downloads\contosofile.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.

预先感谢

//Lennart

推荐答案

如您所见, -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:\banqsoft_ftp\users",

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

  [string[]]$ignore = $(Get-Content "$env:USERPROFILE\Documents\Scripts\ignorelist.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天全站免登陆