mercredi 6 septembre 2017

Powershell - filter/if-statement not properly working from the Pipe?

I'm piping a select-string | select-object piece of code into a sort which outputs two columns. I had two files set up with two different sorts but wanted to try and combine them to one file using a conditional based on a read-host asking the user which sort they want to use (1 or 2).

I first tried an if statement within the pipe. As far as I can tell, I can't actually do an 'if' inside of the pipe. This is the statement. $sort is set by the user who types '1' for the first sort and anything else for the second. It produces an error about not recognizing 'if'.

| if ($sort -eq 1){ sort-object { $_.'Character'  } } else { sort-object { [double]$_.'Value'  } –Descending } |

I read about making a filter, so I also tried that with this piece of code and piped the code into the filter name 'sortby':

filter sortby{
    if ($sort -eq 1){ sort-object { $_.'Character'  } } 
    else { sort-object { [double]$_.'Value'  } –Descending } 
}

This goes through without an error, but the output actually comes out blank.

Is there a way I can successfully implement a conditional sort into the pipe without error? Or did I make a mistake?

Thank you!

Aucun commentaire:

Enregistrer un commentaire