use variable in get-adobject foreach loop

Welcome Forums General PowerShell Q&A use variable in get-adobject foreach loop

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
1 month, 2 weeks ago.

  • Author
    Posts
  • #116559

    Participant
    Points: 2
    Rank: Member

    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

  • #116589

    Participant
    Points: 275
    Helping Hand
    Rank: Contributor

    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.

  • #119160

    Participant
    Points: 2
    Rank: Member

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

    PowerShell
    7 lines

    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.

    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.

You must be logged in to reply to this topic.