Editor's note:
The original form of this question had a well-defined problem that stemmed from accidentally -le
-comparing a [string]
LHS with a [double]
RHS (annotations added):
$c = Get-Date (Get-Date).ToUniversalTime() -UFormat %s
$epochseconds=[math]::Round($c) # $epochseconds is now a [double]
$d = get-childitem C:\scripts\PALO\* -recurse | Select-String -pattern "expiry-epoch"
$e = $d -split "epoch" # -split always returns *strings*
$certtime = $e[1] # $certtime is now a [string]
$epochtime = $epochseconds - 2505600 # $epochtime is a [double]
ForEach ($i in $d){
If ($certtime -le $epochtime) { # LHS is [string], RHS is [double]
Write-Output $i
}
}
The OP's later revision (see below), by replacing the original code, accidentally eliminated this problem by first applying a subtraction to the [string]
LHS (during which an implicit conversion to a number happens), rendering the existing answer inapplicable.
[The OP's later revision, which should be a *new* question.]
I am writing a script that is supposed to notify me if the expiry-epoch time of a certificate is within 30 days of expiration. However, if one file does not match the IF statement then I get no output, if all files match the IF statement then I get the appropriate output.
PS C:\scripts> $certtime
1560350005
PS C:\scripts> $epochtime
1520858749
I just updated the code:
$c = Get-Date (Get-Date).ToUniversalTime() -UFormat %s
$epochtimes=[math]::Round($c)
$d = get-childitem C:\scripts\PALO\* -recurse | Select-String -pattern "expiry-epoch"
$e=$d -split "epoch"
$certtime=$e[1]
$certexp = $certtime - 2592000
ForEach ($i in $d){
If ($certexp -le $epochtime) {
Write-Output $i
}
}