Reputation: 102
We have this PowerShell script that we use to take out users from the AD. We recently moved to Windows Server 2012R2 from 2008R2 and noticed that the script no longer works. After a few tweaks and turns I have managed to get almost everything to work except this loop:
$mail += $nameofgroup | ForEach-Object {$mail += $_.Name+"<br>"}
This is to be sent via email later therefore we have the $mail
variable.
Here is a code example:
Get info from AD:
$alla = Get-ADUser -filter 'name -like "*"' `
-Properties extensionattribute2, extensionattribute14, extensionattribute15 | `
Select-Object Name, Extensionattribute2, `
Extensionattribute14, Extensionattribute15
Sort the data:
$nameofgroup = $alla | where { `
$.Extensionattribute14 -like "nameofgroup" -and `
$.Extensionattribute15 -like "FULL"} | `
Select-Object Name | Sort-Object Name
Post total amount of users:
$mail += "nameofgroup: " + ($nameofgroup).count + "<br>"
and then my line from before, I want this to list the users by "name"
$mail += $nameofgroup | ForEach-Object {$mail += $_.Name+"<br>"}
Can anyone spot a fault in this last line?
Update:
I just tried to Write-Host $nameofgroup
to see if contains data and it does, so the problem is more or less why it won't print it to $mail
.
Upvotes: 0
Views: 321
Reputation: 200233
You append to $mail
both inside and outside the loop. Decide on one or the other:
$mail += $nameofgroup | ForEach-Object { $_.Name+"<br>" }
or
$nameofgroup | ForEach-Object { $mail += $_.Name+"<br>" }
Upvotes: 3