Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
powershell如何为循环使用管道_Powershell - Fatal编程技术网

powershell如何为循环使用管道

powershell如何为循环使用管道,powershell,Powershell,我正在尝试用这样的for循环更新数组 $comps = Get-ChildItem $LocalPath -recurse | Select-String -pattern "MyStringFilter" | group path | select name | Foreach {"$($_.Name)".substring(0,"$($_.Name)".IndexOf('.')).substring("$($_.Name)".LastIndexOf('\')+1) } for ($i = 0

我正在尝试用这样的for循环更新数组

$comps = Get-ChildItem $LocalPath -recurse | Select-String -pattern "MyStringFilter" | group path | select name | Foreach {"$($_.Name)".substring(0,"$($_.Name)".IndexOf('.')).substring("$($_.Name)".LastIndexOf('\')+1) }
for ($i = 0; $i -lt $comps.count; $i++) {
  if ($comps[$i].substring(0,3) -eq "AAA") {$comps[$i] = "/aaa "+$comps[$i]}  else {$comps[$i] = "/bbb "+$comps[$i]}
}
$comps 
上面的代码工作正常,但当我尝试使用forEach将其放入管道时,数组变量变为空

$comps = Get-ChildItem $LocalPath -recurse | Select-String -pattern "MyStringFilter" | group path | select name | Foreach {$_.Name.substring(0,$_.Name.IndexOf('.')).substring($_.Name.LastIndexOf('\')+1) } | foreach { if ($_.substring(0,3) -eq "AAA" ) {$_ = "/aaa "+$_} else {$_ = "/bbb "+$_} }
我错过了什么?有什么办法让它工作吗


谢谢,

您不能在每个对象的块中指定迭代值
$\ucode>:

$comps = Get-ChildItem $LocalPath -recurse | Select-String -pattern "MyStringFilter" | group path | select name | Foreach {
    $_.Name.substring(0,$_.Name.IndexOf('.')).substring($_.Name.LastIndexOf('\')+1) 
} | foreach { 
    if ($_.substring(0,3) -eq "AAA" ) {
        "/aaa "+$_
    } else {
        "/bbb "+$_
    } 
}

相反,请按照上述操作,并将要指定的值作为块的返回值。每次迭代的所有返回组成一个数组,当您执行
$comps=

{/aaa“+$\u}否则{/bbb”+$\u}
时,赋值一直发生在该语句的开头。谢谢。