2

I had a variable $gh with the below output. I mean when I type $gh. I will get values like below.

PS C:\Windows\system32>$gh

No.  Department Name          Environment Id           Location Name   Ready Status          Machine Pure Status   
1    EMA-MET-CAT5-KY04        Environment-k2345             EMA               Enabled                  GREEN         
2    EMA-MET-CAT5-KY03        Environment-k89               EMA               Enabled                  GREEN         
3    EMA-EFT-JIU-FC           Environment-k3456             EMA               Enabled                  GREEN         
4    EMA-MET-CAT5-KY08        Environment-k7890             EMA               Not Ready                UNKNOWN       
5    EMA-MET-CAT5-KY02-ED      Environment-k9                EMA               Enabled                  GREEN         

The type of $gh is System.String as indicated by $gh.GetType().fullname.

I would like to get values from department name and environment id into a array or variable so that if i query about EMA-MET-CAT5-KY08 i can get environment id Environment-k7890.

2 Answers 2

1

Assuming the department name and environment Id have no spaces in them, I would use a regular expression (regex) looking for the pattern at the start of a line (number, spaces, name, spaces, id) and put them into a hashtable.

PS C:\> $lookup = @{}
PS C:\> [regex]::matches($gh, '(?m)^\d+\s+([^\s]+)\s+([^\s]+)').foreach{
    $lookup[$_.groups[1].value] = $_.groups[2].value
}
PS C:\> $lookup

Name                           Value
----                           -----
EMA-EFT-JIU-FC                 Environment-k3456
EMA-MET-CAT5-KY02-ED           Environment-k9
EMA-MET-CAT5-KY04              Environment-k2345
EMA-MET-CAT5-KY03              Environment-k89
EMA-MET-CAT5-KY08              Environment-k7890


PS C:\> $lookup['EMA-MET-CAT5-KY08']
Environment-k7890
Sign up to request clarification or add additional context in comments.

Comments

0
  • A different approach is to split your table into columns
    by replacing two or more spaces with a comma
  • as you have proper headers you can then ConvertFrom-Csv and
  • Select-Object the wanted properties

$gh = "No.  Department Name          Environment Id           Location Name   Ready Status          Machine Pure Status   
1    EMA-MET-CAT5-KY04        Environment-k2345             EMA               Enabled                  GREEN         
2    EMA-MET-CAT5-KY03        Environment-k89               EMA               Enabled                  GREEN         
3    EMA-EFT-JIU-FC           Environment-k3456             EMA               Enabled                  GREEN         
4    EMA-MET-CAT5-KY08        Environment-k7890             EMA               Not Ready                UNKNOWN       
5    EMA-MET-CAT5-KY02-ED      Environment-k9                EMA               Enabled                  GREEN        
"

$Data = $gh -split [environment]::newline | ForEach-Object {
    $_ -replace ' {2,}',','
} | ConvertFrom-csv | Select-Object "Department Name","Environment Id"

$Data | Where-Object 'Department Name' -eq 'EMA-MET-CAT5-KY08'

Sample output:

Department Name   Environment Id
---------------   --------------
EMA-MET-CAT5-KY08 Environment-k7890 

1 Comment

Thank you for the reply. I have two question 1:- How to convert the system.string to PSObject. for example:- to print or retrieve a selected value. In the above table, $gh is having the table output so if i print or export-csv i am not getting the table format but the export-csv is giving "Length" "750".. question 2:- The column headings "Department Name"/"Environment Id" is having spaces in the names, how to read headings even though it is having spaces...

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.