Reputation: 77
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.
Upvotes: 2
Views: 81
Reputation:
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
Upvotes: 0
Reputation: 28983
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
Upvotes: 1