我想将从xml文件获得的日期与文件的“lastwritetime”进行比较。 Powershell ISE对两者都显示相同的内容,但是我的if不返回true。我尝试了一些日期时间转换,但这也无济于事。
我的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
但是我的if不返回true。
最佳答案
$ node.lastwrite是一个字符串(您从文件中读取),而LastWriteTime是DateTime。您需要将它们都转换为日期,或者都转换为DateTime。
如果将两者都转换为DateTime,则需要将文件时间四舍五入到最接近的秒。在NTFS上,文件时间的精度为100纳秒。
最好将文件时间转换为相同格式的字符串。
$writeTimeString = (ls "D:\tomcat.log").LastWriteTime.ToString("MM/dd/yy HH:mm:ss")
关于powershell - 日期时间比较不匹配,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31802277/