Foreach 循环中的多个变量 [PowerShell]

发布于 2024-12-10 06:03:49 字数 568 浏览 1 评论 0原文

是否可以将两个变量放入 Foreach 循环中?

以下是针对 PowerShell ASP 的编码。我的 Foreach 循环中的语法不正确,但您应该能够破译我试图构建的逻辑。

$list = Get-QADUser $userid -includeAllProperties | Select-Object -expandproperty name
$userList = Get-QADUser $userid -includeAllProperties | Select-Object -expandproperty LogonName
if ($list.Count -ge 2)
{
    Write-host "Please select the appropriate user.<br>"
    Foreach ($a in $list & $b in $userList)
    {
        Write-host "<a href=default.ps1x?UserID=$b&domain=$domain>$b - $a</a><br>"}
    }
}

Is it possible to pull two variables into a Foreach loop?

The following is coded for the PowerShell ASP. The syntax is incorrect on my Foreach loop, but you should be able to decipher the logic I'm attempting to make.

$list = Get-QADUser $userid -includeAllProperties | Select-Object -expandproperty name
$userList = Get-QADUser $userid -includeAllProperties | Select-Object -expandproperty LogonName
if ($list.Count -ge 2)
{
    Write-host "Please select the appropriate user.<br>"
    Foreach ($a in $list & $b in $userList)
    {
        Write-host "<a href=default.ps1x?UserID=$b&domain=$domain>$b - $a</a><br>"}
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忘你却要生生世世 2024-12-17 06:03:49

克里斯蒂安的回答是你在你的情况下应该做什么。无需获取这两个列表。请记住 PowerShell 中的一件事 - 对对象进行操作直到最后一步。在实际使用它们之前,不要尝试获取它们的属性等。

但是,对于一般情况,当您确实有两个列表并希望在这两个列表上使用 Foreach 时:

您可以自己执行 Foreach 所做的操作:

$a = 1, 2, 3
$b = "one", "two", "three"

$ae = $a.getenumerator()
$be = $b.getenumerator()

while ($ae.MoveNext() -and $be.MoveNext()) {
    Write-Host $ae.current $be.current
}

或者使用普通的 for 与 $a.length 循环,等等。

Christian's answer is what you should do in your situation. There is no need to get the two lists. Remember one thing in PowerShell - operate with the objects till the last step. Don't try to get their properties, etc. until the point where you actually use them.

But, for the general case, when you do have two lists and want to have a Foreach over the two:

You can either do what the Foreach does yourself:

$a = 1, 2, 3
$b = "one", "two", "three"

$ae = $a.getenumerator()
$be = $b.getenumerator()

while ($ae.MoveNext() -and $be.MoveNext()) {
    Write-Host $ae.current $be.current
}

Or use a normal for loop with $a.length, etc.

动次打次papapa 2024-12-17 06:03:49

尝试如下所示。你根本不需要两个变量:

$list = Get-QADUser $userid -includeAllProperties 
if ($list.Count -ge 2)
{
    Write-Host "Please select the appropriate user.<br>"
    Foreach ($a in $list)
    {
        Write-Host "<a href=default.ps1x?UserID=$a.LogonName&domain=$domain>$a.logonname - $a.name</a><br>"
    }  
}

Try like the following. You don't need two variables at all:

$list = Get-QADUser $userid -includeAllProperties 
if ($list.Count -ge 2)
{
    Write-Host "Please select the appropriate user.<br>"
    Foreach ($a in $list)
    {
        Write-Host "<a href=default.ps1x?UserID=$a.LogonName&domain=$domain>$a.logonname - $a.name</a><br>"
    }  
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文