我想比较从xml文件中获得的日期与文件的"lastwritetime"。Powershell ISE显示两者相同,但我的if不返回true。我尝试了一些datetime类型转换,但这也没有帮助。
我的XML:
<meta>
<log path="D:tomcat.log">
<lastwrite>08/03/2015 13:44:09</lastwrite>
</log>
</meta>
powershell脚本:
[xml]$log_meta = Get-Content "D:log_meta.xml"
$node = $log_meta.meta.log | where {$_.path -eq "D:tomcat.log"}
(ls "D:tomcat.log").LastWriteTime
$node.lastwrite
if((ls "D:tomcat.log").LastWriteTime -eq $node.lastwrite){
"Date and time is the same"
}
第三行显示:(德国日期格式)
Montag, 3. August 2015 13:44:09
第四行显示:
08/03/2015 13:44:09
$node。lastwrite是一个字符串(你从文件中读取它),而LastWriteTime是DateTime。您需要将它们都转换为日期,或者都转换为DateTime。
如果将两者都转换为DateTime,则需要将文件时间四舍五入到最接近的秒。在NTFS上,文件时间的精度为100纳秒。
最好将文件时间转换为相同格式的字符串。
$writeTimeString = (ls "D:tomcat.log").LastWriteTime.ToString("MM/dd/yy HH:mm:ss")
好了,现在可以工作了。我把它们都转换成字符串。
(ls "D:tomcat.log").LastWriteTime.ToString("dd/MM/yyyy HH:mm:ss")
我希望我可以工作与日期时间,但也许这是不可能的。谢谢你!