2

There is a command which executes and give me the following output

Customer ID Client Name Computer Name Computer Brand
123        user1    127.0.0.1  lenovo
1          user2    127.0.0.2  apple
86         user3    127.0.0.1  dell
21         user4    127.0.0.4  apple

I want to get the Customer ID of users who use Computer Brand apple. The output should be as follows, so that I can do further operations on those ID's:

1
21

I tried to first select the users with 'apple' using the following:

$output | Select-String -pattern 'apple'

but then it will just print users with 'apple', but not the first row which makes it difficult for me to process further.

Update

I do not know if the following helps.

PS> $output.GetType();

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array

3 Answers 3

4

You are missing basic PowerShell concepts of the cmdlets Where-Object and Select-Object

#simulating $output
$output = @"
Customer ID,Client Name,Computer Name,Computer Brand
123,user1,127.0.0.1,lenovo
1,user2,127.0.0.2,apple
86,user3,127.0.0.1,dell
21,user4,127.0.0.4,apple
"@ -split '\r?\n' | ConvertFrom-Csv

$output | Where 'Computer Brand' -eq 'apple' | Select-Object 'Customer ID'

Sample output:

Customer ID
-----------
1
21

EDIT just in case your output isn't an object but pure text, try this

$output -replace '\s{2,}',',' | Select -skip 1 | 
  Convertfrom-Csv -Header 'Customer ID','Client Name','Computer Name','Computer Brand' | 
    Where 'Computer Brand' -eq 'apple' | Select-Object 'Customer ID'

Or simpler

($output | sls '^(\d+).*apple').Matches|% {$_.groups[1].value}
Sign up to request clarification or add additional context in comments.

5 Comments

The output is not CSV. It is as I have shown in the question.
Guess what the comment #simulating data is meant for? I'll replace $Data with $output to be easier to understand.
hmm strange, it is giving me no output. Also, you are missing single quote for closing Computer Brand. Also, I will not need "Customer ID"
The first one gave me the output with "Customer ID" which I do not want. The simpler one gives me the output that I wanted. Now, I need to do some operation on those ID's as do some work on ID
To omit the header in the 1st, change the Select to Select-Object -ExpandProperty 'Customer ID'
1

If we assume $output is an object with the properties listed in your header, then you can do the following to access the Customer ID value:

$output.where({$_.'Computer Brand' -eq 'apple'}).'Customer ID'

The following will list the all of the properties for the object that has the targeted Computer Brand:

$output.where({$_.'Computer Brand' -eq 'apple'})

If $output is already an array with lines of text, you can do something like the following:

$output = ($output | Select-Object -skip 1) -replace "\s+","," |
             ConvertFrom-Csv -Header 'Computer ID','Client Name','Computer Name','Computer Brand'

$output.where({$_.'Computer Brand' -eq 'apple'}).'Customer ID'

Comments

0

this might come in handy:

((query user) -replace ">","") -replace "  {2,}","`t" |ConvertFrom-Csv -Delimiter "`t" |select -ExpandProperty USERNAME

This gives back a list of currently logged on users. Only the USERNAME column, no headers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.