Asked by:
Conditioned output from Select-Object

Question
-
I have an object that's beeing piped to a Select-Object and then piped to an Out-GridView.
In one of the columns there is data that's hard for the end user to understand. So, I'd like to set up a condition to what's printed out in the Out-GridView.
Like: "IF text = '2jk4h52kljh' THEN output = 'Hello!' ELSEIF text = 'h42klj5h2345' THEN output = 'Goodbye!' ELSE output = 'nothing'" in just one specific column.
Can't get the hang of it, anyone have any ideas?
//Daniel
- Moved by Bill_Stewart Tuesday, November 7, 2017 10:38 PM Unanswerable drive-by question
Friday, September 29, 2017 12:29 PM
All replies
-
Hi Daniel,
you can do this with a slightly non-standard hashtable:
$select_conditional = @{ N = "Text" E = { if ($_.Text -eq '2jk4h52kljh') { 'Hello!' } elseif ($_.Text -eq 'h42klj5h2345') { 'Goodbye!' } else { 'nothing' } } } $object | Select Foo, Bar, $select-conditional | ogv
Cheers,
FredThere's no place like 127.0.0.1
Friday, September 29, 2017 12:34 PM -
Great! Thanks Fred.
Almost there! Suppose that column "Bar" (as the one in 'Select Foo, Bar, ...) is the column containing the data I want to compare and manipulate, is it just to change the N = "Text" to N = $_.Bar?
//Daniel
Friday, September 29, 2017 1:18 PM -
Hi Daniel,
not quite. That hashtable has two segments: N (Name) and E (Expression). The N part is the new name of the property. You'd be replacing all instances of $_.Text with $_.Bar within the E (Expression) part of the Hashtable.
Basically, with that, for each object you pipe to Select-Object, it will run the code in E and add it to the object as the value of the property named as the text in N.
Cheers,
FredExample:
$select_conditional = @{ N = "Text" E = { if ($_.Bar -eq '2jk4h52kljh') { 'Hello!' } elseif ($_.Bar -eq 'h42klj5h2345') { 'Goodbye!' } else { 'nothing' } } } $object | Select Foo, Bar, $select-conditional | ogv
PS: You are of course totally free to set the name to any legal property name.
There's no place like 127.0.0.1
Friday, September 29, 2017 1:25 PM -
OK! Thanks a bunch.
Even so, I still get nothing but 'nothing' in my column. I'm guessing that the content is'nt actually a string but something else. The object that I'm getting is Get-MsolUser (from our Office 365 subscription) and the data I'm trying to get hold of is the license data to each MsolUser. Some users has a standard Exchange license and some has the O365 Business license as well.
My idea was to -match the content with a known part of the content and print out 'Exchange' or 'Office'.
So, I'm guessing that an output that looks like {account:O365_BUSINESS, account:EXCHANGESTANDARD} in fact is some kind of array(?) that has to be handeled right?
//Daniel
Friday, September 29, 2017 1:41 PM -
Looks like it, yes.
You can cheat it, if you want to, but handling the actual content would be more reliable.
# Cheating like hell $select_conditional = @{ N = "Text" E = { if ($_.Bar.ToString() -like '*2jk4h52kljh*') { 'Hello!' } elseif ($_.Bar.ToString() -like '*h42klj5h2345*') { 'Goodbye!' } else { 'nothing' } } } $object | Select Foo, Bar, $select_conditional | ogv
Cheers,
FredThere's no place like 127.0.0.1
Friday, September 29, 2017 1:53 PM