Powershell Foreach where-object -eq value
If I import a csv of computer names $computers that looks like this
Host
----
computer5
Computer20
Computer1
and import another csv of data $data that looks like this
Host OS HD Capacity Owner
---- -- -- ------ -----
Computer20 Windows7 C 80 Becky
Computer1 Windows10 C 80 Tom
Computer1 Windows10 D 100 Tom
computer5 Windows8 C 100 sara
computer5 Windows8 D 1000 sara
computer5 Windows8 E 1000 sara
Im trying to do this
foreach ($pc in $computers){
$hostname = $pc.host
foreach ($entry in $data | where {$enty.host -eq $hostname})
{write-host "$entry.Owner"}
}
The foreach statement isn't finding any matches. How can I fix this? Im trying to do more than just write the owner name but, this is the basic premise
Thanks! Steve
Replace $enty
with $_
inside the Where-Object
filter block:
foreach ($entry in $data |Where-Object {$_.host -eq $hostname}){
Write-Host $entry.Owner
}