2017-08-30 87 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