【问题标题】:Is there a way to check AD group membership for a computer?有没有办法检查计算机的 AD 组成员身份?
【发布时间】:2012-06-20 18:40:36
【问题描述】:

我正在尝试通过 Powershell 检查计算机组成员身份。我希望能够指定某个计算机名称并从 Powershell 脚本中找到该计算机所在的组。我打算在计算机上运行脚本,获取主机名,然后打印出该计算机所在的 AD 组。有简单的方法吗?

编辑: 所以这里的计划是让计算机检查它属于哪个组,然后根据它所在的组分配一台打印机。我们有许多打印机,只有 3 到 4 人使用,但由于分散的性质用户不能缩小打印机的数量。我正在查看组策略,但不想创建 20 个不同的 GPO。我想用登录/启动脚本来做到这一点。我不确定这是否可行。

编辑#2: 这个编辑真的晚了,但我想如果有人发现它会有所帮助。我们最终在用户>首选项>控制面板>打印机对象上使用了项目级目标。我们在 AD 中为需要访问打印机的每组用户创建了一个帐户。尽管它确实为计算机的第一次登录创建了一个漫长的登录过程,但它仍然有效。我们还启用了 Point-to-Print 限制,以便从服务器安静地加载驱动程序。

【问题讨论】:

  • 您的环境如何?你有可以运行它的 2008R2 服务器吗?您是否安装了 Quest AD cmdlet?是否有特定原因要在本地机器上运行它而不是简单地从一台机器上查询 AD?
  • 它是 Server 2008 R2,其背后的想法是查看计算机所在的组,然后根据该组分配打印机
  • @user1470158 我建议使用原生 gpo 来完成这项工作:technet.microsoft.com/en-us/windowsserver/bb310732.aspx
  • 如果您根据组成员分配打印机,那么我希望 GPO 也是最佳解决方案。
  • 我也在考虑组策略,但我们有近 20 个不同的打印机组,并且不认为只为打印机设置 20 个组策略对象是值得的。另一个问题是每个组大约有三个用户,因此这些对象不会影响很多人。我对 AD 和 GPO 的整个生产领域有点陌生,不熟悉最佳实践。

标签: windows powershell active-directory active-directory-group


【解决方案1】:

这将为您提供本地计算机的组成员身份(组名)(需要 powershell 2.0):

([adsisearcher]"(&(objectCategory=computer)(cn=$env:COMPUTERNAME))").FindOne().Properties.memberof -replace '^CN=([^,]+).+$','$1'

【讨论】:

  • 我在客户端上试过这个,它什么也没返回。我尝试将其添加到变量中并回显该变量,但它仍然没有返回任何内容。我不确定我错过了什么。
  • 如果你删除 findone() 方法之后的所有内容,会发生什么输出?
  • 仍然没有,虽然我发现使用 'Get-ADComputer "TestClient" -Properties * | Select-Object MemberOf' 让我更接近团体。我觉得我应该改写我的问题来描述我正在尝试的内容。
  • 您可以使用 AD 模块,但这需要您在任何客户端上加载它,它会减慢您的登录时间。 adsisearcher 在 v2 中本机工作,所以我建议你找出为什么如果你要在登录脚本中这样做它不起作用。
  • 我可能已经找到了它为什么不起作用的原因,samaccountname 要求计算机名称末尾有美元符号。也就是说,没有它它对我有用。尝试在计算机名称的末尾添加一个转义的美元符号:(samaccountname=$env:COMPUTERNAME`$)
【解决方案2】:

抱歉,如果我在聚会上迟到了一点,但我还需要找到计算机的组成员身份。经过大量的试验和错误,这对我有用。

Get-ADComputer "mycomp" -Properties MemberOf | %{if ($_.MemberOf -like "*group name*") {Write-Host "found"} }

我注意到如果字符串比较在单独的行上,我需要执行以下操作

$g=Get-ADGroupMember -Identity "CN=myADgroup,OU=myOU,DC=corp,DC=com" -server corp.com
foreach($mbr in $g) {if($name.MemberOf -like "*mycomp*" -eq $true) {Write-Host "found"}}

不确定,但我认为测试计算机可能比测试组更快更容易,具体取决于成员的数量。

【讨论】:

    【解决方案3】:

    gpresult 方法似乎是我能找到准确的唯一方法。查询 AD 不准确,因为计算机可能已添加到组但未重新启动。我相信有人可以浓缩这一点,但它运作良好,足以满足我的需要。

    # Get all active domain servers
    $staledate = (Get-Date).AddDays(-90)
    $computers = Get-ADComputer -Filter {(OperatingSystem -Like "*Server*") -and (Enabled -eq $True) -and (LastLogonDate -ge $staledate) -and (Modified -ge $staledate) -and (PasswordLastSet -ge $staledate) -and (whenChanged -ge $staledate) -and (Name -notlike "PHXVSSA101A")} | Select name -expandproperty name
    $computers = $computers | Where {(Resolve-DNSName $_.name -ea 0) -and (Test-Connection -ComputerName $_.Name -Count 1 -ea 0)} | Sort
    # Loop through all active domain servers
    Foreach ($computer in $computers)
    {
    # Pull the gpresult for the current server
    $Lines = gpresult /s $computer /v /SCOPE COMPUTER
    # Initialize arrays
    $cgroups = @()
    $dgroups = @()
    # Out equals false by default
    $Out = $False
    # Define start and end lines for the section we want
    $start = "The computer is a part of the following security groups"
    $end = "Resultant Set Of Policies for Computer"
    # Loop through the gpresult output looking for the computer security group section
    ForEach ($Line In $Lines)
    {
    If ($Line -match $start) {$Out = $True}
    If ($Out -eq $True) {$cgroups += $Line}
    If ($Line -match $end) {Break}
    }
    # Loop through all the gathered groups and check for Active Directory groups
    ForEach ($group in $cgroups)
    {
    Try {
    $check = Get-ADgroup $group -ErrorAction Stop
    If ($check) {
    $dgroups += $group
    }
    }
    Catch {}
    }
    # Output server name and any Active Directory groups it is in
    $computer
    $dgroups
    # End of computers loop
    }
    

    【讨论】:

      【解决方案4】:

      尝试运行 gpresult /V 并在“安全组”下检查

      您也可以在命令提示符(提升为管理员)下尝试gpresult /scope computer /v 以获得更具体的结果

      【讨论】:

        【解决方案5】:

        这是一个 LDAP 查询,用于递归查找计算机是否在组中:

        (((objectClass=computer)(sAMAccountName=COMPUTERNAME$))(memberof:1.2.840.113556.1.4.1941:=DistinguishedNameOfGroup))
        

        更多信息:http://justanotheritblog.co.uk/2016/01/27/recursively-check-if-a-usercomputer-is-a-member-of-an-ad-group-with-powershell-2-0/

        【讨论】:

          【解决方案6】:

          要检查计算机自己的组成员视图,您可以运行:

          (New-Object System.Security.Principal.WindowsPrincipal("$env:computername$")).IsInRole('Example Group')
          True
          

          将计算机从Example Group中取出不会影响上面的输出,直到计算机重新启动。

          【讨论】:

            【解决方案7】:

            试试这个 DOS 命令,这将返回这台计算机所属的所有本地组:

            net localgroup
            

            【讨论】:

            • 这列出了本地组,而不是计算机帐户的组成员身份。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-11-11
            • 2019-08-17
            • 2016-10-26
            • 1970-01-01
            相关资源
            最近更新 更多