locked
Filtering two objects with powershell RRS feed

  • Question

  • Hello,

    Code:

    $Information = New-Object -TypeName PSObject
    
    Add-Member -InputObject $Information -MemberType NoteProperty -Name DPName -Value $Server
    
    $LastLineFormatted = ($LastLine.Replace( '"',' ' )).Substring(158,36)
    $Z = $LastLineFormatted.Replace('.', ' ') | ConvertFrom-Csv -Delimiter ' ' -Header "Notrequired", "Time", "Notrequired2", "0", "1", "Date"
     
    Add-Member -InputObject $Information -MemberType NoteProperty -Name Timespan -Value $(New-TimeSpan -Start ([datetime]"$($Z.Date) $($Z.Time)") -End $DPlocalTime )
    
    
    $list += $Information
    
    }


    $list variable output with two objects. servername and timedifference:

    ServerName              Timedifference 

    -------------                ---------------------

    Server1.domains.net 00:36:10  
    Server2.domains.net 00:30:06  
    Server3.domains.net 00:43:04  
    Server4.domains.net 00:48:54  
    Server5.domains.net 00:42:02

    I would like to get output with server names with timedifference greater than 40 minutes.

    Ok.

    To get more than 40 minutes im typing following code:

    ($list.timedifference).minutes -gt 40

    With this im getting output just greater than 40.

    43 
    48  
    42

    Question, how can i get results with server name object and timedifference object (which servers have timedifference more than 40 minutes)  like this:

    Server3.domains.net 00:43:04  
    Server4.domains.net 00:48:54  
    Server5.domains.net 00:42:02


    • Edited by TechSpark0 Sunday, January 7, 2018 5:39 PM add
    Sunday, January 7, 2018 5:21 PM

Answers

  • Greater that what? 40 minutes?  40 Seconds?  40 days?

    Your code is incomplete and a bit unnecessary. Here is how to approach custom objects and how to manage them:

    $LastLineFormatted = ($LastLine.Replace('"', ' ')).Substring(158, 36) $Z = $LastLineFormatted.Replace('.', ' ') | ConvertFrom-Csv -Delimiter ' ' -Header "Notrequired", "Time", "Notrequired2", "0", "1", "Date"

    # ...

    $list += [pscustomobject]@{ DPName = $Server Timespan = [datetime]"$($Z.Date) $($Z.Time)" - $DPlocalTime } #.... #This filter on greeater than 40 minutes $list | Where-Object{$_.TimeSpan -gt [timespan]'0:0:40:0'}



    \_(ツ)_/





    • Edited by jrv Sunday, January 7, 2018 5:34 PM
    • Proposed as answer by Richard MuellerMVP Sunday, January 7, 2018 10:05 PM
    • Marked as answer by TechSpark0 Monday, January 8, 2018 7:24 AM
    Sunday, January 7, 2018 5:31 PM

All replies

  • Greater that what? 40 minutes?  40 Seconds?  40 days?

    Your code is incomplete and a bit unnecessary. Here is how to approach custom objects and how to manage them:

    $LastLineFormatted = ($LastLine.Replace('"', ' ')).Substring(158, 36) $Z = $LastLineFormatted.Replace('.', ' ') | ConvertFrom-Csv -Delimiter ' ' -Header "Notrequired", "Time", "Notrequired2", "0", "1", "Date"

    # ...

    $list += [pscustomobject]@{ DPName = $Server Timespan = [datetime]"$($Z.Date) $($Z.Time)" - $DPlocalTime } #.... #This filter on greeater than 40 minutes $list | Where-Object{$_.TimeSpan -gt [timespan]'0:0:40:0'}



    \_(ツ)_/





    • Edited by jrv Sunday, January 7, 2018 5:34 PM
    • Proposed as answer by Richard MuellerMVP Sunday, January 7, 2018 10:05 PM
    • Marked as answer by TechSpark0 Monday, January 8, 2018 7:24 AM
    Sunday, January 7, 2018 5:31 PM
  • Note that the use of New-Object PsObject and Add-Member to build objects are old PS 1 methods that are not currently used in this way as the new constructs above are much easier and create objects in a much more manageable way.


    \_(ツ)_/

    Sunday, January 7, 2018 5:36 PM