SOLVED

Task scheduler messes up date format in my PowerShell script

%3CLINGO-SUB%20id%3D%22lingo-sub-2043740%22%20slang%3D%22en-US%22%3ETask%20scheduler%20messes%20up%20date%20format%20in%20my%20PowerShell%20script%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2043740%22%20slang%3D%22en-US%22%3E%3CP%3EHello%20I%20have%20the%20following%20PowerShell%20script%20that%20calculates%20expired%20certificates%20and%20sends%20email%20about%20it.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%24Urls%20%3D%20%40()%0A%24Urls%20%3D%20%22https%3A%2F%2F....%22%2C%0A%22https%3A%2F%2F......%22%2C%0A%22https%3A%2F%2F....%22%0A%0A%24Result%20%3D%20%40()%0A%0A%24MinimumCertAgeDays%20%3D%205000%0A%24dummy%20%3D%200%0A%24count%20%3D%201%0A%24ErrorActionPreference%3D%20'silentlycontinue'%0A%0A%0AForeach%20(%24url%20in%20%24Urls)%0A%7B%0A%5BNet.ServicePointManager%5D%3A%3AServerCertificateValidationCallback%20%3D%20%7B%20%24true%20%7D%0A%0A%24req%20%3D%20%5BNet.HttpWebRequest%5D%3A%3ACreate(%24url)%0A%24req.GetResponse()%20%7C%20Out-Null%0A%23%24req.ServicePoint.Certificate.GetExpirationDateString()%0A%0A%24ExpirationDate%20%3D%20%24req.ServicePoint.Certificate.GetExpirationDateString()%0A%24ExpDateToDT%20%3D%20%5BDatetime%5D%3A%3AParseExact(%24ExpirationDate%2C%20%22dd%2FMM%2Fyyyy%20hh%3Amm%3Ass%22%2C%20%24null)%0A%24DayCount%20%3D%20(%20%24(%20%24ExpDateToDT%20)%20-%20%24(%20Get-Date%20)%20).Days%0A%0A%0AIf%20(%20%24DayCount%20-le%20%24MinimumCertAgeDays%20)%0A%20%7B%20%20%0A%20%0A%20%24dummy%20%3D%201%0A%0A%0A%20%24Result%20%2B%3D%20%22%0A%20%20%20%24count)%20Certificate%20for%20Host%3A%20%24url%20%20Expires%20on%20-----%26gt%3B%20%20%24ExpirationDate%20%20%20%20Expires%20in%20-----%26gt%3B%20%20%24DayCount%20DAYS%20%60n%20%22%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%24count%20%3D%20%24count%20%2B%201%0A%0A%20%7D%0A%0A%7D%0A%0AIf%20(%24dummy%20-eq%201%20)%0A%20%7B%0A%20%0A%0A%20%20%20%23%20Sender%20and%20Recipient%20Info%0A%20%20%20%24MailFrom%20%3D%20%22myemail%22%0A%20%20%20%24MailTo%20%3D%20%22myemail%22%0A%0A%20%20%20%23%20Sender%20Credentials%0A%20%20%20%24Username%20%3D%20%22myusername%22%0A%20%20%20%24Password%20%3D%20%22mypass%22%0A%0A%20%20%20%23%20Server%20Info%0A%20%20%20%24SmtpServer%20%3D%20%22myserver%22%0A%20%20%20%24SmtpPort%20%3D%20%22myport%22%0A%0A%20%20%20%23%20Message%20stuff%0A%20%20%20%24MessageSubject%20%3D%20%22!!!!!!!!!!!!!!SOS%20CERTIFICATES%20ARE%20GOING%20TO%20EXPIRE!!!!!!!!!!!!!!!!!!%22%20%0A%20%20%20%24Message%20%3D%20New-Object%20System.Net.Mail.MailMessage%20%24MailFrom%2C%24MailTo%0A%20%20%20%23%24Message.IsBodyHTML%20%3D%20%24true%0A%20%20%20%24Message.Subject%20%3D%20%24MessageSubject%0A%20%20%20%24Message.Body%20%3D%20%24Result%0A%20%20%20%24Message.Priority%20%3D%20%22high%22%20%20%0A%0A%20%20%20%23%20Construct%20the%20SMTP%20client%20object%2C%20credentials%2C%20and%20send%0A%20%20%20%24Smtp%20%3D%20New-Object%20Net.Mail.SmtpClient(%24SmtpServer%2C%24SmtpPort)%0A%20%20%20%24Smtp.EnableSsl%20%3D%20%24true%0A%20%20%20%24Smtp.Credentials%20%3D%20New-Object%20System.Net.NetworkCredential(%24Username%2C%24Password)%0A%20%20%20%24Smtp.Send(%24Message)%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20script%20runs%20%3CSTRONG%3Esuccessfully%3C%2FSTRONG%3E%20when%20i%20trigger%20it%20from%20Powershell%20ISE%20and%20i%20got%20the%20following%20email%20as%20exactly%20i%20wanted%20(please%20look%20image%201%20below)%3C%2FP%3E%3CP%3EIMAGE%201%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22image1.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F244897i59D2960AD38CD968%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22image1.png%22%20alt%3D%22image1.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENevertheless%20when%20i%20run%20the%20exact%20same%20script%20BUT%20this%20time%20through%20task%20scheduler%20in%20windows%20(in%20order%20to%20automate%20this%20procedure)%20i%20get%20the%20following%20results%20(please%20see%20image%202%20below)%3C%2FP%3E%3CP%3EIMAGE2%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22image2.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F244898iA355DBEA93D1A701%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22image2.png%22%20alt%3D%22image2.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESo%20as%20it%20can%20be%20detected%20by%20image%202%2C%20through%20task%20scheduler%20i%20get%20a%20crazy%20negative%20number%20of%20days%20remaining%20for%20expiration%20and%20also%20the%20%CF%80%CE%BC%20word%20in%20Greek%20%3A).%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECan%20somebody%20may%20help%20on%20how%20to%20resolve%20that%20problem%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBest%20regards%2C%3C%2FP%3E%3CP%3EAlexandros%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2043740%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2044257%22%20slang%3D%22en-US%22%3ERe%3A%20Task%20scheduler%20messes%20up%20date%20format%20in%20my%20PowerShell%20script%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2044257%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F878526%22%20target%3D%22_blank%22%3E%40Alexandros8888%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAs%20I%20see%20it%2C%20its%20a%20Culture%20issue%26nbsp%3B%3C%2FP%3E%3CP%3Ewhich%20account%20is%20used%20to%20run%20the%20script%2C%20let%20me%20tell%20you%20this%3C%2FP%3E%3CP%3EActually%2C%20your%20script%20is%20throwing%20an%20Exception%2C%20which%20is%26nbsp%3B%3CSTRONG%3E%3CEM%3EString%20was%20not%20recognized%20as%20a%20valid%20DateTime%3C%2FEM%3E%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3EBut%20it%20was%20not%20displayed%20and%20totally%20ignored%20due%20to%20%3CEM%3E%24ErrorActionPreference.%3C%2FEM%3E%3C%2FP%3E%3CP%3EAs%20a%20quick%20fix%20you%20will%20need%20to%20configure%20the%20account%20which%20is%20used%20to%20Run%20the%20script%20as%20with%20the%20same%20System%20TimeDate%20configuration%20or%20try%20to%20load%20the%20culture%3C%2FP%3E%3CP%3E%3CEM%3E%3CA%20href%3D%22https%3A%2F%2Fstackoverflow.com%2Fquestions%2F44654045%2Fget-date-formatting-culture%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fstackoverflow.com%2Fquestions%2F44654045%2Fget-date-formatting-culture%3C%2FA%3E%3C%2FEM%3E%3C%2FP%3E%3CP%3E%3CEM%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fdotnet%2Fapi%2Fsystem.datetime.parseexact%3Fview%3Dnet-5.0%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fdotnet%2Fapi%2Fsystem.datetime.parseexact%3Fview%3Dnet-5.0%3C%2FA%3E%3C%2FEM%3E%3C%2FP%3E%3CP%3EThe%20reason%20why%20the%20%24Result%20is%20in%20minus%20when%20running%20it%20using%20the%20scheduler%20is%26nbsp%3B%3CSTRONG%3E%24ExpDateToDT%3C%2FSTRONG%3E%20is%20Null%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHope%20this%20helps.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E---------------------%3C%2FP%3E%3CP%3EIf%20this%20Answer%20help%2C%20please%20click%20on%20Best%20Response%20and%20give%20like%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional 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 (Occasional 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