SOLVED

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://....",
"https://......",
"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
#$req.ServicePoint.Certificate.GetExpirationDateString()

$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)
   $Smtp.Send($Message)
}

 

 

 

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)

IMAGE 1

image1.png

 

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)

IMAGE2

image2.png

 

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,

Alexandros

2 Replies
best response confirmed by Alexandros8888 (Copper Contributor)
Solution

@Alexandros8888 

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

https://stackoverflow.com/questions/44654045/get-date-formatting-culture

https://docs.microsoft.com/en-us/dotnet/api/system.datetime.parseexact?view=net-5.0

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

 

@farismalaeb 

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,

Alexandros

1 best response

Accepted Solutions
best response confirmed by Alexandros8888 (Copper Contributor)
Solution

@Alexandros8888 

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

https://stackoverflow.com/questions/44654045/get-date-formatting-culture

https://docs.microsoft.com/en-us/dotnet/api/system.datetime.parseexact?view=net-5.0

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

 

View solution in original post