Task scheduler messes up date format in my PowerShell script

Copper Contributor

Hello I have the following PowerShell script that calculates expired certificates and sends email about it.




$Urls = @()
$Urls = "https://....",

$Result = @()

$MinimumCertAgeDays = 5000
$dummy = 0
$count = 1
$ErrorActionPreference= 'silentlycontinue'

Foreach ($url in $Urls)
[Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }

$req = [Net.HttpWebRequest]::Create($url)
$req.GetResponse() | Out-Null

$ExpirationDate = $req.ServicePoint.Certificate.GetExpirationDateString()
$ExpDateToDT = [Datetime]::ParseExact($ExpirationDate, "dd/MM/yyyy hh:mm:ss", $null)
$DayCount = ( $( $ExpDateToDT ) - $( Get-Date ) ).Days

If ( $DayCount -le $MinimumCertAgeDays )
 $dummy = 1

 $Result += "
   $count) Certificate for Host: $url  Expires on ----->  $ExpirationDate    Expires in ----->  $DayCount DAYS `n "  
  $count = $count + 1



If ($dummy -eq 1 )

   # Sender and Recipient Info
   $MailFrom = "myemail"
   $MailTo = "myemail"

   # Sender Credentials
   $Username = "myusername"
   $Password = "mypass"

   # Server Info
   $SmtpServer = "myserver"
   $SmtpPort = "myport"

   # Message stuff
   $MessageSubject = "!!!!!!!!!!!!!!SOS CERTIFICATES ARE GOING TO EXPIRE!!!!!!!!!!!!!!!!!!" 
   $Message = New-Object System.Net.Mail.MailMessage $MailFrom,$MailTo
   #$Message.IsBodyHTML = $true
   $Message.Subject = $MessageSubject
   $Message.Body = $Result
   $Message.Priority = "high"  

   # Construct the SMTP client object, credentials, and send
   $Smtp = New-Object Net.Mail.SmtpClient($SmtpServer,$SmtpPort)
   $Smtp.EnableSsl = $true
   $Smtp.Credentials = New-Object System.Net.NetworkCredential($Username,$Password)




The script runs successfully when i trigger it from Powershell ISE and i got the following email as exactly i wanted (please look image 1 below)




Nevertheless when i run the exact same script BUT this time through task scheduler in windows (in order to automate this procedure) i get the following results (please see image 2 below)




So as it can be detected by image 2, through task scheduler i get a crazy negative number of days remaining for expiration and also the πμ word in Greek :).


Can somebody may help on how to resolve that problem?


Best regards,


2 Replies
best response confirmed by Alexandros8888 (Copper Contributor)


As I see it, its a Culture issue 

which account is used to run the script, let me tell you this

Actually, your script is throwing an Exception, which is String was not recognized as a valid DateTime

But it was not displayed and totally ignored due to $ErrorActionPreference.

As a quick fix you will need to configure the account which is used to Run the script as with the same System TimeDate configuration or try to load the culture

The reason why the $Result is in minus when running it using the scheduler is $ExpDateToDT is Null



Hope this helps.



If this Answer help, please click on Best Response and give like



Thank you very much for your solution and sorry for the late response.  I followed the quick fix you recommended and worked smoothly.


Best regards,