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.