Foreach 循环中的多个变量 [PowerShell]
是否可以将两个变量放入 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
克里斯蒂安的回答是你在你的情况下应该做什么。无需获取这两个列表。请记住 PowerShell 中的一件事 - 对对象进行操作直到最后一步。在实际使用它们之前,不要尝试获取它们的属性等。
但是,对于一般情况,当您确实有两个列表并希望在这两个列表上使用
Foreach
时:您可以自己执行 Foreach 所做的操作:
或者使用普通的
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:
Or use a normal
for
loop with$a.length
, etc.尝试如下所示。你根本不需要两个变量:
Try like the following. You don't need two variables at all: