1

I am using poshWSUS 2.3.1.6 module and have a query like below:

& Get-PSWSUSClient | select @{name="Computer";expression={$($_.FullDomainName)}},@{name="LastUpdated";expression={$($_.LastReportedStatusTime)}} | export-csv -NoTypeInformation -append $FileOutput

My problem is that for some computers there is no reported status time, so LastReportedStatusTime is equal to 1/1/0001 12:00:00 AM. I want to the field: LastSyncTime for those computers. Basically I need something like

expression= if (LastReportedStatusTime > '1/1/0001 12:00:00 AM') then {$($_.LastReportedStatusTime)}} else {$($_.LastSyncTime)}}  

but I couldn't find the correct syntax. I appreciate if you may help.

Eray Balkanli
  • 7,752
  • 11
  • 48
  • 82

1 Answers1

1

You could either convert your string to datetime:

if ([datetime]$_.LastReportedStatusTime -gt [datetime]'1/1/0001 12:00:00 AM') {
    $_.LastReportedStatusTime
} else {
    $_.LastSyncTime
}

I'm assuming here that $_.LastReportedStatusTime returns not a datetime itself but a string. It should work with casting only one side of the if expression to datetime, as powershell should try to convert the other part, too. But I like to be explicit in this kind of conversions.

Or for a string based version check if they match instead:

if ($_.LastReportedStatusTime -eq '1/1/0001 12:00:00 AM') {
    $_.LastSyncTime
} else {
    $_.LastReportedStatusTime
}

Don't have a WSUS to test around but for a datetime version this should do the job:

& Get-PSWSUSClient | select @{name="Computer";expression={$_.FullDomainName}},@{name="LastUpdated";expression={if ([datetime]$_.LastReportedStatusTime -gt [datetime]"1/1/0001 12:00:00 AM") {$_.LastReportedStatusTime} else {$_.LastSyncTime}}} | export-csv -NoTypeInformation -append $FileOutput
Olaf Reitz
  • 684
  • 3
  • 10