Powershell - Get-ADComputer -properties memberof

后端 未结 3 1011
时光取名叫无心
时光取名叫无心 2021-01-25 10:36

I am trying to find if any servers in our enviroment have NOT been applied to a particular group. I have a list of groups that we use to patch our Windows Servers on partiular d

3条回答
  •  无人共我
    2021-01-25 11:16

    Let's work with this:

    $groups = @("Terminal Server License Servers","Exchange Trusted Subsystem","Cert Publishers")
    $regex = '^({0})' -f ($groups -join '|')
    get-adcomputer -Filter {OperatingSystem -like "Windows Server 200*"} -properties * | 
        Where-Object{($_.MemberOf | Get-ADGroup).Name -notmatch $regex} |
        Select-Object Name,OperatingSystem,MemberOf
    

    Take the groups and turn them into an array. Join the array members into a regex string which will match the full names of groups. Move the If statement into a -Filter to return only what you want which would make it more efficient. The MemberOf is a list of DistinguishedNames. Get the just the group name from Get-AdGroup. You could easily use string manipulation to extract the name from the dn. I just find this easier. Havent done anything, beyond a Select-Object, with the results but you could pipe into a ForEach-Object and process accordingly.

提交回复
热议问题