1

I have an array that i load from CSV with import csv. The array $s has $s.Name and $s.IPAddress.

When I m running jobs I want to filter the failed jobs and then compare them with the array $s where the IPAddress matches, finally I want to show the values in $s with $s.Name and $s.IPAddress...

So far I can only get the IPAddress because after I apply the filter Where I1m left with only that property.

How can I get the correspondent array value for Name and IPAddress where I find the match?

Here's the code as you see only returns IP address, but I want the correspondent Name that exists in $s as well

PS C:\> $j.ChildJobs | ?{$_.State -eq 'Failed'} | %{$s.IPAddress -eq $_.Location}
192.1.8.149
192.1.8.152
192.1.8.155

If I do $s(not filtered), I have Name and IPAddress

PS C:\> $s

Name        IPAddress
----        ---------
Server100   192.1.8.148
Server101   192.1.8.149
Server102   192.1.8.150
Server103   192.1.8.151
Server104   192.1.8.152
Server105   192.1.8.153
Server106   192.1.8.154
Server107   192.1.8.155

So the goal with this

PS C:\> $j.ChildJobs | ?{$_.State -eq 'Failed'} | %{$s.IPAddress -eq $_.Location}

is to get the output like this

Server101   192.1.8.149
Server104   192.1.8.152
Server107   192.1.8.155
6
  • 1
    What does the formatting of the output matter? Commented Jul 25, 2017 at 22:21
  • $s.IPAddress -eq $_.Location -> $l=$_.Location;$s |?{$.IPAddress -eq $l} Commented Jul 25, 2017 at 23:11
  • So I can identify the Name of the IPAddress that failed, its easier for what I trying to achieve Commented Jul 25, 2017 at 23:15
  • can you paste the complete command with your suggestion? Commented Jul 25, 2017 at 23:16
  • Ok got it, thank you Commented Jul 25, 2017 at 23:34

1 Answer 1

1

An efficient solution requires fast lookups of your CSV-imported objects by IP address.

Therefore, I recommend converting the imported objects to a hashtable keyed by IP address first:

# Convert the CSV-imported custom objects to a hashtable that maps
# IP addresses to names.
$ht = @{}
$s | % { $ht[$_.IPAddress] = $_.Name }

# Process the jobs and look up the name corresponding to a job's
# IP address in the hashtable, using Select-Object with calculated properties.
$j.ChildJobs | ? State -eq 'Failed' |
  Select-Object @{ l='Name'; e={ $ht[$_.Location] } }, @{ l='IPAddress'; e='Location' }
Sign up to request clarification or add additional context in comments.

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.