Is this what you are looking for?
$MyString = 'Change: 2021-05-31 09:57:55.851417200 -0700'
$MyString.split(' ')[1]
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hi all,
in linux i wrote a bash script, that check the modification date a file:
changedate_letsencrypt=$(stat /opt/certbot/letsencrypt.pfx | grep Change | awk '{print $2}')
date_now=$(date "+%F")
if [ $changedate_letsencrypt == $date_now ]; then
mail -s "Tomcat_Certificate" ....... <<< 'Certificate is updated'
else
mail -s "Tomcat_Certificate" ........ <<< 'Certificate is not updated'
fi
I need to modify for windows powershell script, but i don't know how to replace awk. I read on the internet that need to use gc, but does not help me. For example
Linux
awk -F ":" '{print $1}' file.txt
PowerShell
gc file.txt | %{ $_.Split(':')[1]; }
I wrote followings:
PS C:\Users\Administrator\Desktop\test> $changedate_letsencrypt = stat C:\certbot\letsencrypt.pfx | Select-String change
PS C:\Users\Administrator\Desktop\test> $changedate_letsencrypt
Change: 2021-05-31 09:57:55.851417200 -0700
And i need only this to print and 2021-05-31 which should be compared with today's date in the form 2021-05-31
Thank you.
Is this what you are looking for?
$MyString = 'Change: 2021-05-31 09:57:55.851417200 -0700'
$MyString.split(' ')[1]
Hi MotoX80,
thank you for your answer. Unfortunately this not work for me. May be need to add or modify something. But i found how to make it. Below is the code that I wrote:
$date_today = Get-Date -Format "dd/MM/yyyy"
$mod_date = Get-Item C:\User\Administrator\test.txt | Foreach {$_.LastWriteTime}
$format_date = Get-Date $mod_date -Format "dd/MM/yyyy"
if ($format_date -like $date_today) {
Write-Host "The Certificate is renewed"
Send-MailMessage -From ".........." -To ".............." -Subject "$env:COMPUTERNAME Letsencrypt Certificate" -Body "The Certificate is renewed" -SmtpServer ".........."
}
else {
Write-Host "The Certificate is not renewed"
Send-MailMessage -From ".............." -To "...................." -Subject "$env:COMPUTERNAME Letsencrypt Certificate" -Body "The Certificate is not renewed" -SmtpServer ".........."
}
Anyway, thank you.
Dealing with dates as text strings isn't the way to do this with PowerShell.
$mod_date = (Get-Item c:\junk\fwd.csv).LastWriteTime.Date
if ($mod_date -eq (Get-Date).Date){
Write-Host "The Certificate is renewed"
# Send-MailMessage . . .
}
else{
Write-Host "The Certificate is not renewed"
# Send-MailMessage . . .
}