Need sorting in Output ForEach

This topic contains 6 replies, has 4 voices, and was last updated by Profile photo of Albert van Boerum Albert van Boerum 7 months, 2 weeks ago.

  • Author
    Posts
  • #59619
    Profile photo of Albert van Boerum
    Albert van Boerum
    Participant

    Hello,

    I have a script for enumerating the folder-date of Progress software on my computers.

    The script runs fine but I want to sort the output on the lastwrite time. I'm a little stuck here.
    Can anyone please help me in the right direction?

    The output I get is:

    Root         LastWriteTime      
    ----         -------------      
    \\WKS2000\c$ 1-12-2016 11:01:55 
    \\WKS2001\c$ 1-12-2016 11:05:36 
    \\WKS2002\c$ 1-12-2016 11:08:33 
    \\WKS2005\c$ 6-6-2016 15:49:44  
    \\WKS2007\c$ 6-6-2016 15:49:44  
    \\WKS2008\c$ 1-12-2016 12:38:55 
    \\WKS2009\c$ 31-10-2016 14:13:07
    \\WKS2010\c$ 2-12-2016 14:12:21 
    \\WKS2011\c$ 31-10-2016 14:13:06
    \\WKS2012\c$ 31-10-2016 14:16:19
    \\WKS2013\c$ 31-10-2016 14:13:05
    \\WKS2015\c$ 31-10-2016 14:16:26
    \\WKS2016\c$ 31-10-2016 14:13:21
    \\WKS2017\c$ 31-10-2016 13:27:46
    \\WKS2019\c$ 6-6-2016 15:49:44  
    \\WKS2020\c$ 2-12-2016 11:19:36 
    \\WKS2021\c$ 2-12-2016 14:52:01 
    \\WKS2022\c$ 2-12-2016 09:20:00 
    WKS2023 Software installation not found
    \\WKS2024\c$ 31-10-2016 14:19:22
    \\WKS2025\c$ 1-12-2016 12:59:26 
    \\WKS2027\c$ 31-10-2016 14:13:04
    \\WKS2028\c$ 2-12-2016 14:04:00 
    

    The code I use is:

    $allepcs2 = Get-ADComputer -Filter {(Name -like "wks*") -and (enabled -eq "true")}  | Where { Test-Connection $_.name -count 1 -quiet } |Select-Object -expand Name
    
    foreach ($pc in $allepcs2) 
        {
        if ( Test-Path "\\$pc\c$\Program Files (x86)\Progress Software Corporation\OpenEdge 11.5 Shared Network Installation\bin" )
            {
            get-childitem "\\$pc\c$\Program Files (x86)\Progress Software Corporation\OpenEdge 11.5 Shared Network Installation\b*" |select root,lastwritetime
            }
        else
            {
            Write-host "$pc Software installation not found"
        }
     
    }
    
  • #59626
    Profile photo of Olaf Soyk
    Olaf Soyk
    Participant

    Albert,

    first – I would remove the 'Write-Host' line. If Don sees this he will not be amused. 😉

    Then you can store the result of your foreach loop in a variable and sort it then afterwards like this:

    $Variable | Sort-Object -Property lastwritetime
    • #59632
      Profile photo of Albert van Boerum
      Albert van Boerum
      Participant

      Haha, good point. I will change it into a warning. (sorry, Don. I should know better as I'm reading your monthly lunches books)

      on-topic: I was trying to do so as you suggested but I'm unable to put my foreach into a variable. Probably some basic thing but I can't seem to get it.

      How do I do this?

  • #59638
    Profile photo of Mark Prior
    Mark Prior
    Participant

    Hi Albert, should be as simple as below

    $allepcs2 = Get-ADComputer -Filter {(Name -like "wks*") -and (enabled -eq "true")}  | Where { Test-Connection $_.name -count 1 -quiet } |Select-Object -expand Name
    
    $FEVarible = foreach ($pc in $allepcs2) 
        {
        if ( Test-Path "\\$pc\c$\Program Files (x86)\Progress Software Corporation\OpenEdge 11.5 Shared Network Installation\bin" )
            {
            get-childitem "\\$pc\c$\Program Files (x86)\Progress Software Corporation\OpenEdge 11.5 Shared Network Installation\b*" |select root,lastwritetime
            }
        else
            {
    #Every Time you use write-host god shoots a puppy#
    
            Write-host "$pc Software installation not found"
        }
     
    }
    
    $FEVarible | Sort-Object -Property lastwritetime
    
  • #59643
    Profile photo of Ron
    Ron
    Participant

    Change write-host so it creates a matching object.

    new-object psobject -Property @{root=$pc;lastwritetime="Software installation not found"}

    The sort will group them together.

    • #59653
      Profile photo of Albert van Boerum
      Albert van Boerum
      Participant

      And so should it be... 🙂 thanks for everything, I'm up and running now.

You must be logged in to reply to this topic.