2017-08-30 96 views
0

我写了一个PowerShell脚本来捕获McAfee AVDate,并且它也提供输出。但问题是,我在脚本中添加了另一行,如果McAfee AVDate日期比当前日期早2天,则应该以红色显示McAfee AVdate,但这不起作用。PowerShell脚本查询

任何人都可以帮我解决这个问题吗?

$AVDate = (Get-ItemProperty -Path "HKLM:\SOFTWARE\Wow6432Node\McAfee\AVEngine").AVDatDate 
$AVDatDate = $AVDate 
$thedate = get-date -date $(get-date).adddays(-2) -format yyyy-MM-dd 

if($AVDatDate -lt $thedate) { 

Add-Content $report "<tr>" 
    Add-Content $report "<td bgcolor= 'White' height='30' align=center><B>12</B></td>" 
    Add-Content $report "<td bgcolor= 'White' height='30' align=left><B>McAfee AVDate</B></td>" 
    Add-Content $report "<td bgcolor= 'red' height='30' align=left><B>$AVDatDate</B></td>" 
Add-Content $report "</tr>" 

} 

else 

{ 

Add-Content $report "<tr>" 
    Add-Content $report "<td bgcolor= 'White' height='30' align=center><B>12</B></td>" 
    Add-Content $report "<td bgcolor= 'White' height='30' align=left><B>McAfee AVDate</B></td>" 
    Add-Content $report "<td bgcolor= 'Aquamarine' height='30' align=left><B>$AVDatDate</B></td>" 
Add-Content $report "</tr>" 

} 
+0

因为它永远不会去else块。您必须检查来自$ avdate的日期,并相应地输入条件 –

+0

日期即将采用此格式“McAfee AVDate \t 2017/06/21”,但颜色不会变为红色。由于日期比当前日期早了9天 – Sandeep

+0

因此您明确提及elseif中的条件 –

回答

0

如果$avDate是 “的McAfee AVDate 2017年6月21日”,那么

$avDate -lt [datetime]::Today.AddDays(-2) 

将总是假,因为日期将被转换成字符串(以匹配$avDate)和字符串比较完成(并且数字比较小于字母)。

您需要提取并解析日期并进行比较。假设格式为 “yyyy/MM/DD” 总是那么:

$d = [DateTime]::ParseExact($avDate.Substring(14), "yyyy'/'MM'/'dd", $null); 

,然后做$d日期比较。

+0

我编辑了上面的脚本。但现在甚至比有限的一天数据变红。当AVDate比当前系统日期早2天时,我只想要数据是红色的.. – Sandeep

+0

@Sandeep鉴于(如您在问题的评论中注释)以其他文本开头,将它与日期进行比较将不起作用。这就是为什么我在这个答案中提取日期部分的原因:对'Substring'的调用。 – Richard