Need help: filtering required values

Hi,

I am newbie to powershell and trying to filter required values and need help here.

Scenario:

From csv file which has columns name, u_last_backup, I’m trying to filter only names which are dated other than $strdates 2018-10-17,2018-10-16,2018-10-15,2018-10-14

Script below:

$hostswithnolatestbackup= import-csv ‘.\hostswithcurrentbackupdate.csv’
$server=@()

foreach($hosts in $hostswithnolatestbackup)
{
if ($strdates -contains $hosts.u_last_backup)
{

write-log “server has latest backups”

}

else
{
$hostmove= $hosts.name
}
$server+=$hostmove
}

In this script, though u_last_backup value is in list of $strdates, still I see if loop is not executed and goes to else statement and indeed all host names come up though having latest backup.

What am I missing here? Please suggest.

 

 

Isn’t that contains statement backwards?

PS C:\Users\admin> 1 -contains 1,2,3
False
PS C:\Users\admin> 1,2,3 -contains 1
True

Hi,

yeah, I did give condition like

1,2,3 -contains 1

if ($strdates -contains $hosts.u_last_backup)

Here $strdates have values 2018-10-17,2018-10-16,2018-10-15,2018-10-14

and $hosts.u_last_backup contains backup date in specific for 1 host in foreach loop.

Let me know if I need to change something?

Thanks

 

Can I see where $strdates is set and part of the csv? Contains wouldn’t be used this way:

PS C:\Users\admin> 'hi there' -contains 'hi'
False

Sure,

strdates script as below:

$strDates = “”

 

for($x=0;$x -gt -4;$x–){

 

$date = (get-date).AddDays($x)

 

If($strDates){

 

$strDates += “,” + $(Get-Date $date -Format “yyyy-MM-dd”)

 

 

 

}

 

 

else {

 

$strDates = Get-Date $date -Format “yyyy-MM-dd”

 

 

 

}

 

}

 

and csv looks like

u_last_backup
name

3/15/2017
hostname

10/15/2018
hostname

10/15/2018
hostname

5/11/2014
hostname

10/15/2018
hostname

10/15/2018
hostname

3/20/2017
hostname

10/15/2018
hostname

10/15/2018
hostname

though the format of date in csv is MM/DD/YYYY , after import.csv when I check the date format, it shows as yyyy-MM-DD so I used this format.

10/15/2018
ctx-mof-608-p

 

 

If I understand you correctly, $strdates is a string, not an array. -contains tests against arrays. This might fix it:

$strdates = $strdates -split ','

This might be a bit messy, but the dates could be included in the if statement.

CSV:

u_last_backup Name


10/17/2018 Server A
10/16/2018 Server B
10/15/2018 Server C
10/14/2018 Server D
10/13/2018 Server F
10/12/2018 Server G

$Hosts = Import-Csv .\Hostswithnobackupdates.csv

Foreach ($Server in $Hosts) {

    $ServerName = $Server.Name
    $Date = $Server.u_last_backup

        if ($Date.Contains("10/17/2018") -or ($Date.Contains("10/16/2018")) -or ($Date.Contains("10/15/2018") -or ($Date.Contains("10/14/2018"))))  {

            Write-Host "$ServerName Has the latest backups"
            }

        Else {

            Write-Host "$ServerName Does not have the latest backups"
            }
    }

Returned Values:
Server A Has the latest backups
Server B Has the latest backups
Server C Has the latest backups
Server D Has the latest backups
Server F Does not have the latest backups
Server G Does not have the latest backups

Split helped to pull the desired result.

Thanks for your help!