Powershell ForEach-Object循环与扭曲
我们有这个PowerShell脚本,我们用它从AD中取出用户。我们最近从2008R2迁移到Windows Server 2012R2,并注意到该脚本不再有效。经过几次调整并最终我设法得到几乎一切,除了这个循环的工作:Powershell ForEach-Object循环与扭曲
$mail += $nameofgroup | ForEach-Object {$mail += $_.Name+"<br>"}
这是可以通过电子邮件后发送,因此我们有$mail
变量。
下面是一个代码示例:
$alla = Get-ADUser -filter 'name -like "*"' `
-Properties extensionattribute2, extensionattribute14, extensionattribute15 | `
Select-Object Name, Extensionattribute2, `
Extensionattribute14, Extensionattribute15
排序数据:
$nameofgroup = $alla | where { `
$.Extensionattribute14 -like "nameofgroup" -and `
$.Extensionattribute15 -like "FULL"} | `
Select-Object Name | Sort-Object Name
发布用户的总量:
$mail += "nameofgroup: " + ($nameofgroup).count + "<br>"
从AD获取信息然后我的路线从之前,我希望按“名称”列出用户
$mail += $nameofgroup | ForEach-Object {$mail += $_.Name+"<br>"}
任何人都可以在最后一行发现错误吗?
更新:
我只是试图Write-Host $nameofgroup
,看是否包含数据和它,所以这个问题或多或少为什么它不会将其打印$mail
。
您可以在循环内部和外部追加到$mail
。决定一个或另一个:
$mail += $nameofgroup | ForEach-Object { $_.Name+"<br>" }
或
$nameofgroup | ForEach-Object { $mail += $_.Name+"<br>" }
你的第二个例子正是我在我的代码中使用的。 –
工作,如果我这样做$ nameofgroup | ForEach-Object {$ mail + = $ _。Name +“
”} –
_I设法让几乎所有的工作,除了这loop_这是什么意思?你有任何错误? –
不,没有错误,只是当我稍后将它打印到邮件时,它只是不会输出任何数据。 –
定义“无法工作”。请提供您期望循环产生的实例以及实际产生的实例。 –