Compare dates with powershell

0

Good afternoon;

I have a problem when comparing dates with powershell, the dates with which I make the comparison are in a CSV file, example:

titulo    - recordatorio - vence
Noticia01 - 13/01/2018   - 30/01/2018
Noticia02 - 14/01/2018   - 30/01/2018

where, the reminder date will make a comparison with the current date and if they are the same they will send me an alert.

This is the powershell script

    $csv = "datos.csv"
    $datos = Import-CSV $csv
    $fr = ForEach ($var in $datos)
        {
            $var.recordatorio
        }

    $fa = Get-Date -Format d

    if($fa -eq $fr)
        {
            echo "está proximo a vencer la siguiente noticia"           
        }

    else
        {
            echo "no hacer nada"
        }

but at the moment of executing and comparing dates it does not, please support me in that I am wrong.

    
asked by Carlos 12.01.2018 в 18:53
source

1 answer

0

First what you should do to make things easier is to normalize the .csv file, to use for example a more common delimiter such as the comma (,).

The file would be as follows:

titulo,recordatorio,vence
Noticia01,13/01/2018,30/01/2018
Noticia02,14/01/2018,30/01/2018

This allows that when importing the .csv you do not need to add anything. Taking the first 2 lines of the code we get the following.

PS C:\Users\vmsilvamolina> $datos

titulo    recordatorio vence     
------    ------------ -----     
Noticia01 13/01/2018   30/01/2018
Noticia02 14/01/2018   30/01/2018

After this, we have that $ data is a System.Array, which allows us to traverse it using foreach as follows:

foreach ($item in $datos) {
    if ($item.recordatorio -eq $fa) {
        Write-host "Está proximo a vencer la siguiente noticia:" $item.titulo
    } else {
        Write-host "No pasa nada"
    }
}

There I modified it so that it prints the defined message of expiration adding the title of the news.

  

With the data in the example .csv file it does not meet the condition to display the expiration message.

I enclose all the code:

$csv = 'C:\Users\vmsilvamolina\Desktop\prueba.csv'
$datos = Import-CSV $csv

#Formato de fecha
$fa = Get-Date -Format dd/MM/yyyy

foreach ($item in $datos) {
    if ($item.recordatorio -eq $fa) {
        Write-host "Se está por vencer!" $item.titulo
    } else {
        Write-host "No pasa nada"
    }
}

UPDATE:

From the request expressed in the comments, I modified the code to print all the news that meet the condition, separated by a comma:

$ csv = 'C: \ Users \ vmsilvamolina \ Desktop \ test.csv' $ data = Import-CSV $ csv

$ fa = Get-Date -Format dd / MM / yyyy $ news = $ null

foreach ($ item in $ data) {     if ($ item.recordatorio) {         $ news + = $ item.title + ","     } else {         Write-host "Nothing happens"     } }

Write-host "It's about to expire!" $ news.Substring (0, $ news.Length-2)

  

The Substring () method was used to delete the last comma (,) that is added to the end of the chain.

    
answered by 12.01.2018 / 20:45
source