PowerShell ForEach循环中的内联命令执行
我熟悉BASH语法如何处理此请求,但在PowerShell中找不到这样做的方法 BASH示例:PowerShell ForEach循环中的内联命令执行,powershell,Powershell,我熟悉BASH语法如何处理此请求,但在PowerShell中找不到这样做的方法 BASH示例: for x in `ls .`; do something with ${x}; done 我正试图用PowerShell执行相同的操作(出于演示目的,它的语法不正确) …我显然有语法错误 BASH到PowerShell的翻译是这里的问题:D对于您的示例,它只会起作用(没有反勾号): 如果是命令,可以将其包装到子表达式中: foreach ($process in $(Get-Process)) {
for x in `ls .`; do something with ${x}; done
我正试图用PowerShell执行相同的操作(出于演示目的,它的语法不正确)
…我显然有语法错误
BASH到PowerShell的翻译是这里的问题:D对于您的示例,它只会起作用(没有反勾号): 如果是命令,可以将其包装到子表达式中:
foreach ($process in $(Get-Process)) {
# do stuff
}
您还可以查看ForEach对象
cmdlet,它在PowerShell的管道中更为惯用:
Get-Process | ForEach-Object { Write-Verbose "This process is $_" -Verbose }
这可能会帮助你开始
$things = Get-ChildItem C:\Windows\
foreach ($thing in $things) {
# Do a thing with each one in turn
Write-Host $thing.Name -ForegroundColor Magenta
}
Get-Process | ForEach-Object { Write-Verbose "This process is $_" -Verbose }
$things = Get-ChildItem C:\Windows\
foreach ($thing in $things) {
# Do a thing with each one in turn
Write-Host $thing.Name -ForegroundColor Magenta
}