sort before or after

Welcome Forums General PowerShell Q&A sort before or after

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

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

      Hostname : SVR0101
      Clustername : ClusterB
      IPaddress : 10.10.10.121
      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
      Participant
      Topics: 4
      Replies: 2249
      Points: 5,494
      Helping Hand
      Rank: Community MVP

      Sam,

      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
      Participant
      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
      Participant
      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
      Participant
      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.