首页 > 解决方案 > 在没有 RSAT 的情况下检查广告组成员资格

问题描述

所以这个脚本应该检查用户的成员资格并配置 odbc 连接,如果他们是组的一部分。这在我的 azure 设备上运行完美,但我安装了广告工具。我想使用 intune 将其推到天蓝色的笔记本电脑上。但它在设备上给了我一个错误:

  Get-ADGroupMember : The term 'Get-ADGroupMember' is not recognized as the name of a cmdlet, 
  function, script file, or operable program. Check the spelling of the name, 
  or if a path was included, verify that the path is correct and try again.
  At line:16 char:20
   +         $members = Get-ADGroupMember -server pitt-drdc-01.univ.pitt.e ...
  +                    ~~~~~~~~~~~~~~~~~
+ CategoryInfo          : ObjectNotFound: (Get-ADGroupMember:String) [], 
 CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

ps 我对此很烂,如果有帮助,这些将在 azure 注册设备上运行。

###########################################################################################
# Createing ODBC Connection to FMTMA-BE02 for Jobs
###########################################################################################
$ChkFile = "C:\Windows\System32\msodbcsql17.dll"
$FileExists = Test-Path $ChkFile


If ($FileExists -eq $True) {
    # Configure Odbc connection for FMTMA-BE02
    
$user = "$env:UserName"
$groups = 'CN=FM-TMASQLUserAccess'

  #verify Group Membership 
foreach ($group in $groups) {

    $members = Get-ADGroupMember -server fsc-server-place.com -Identity $group - 
    Recursive | Select -ExpandProperty SamAccountName

    # If users in in Ad group Create ODBC connection.
    If ($members -contains $user) {
        Write-Host "Adding ODBC Connection FMTMA-BE02"
        Add-OdbcDsn -DriverName "ODBC Driver 17 for SQL Server" -DsnType User -Name fmTMA-BE02 
    -AsJob -SetPropertyValue ("Server=FSC-TMA-BE02.edu", "Trusted_Connection=Yes", 
     "Database=FM")
    }
    Else {
        Write-Host "$user is not a member of $group"
      }
   }
  
}
    else {

    Write-Host "$user is not a member of $group"
}

标签: powershell

解决方案


又快又脏:

$Group = [ADSI]"CN=ODBCGroup,OU=Admin Groups,DC=Domain,DC=com"
if ($userDistinguishedName -match $Group.Member) {
    # do stuff
}

您可以对组成员进行部分匹配,但请记住它们是 distinctNames - 在我的环境中,如果我们尝试匹配$env:username,它会失败。

或者,如果您的组很大和/或您有很多网络延迟,另一种方法是在本地计算机上实际检查用户的组成员身份,而不是解析您从中拖动的所有组成员广告。

您可以破解打开用户的 Kerberos 令牌以列出他们的 AD 组成员资格[System.Security.Principal.WindowsIdentity]::GetCurrent()

然后,您可以将组的 SID 嵌入到脚本中以与令牌的组列表匹配,或者您可以通过检查 ADSI 中的组 SID 来验证它。

$krbToken = [System.Security.Principal.WindowsIdentity]::GetCurrent()
# get group SID - you can use [adsisearcher] with an LDAP query if 
# you don't want to embed the group DN (e.g. for use in multiple domains)
$odbcSID = ([ADSI]("LDAP://CN=ODBCGroup,OU=Admin Groups,DC=Domain,DC=com")).objectSid.value
# convert to string for matching
$strOdbcSID = (New-Object System.Security.Principal.SecurityIdentifier($odbcSID,0)).Value
# $krbToken.Groups is a hashtable with lots and lots of domain and group SIDs
If ($krbToken.Groups.value -match $strOdbcSID) {
    # Do stuff
}

这是一篇很好的文章,它演示了这种通过 Kerberos 令牌检查组 SID 的方法 - 因为我们有一个已知组,所以场景有点简单:https ://activedirectoryfaq.com/2016/08/read-kerberos-token-电源外壳/

值得尝试这两个选项,Measure-Command看看哪个在您的场景中可能更有效。


推荐阅读