sort before or after

Welcome Forums General PowerShell Q&A sort before or after

Viewing 4 reply threads
  • Author
    • #227626
      Topics: 7
      Replies: 8
      Points: 139
      Rank: Participant

      I have $output likes this (over 10000 records):
      Hostname : PC0101
      Clustername : ClusterA
      IPaddress :
      Folder : \\server\vmpath\ActiveDesktop

      Hostname : SVR0101
      Clustername : ClusterB
      IPaddress :
      Folder : \\server\vmpath\ActiveServer

      $output |Foreach-object { if($_.Hostname -match ‘^P’) {select-object -property Hostname, Clustername} }

      Nothing happens

      $output |select-object -property Hostname, Clustername |where {$_.Hostname -match ‘^P’}

      It works.

      Can someone point to me where is the issue?


    • #227629
      Topics: 4
      Replies: 2249
      Points: 5,494
      Helping Hand
      Rank: Community MVP


      when you post code, error messages, sample data or console output format it as code, please.
      In the “Text” view you can use the code tags “PRE“, in the “Visual” view you can use the format template “Preformatted“. You can go back edit your post and fix the formatting – you don’t have to create a new one.
      Thanks in advance.

      Select-Object needs an InputObject. Either by pipeline or explicit. You do not provide any input object in your first code snippet. ;-} You may (re-)read the help for the cmdlet including the examples to learn how to use it.

      BTW: The subject you choose does not fit your question at all in my opinion.

    • #227662
      Topics: 7
      Replies: 8
      Points: 139
      Rank: Participant
      $output |Foreach-object {$_ | if($_.Hostname -match ‘^P’) {select-object -property Hostname, Clustername} }
      It still doesn't work.
    • #227671
      Topics: 3
      Replies: 342
      Points: 1,130
      Helping Hand
      Rank: Community Hero

      You still aren’t providing it input. You can’t pipe input into if. If your condition is true in your first example, the code that executes is

      select-object -property Hostname, Clustername

      There is no input. You eat up the piped input with the foreach, now provide it back to Select-Object.

      $output |Foreach-object { if($_.Hostname -match ‘^P’) {$_ | select-object -property Hostname, Clustername} }

      So many of the built in examples could’ve answered this question. It may make it easier if you name your variables explicitly.

      foreach($item in $output){ if($item.Hostname -match ‘^P’) {select-object -inputobject $item -property Hostname, Clustername} }

      Hopefully this helps.

    • #227698
      Topics: 0
      Replies: 2
      Points: -10
      Rank: Member

      please provide input that will work for you

Viewing 4 reply threads
  • You must be logged in to reply to this topic.