繁体   English   中英

日期和时间差的Powershell变量查询

[英]Powershell variable interrogation of date & time difference

大家早,

我有一个变量,如下所示: $machines = $user2,$name,$serial,$purchased $$ machines中存储的$machines = $user2,$name,$serial,$purchased示例数据是:

User1,
Laptop1,
xyz1234,
01/01/2010

我想创建一个名为$ tobereplaced的新变量,其中包含$ machines中所有记录的日期都比今天的日期早4年。

这个即时消息的模糊逻辑代码可能像$tobereplaced = $machines.$purchased | where {$_$purchased = -getdate > 4 years} $tobereplaced = $machines.$purchased | where {$_$purchased = -getdate > 4 years}等,但我不太清楚。

协助将不胜感激。

谢谢

$fourYearsAgo = (Get-Date).AddYears(-4) 
$tobereplaced = $machines | Where-Object { (Get-Date $_[-1]) -le $fourYearsAgo }

将日期转换为DateTime并将其与四年前的日期进行比较。 像这样

# Assuming $m[3] contains the timestamp, parse it as a DateTime and compare
# against a date four years ago.
if([DateTime]::Parse($m[3]) -le [DateTime]::Now.AddYear(-4)) {
  $tobereplaced += $m
}

根据您的语言环境,您可能需要告诉[DateTime]::Parse()如何解析日期。 是2010年12月1日或2010年12月1日还是2010年1月12日?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM