awk linux command in powershell

Ivaylo Stefanov 136 Reputation points
2021-05-31T12:07:31.763+00:00

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.

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. MotoX80 36,291 Reputation points
    2021-05-31T14:13:43.897+00:00

    Is this what you are looking for?

    $MyString = 'Change: 2021-05-31 09:57:55.851417200 -0700'
    $MyString.split(' ')[1]
    
    0 comments No comments

  2. Ivaylo Stefanov 136 Reputation points
    2021-05-31T15:01:34.653+00:00

    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.

    0 comments No comments

  3. Rich Matheisen 47,901 Reputation points
    2021-05-31T19:31:03.643+00:00

    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 . . . 
    }
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.