1

I am not able to get the correct report with the below part of code. Here's what I am doing:

  1. Importing .csv file into variable which contains UserName, LastSucessSync (date), Model.
  2. With below foreach loop this will format the .html report and provide the values Green or Red.

Issue is, when Date is blank in .csv file it still shows GREEN or some times RED. It also throws the below error:

Cannot convert value "" to type "System.DateTime". Error: "String was not
recognized as a valid DateTime."
At C:\MobileStats.ps1:87 char:4
+ if([datetime]$Entry.LastSuccessSync -lt (Get-Date).AddDays(-14) -or $Entry.LastS ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvalidCastParseTargetInvocationWithFormatProvider

Rest of all is fine. Just I want to mark it as RED when date is blank or 14 days before.

PowerShell Code:

foreach ($Entry in $Result) {
    if ([datetime]$Entry.LastSuccessSync -lt (Get-Date).AddDays(-14) -or $Entry.LastSuccessSync -like "") {
        $Cdrivecolor="RED"
    } else {
        $Cdrivecolor="Green"
    }
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Jeetcu
  • 43
  • 3
  • 10

0 Answers0