sort before or after

Welcome Forums General PowerShell Q&A sort before or after

Viewing 4 reply threads
  • Author
    • #227626
      Topics: 8
      Replies: 9
      Points: 158
      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: 10
      Replies: 2481
      Points: 6,543
      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: 8
      Replies: 9
      Points: 158
      Rank: Participant
    • #227671
      Topics: 9
      Replies: 707
      Points: 2,848
      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

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

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

      Hopefully this helps.

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

      please provide input that will work for you

Viewing 4 reply threads
  • The topic ‘sort before or after’ is closed to new replies.