Several variables in the Foreach loop [PowerShell]

Is it possible to drag two variables into the Foreach loop?

For PowerShell ASP, the following is encoded. The syntax is incorrect in my Foreach loop, but you should be able to decrypt the logic I'm trying to do.

$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>"} } } 
+4
source share
2 answers

Try the following. You do not 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>" } } 
+6
source

The Christian answer is what you should do in your situation. There is no need to get two lists. Remember one thing in PowerShell - work with objects until the last step. Do not try to get their properties, etc. Until you use them.

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

You can do what Foreach does:

 $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 regular for loop with $a.length , etc.

+10
source

All Articles