use variable in get-adobject foreach loop

Hello PS Board,

I’m trying to get a list of all AD Security Groups which are used for granting rights on Exchange Resources. I hope there is a smarter solution than mine.
I use
$mbxperm=Get-MailboxPermission * | select user| Out-String

for getting a list of username and passwords, with \Domains, results like
“domain\xyz
domain\xjy
…”

I split the domain and name using,
$mbxperm.User
$mbxperm| gm
$domain,$username = $mbxperm.split('')

in $username I only have the names now. They can be group or userobject so I try to filter them using get-adobject where I fail.

foreach ($i in $username) {
get-adgroup -filter {name -eq $i -and ObjectClass -eq “group”}
}

I even tried to use an hashtable, I don’t get any results from this command. If I assign one value to $i and try a oneliner get-adgroup -filter {name -eq $i -and ObjectClass -eq “group”} I get a proper result. Don’t know how to handle it in foreach. Some resources tell that the ScriptBlock is the issue, but with quotationmarks it’s not running at all.

Best Regards,
baschi

You’ve probably got some additional whitespace from using Out-String. Instead…

$UserList = Get-MailboxPermission * | Select-Object -ExpandProperty User
# Go through each individual entry
foreach ($User in $UserList) {
    #.Trim() removes leading or trailing whitespace, just in case
    $Domain, $Username = $Users.Split('\').Trim()
    Get-ADGroup -Filter { Name -eq $Username -and ObjectClass -eq "Group" }
}

I think your main issue here is that you do the split on the whole array of names, instead of one by one. So rather than ending up with it neatly giving you the domains and the names in each variable, it has the first value in $domain, and everything else in $username; when you split an array like that in PS, it doesn’t do an even split, it assigns one by one until it runs out of variables to assign to, and then it assigns the remaining items to the final variable.

[quote quote=116589]You’ve probably got some additional whitespace from using Out-String. Instead…

PowerShell
7 lines
<textarea class="ace_text-input" style="opacity: 0; height: 18px; width: 7px; left: 44px; top: 0px;" spellcheck="false" wrap="off"></textarea>
1
2
3
4
5
6
7
$UserList = Get-MailboxPermission * | Select-Object -ExpandProperty User
# Go through each individual entry
foreach ($User in $UserList) {
#.Trim() removes leading or trailing whitespace, just in case
$Domain, $Username = $Users.Split('\').Trim()
Get-ADGroup -Filter { Name -eq $Username -and ObjectClass -eq "Group" }
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
I think your main issue here is that you do the split on the whole array of names, instead of one by one. So rather than ending up with it neatly giving you the domains and the names in each variable, it has the first value in $domain, and everything else in $username; when you split an array like that in PS, it doesn't do an even split, it assigns one by one until it runs out of variables to assign to, and then it assigns the remaining items to the final variable.

[/quote]
I accidently posted this post twice as it dissapeared the first time. After several hours and finding the solution myself you are EXACTLY describing my issue. Thanks for that perfect answer If I would discovered this thread earlier I would have saved a few hours thanks to you. Thanks Joel.