Powershell Foreach where-object -eq 值
Powershell Foreach where-object -eq value
如果我导入计算机名称 $computers 的 csv,如下所示
Host
----
computer5
Computer20
Computer1
并导入另一个 csv 数据 $data,如下所示
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
我正在尝试这样做
foreach ($pc in $computers){
$hostname = $pc.host
foreach ($entry in $data | where {$enty.host -eq $hostname})
{write-host "$entry.Owner"}
}
foreach 语句未找到任何匹配项。我怎样才能解决这个问题?
我想做的不仅仅是写所有者的名字,但是,这是基本前提
谢谢!
史蒂夫
在 Where-Object
过滤器块内用 $_
替换 $enty
:
foreach ($entry in $data |Where-Object {$_.host -eq $hostname}){
Write-Host $entry.Owner
}
这是另一种方法,使用 -eq 运算符左侧的数组 $computers.host
:
$computers = 'Host
computer5
Computer20
Computer1' | convertfrom-csv
$data = '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' | convertfrom-csv
$data | where { $computers.host -eq $_.host } | ft
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
或
$data | where host -in $computers.host
或
foreach ( $entry in $data | where host -in $computers.host | foreach owner ) {
$entry }
Becky
Tom
Tom
sara
sara
sara
假设 $entry 拼写正确,这个语法恰好不起作用。
表达式 $data | where {$entry.host -eq $hostname}
本身没有意义。不过是个好问题。您确实可以在 foreach 之后的括号中使用管道。
foreach ($pc in $computers){
$hostname = $pc.host
foreach ($entry in $data | where {write-host "entry is $entry";
$entry.host -eq $hostname}){
write-host "$entry.Owner"
}
}
entry is
entry is
entry is
entry is
entry is
entry is
entry is
entry is
entry is
entry is
entry is
entry is
entry is
entry is
entry is
entry is
entry is
entry is
如果我导入计算机名称 $computers 的 csv,如下所示
Host
----
computer5
Computer20
Computer1
并导入另一个 csv 数据 $data,如下所示
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
我正在尝试这样做
foreach ($pc in $computers){
$hostname = $pc.host
foreach ($entry in $data | where {$enty.host -eq $hostname})
{write-host "$entry.Owner"}
}
foreach 语句未找到任何匹配项。我怎样才能解决这个问题? 我想做的不仅仅是写所有者的名字,但是,这是基本前提
谢谢! 史蒂夫
在 Where-Object
过滤器块内用 $_
替换 $enty
:
foreach ($entry in $data |Where-Object {$_.host -eq $hostname}){
Write-Host $entry.Owner
}
这是另一种方法,使用 -eq 运算符左侧的数组 $computers.host
:
$computers = 'Host
computer5
Computer20
Computer1' | convertfrom-csv
$data = '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' | convertfrom-csv
$data | where { $computers.host -eq $_.host } | ft
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
或
$data | where host -in $computers.host
或
foreach ( $entry in $data | where host -in $computers.host | foreach owner ) {
$entry }
Becky
Tom
Tom
sara
sara
sara
假设 $entry 拼写正确,这个语法恰好不起作用。
表达式 $data | where {$entry.host -eq $hostname}
本身没有意义。不过是个好问题。您确实可以在 foreach 之后的括号中使用管道。
foreach ($pc in $computers){
$hostname = $pc.host
foreach ($entry in $data | where {write-host "entry is $entry";
$entry.host -eq $hostname}){
write-host "$entry.Owner"
}
}
entry is
entry is
entry is
entry is
entry is
entry is
entry is
entry is
entry is
entry is
entry is
entry is
entry is
entry is
entry is
entry is
entry is
entry is