在if语句中的Foreach循环

时间:2015-09-16 12:01:35

标签: powershell active-directory

写了一个小脚本来检查某些AD组是否存在。由于某种原因,它不会遍历给定的数组。它只将数组的第一个值写入控制台。当我设置一个断点时:

someCode() // someComment

我看到$ secGroupNames填充了给定值,有人可以帮助我吗?无法弄清楚这个。

代码:

// someComment
someCode()

输出:

foreach ($item in $SecGroupNames)

1 个答案:

答案 0 :(得分:3)

这是因为return陈述。它导致脚本在第一个循环传递中返回值并结束执行。

如果您想从脚本或函数返回多个值,请使用Write-Output代替return

foreach ($item in $SecGroupNames)
{   
  if (Get-ADGroup $item)
  {     
    Write-Host -ForegroundColor Yellow "$item Exists!"
    Write-Output $true;
  }
  else
  {
    Write-Host -ForegroundColor Green "$item Does not exist, Do something!" 
    Write-Output $false;
  }
}
相关问题