Need help: filtering required values

Welcome Forums General PowerShell Q&A Need help: filtering required values

This topic contains 7 replies, has 3 voices, and was last updated by

 
Participant
1 month, 3 weeks ago.

  • Author
    Posts
  • #114366

    Participant
    Points: 36
    Rank: Member

    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.

     

     

  • #114370
    js

    Participant
    Points: 318
    Helping Hand
    Rank: Contributor

    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
    
    • #114378

      Participant
      Points: 36
      Rank: Member

      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

       

    • #114382
      js

      Participant
      Points: 318
      Helping Hand
      Rank: Contributor

      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
      
  • #114384

    Participant
    Points: 36
    Rank: Member

    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

     

     

  • #114387
    js

    Participant
    Points: 318
    Helping Hand
    Rank: Contributor

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

    $strdates = $strdates -split ','
    
    • #114390

      Participant
      Points: 36
      Rank: Member

      Split helped to pull the desired result.

      Thanks for your help!

  • #114388
    Kaj

    Participant
    Points: 1
    Rank: Member

    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

You must be logged in to reply to this topic.