本文介绍了简单的PowerShell LastWriteTime比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个PowerShell脚本,该脚本可以访问文件的属性并发现 LastWriteTime 属性,并将其与当前日期进行比较并返回日期差.

I need a PowerShell script that can access a file's properties and discover the LastWriteTime property and compare it with the current date and return the date difference.

我有这样的东西...

I have something like this...

$writedate = Get-ItemProperty -Path $source -Name LastWriteTime

...但是我不能将 LastWriteTime 转换为"DateTime"数据类型.它说:无法将"@ {LastWriteTime = ... date ...}"转换为"System.DateTime".

...but I can not cast the LastWriteTime to a "DateTime" datatype. It says, "Cannot convert "@{LastWriteTime=...date...}" to "System.DateTime".

推荐答案

尝试以下方法.

$d = [datetime](Get-ItemProperty -Path $source -Name LastWriteTime).lastwritetime

这是item属性怪异的一部分.当您运行Get-ItemProperty时,它不会返回值,而是返回属性.您必须再使用一个间接级别来获得该值.

This is part of the item property weirdness. When you run Get-ItemProperty it does not return the value but instead the property. You have to use one more level of indirection to get to the value.

这篇关于简单的PowerShell LastWriteTime比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 07:16