如何使用嵌套的ForEach-Object

问题描述 投票:0回答:1

如何使用嵌套的 ForEach-Object? $_ 返回“HKCU:..”,但我想访问第二个 ForEach-Object 中的“Microsoft Teams”字符串。

@(
    "Microsoft Teams",
    "Microsoft Teams Meeting Add-in for Microsoft Office",
    "Teams Machine-Wide Installer"
) | ForEach-Object {
    "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*",
    "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*",
    "HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*" | ForEach-Object {
        Write-Host $_
    }
}
powershell foreach nested-loops
1个回答
0
投票

第一个

ForEach-Object
的$_需要保存到变量中。然后,在第二个
ForEach-Object
内部使用。

@(
    "Microsoft Teams",
    "Microsoft Teams Meeting Add-in for Microsoft Office",
    "Teams Machine-Wide Installer"
) |
ForEach-Object {
    $FirstItem = $_
    "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*",
    "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*",
    "HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*" |
        ForEach-Object {
            Write-Host $_
            Write-Host $FirstItem
        }
}
© www.soinside.com 2019 - 2024. All rights reserved.