powershell Windows“网络组/域”输出筛选器

alen0pnh  于 2023-03-12  发布在  Shell
关注(0)|答案(2)|浏览(138)

我需要抓取特定AD组中的成员并将其添加到数组中。使用net group,我可以轻松获得AD组的成员。但是,我不熟悉Windows上的过滤器。我只想从输出中获得用户名。

Group name     test
Comment

Members

---------------------------------------------------------------------
mike                  tom                      jackie
rick                  jason                    nick
The command completed successfully.

我不能使用PowerShell的Get-ADGroupMember命令。如果有一种方法可以获得数据和过滤器使用PowerShell,它也可以。

muk1a3rh

muk1a3rh1#

好消息是,在PowerShell中很少只有一种方法。下面是我手头上的一个较大脚本的一部分,用于一些与组相关的事情,我并不总是有可用的AD模块(例如在其他团队拥有的服务器上):

$Identity = 'test'
$LDAP = "dc="+$env:USERDNSDOMAIN.Replace('.',',dc=')
$Filter = "(&(sAMAccountName=$Identity)(objectClass=group))"
$Searcher = [adsisearcher]$Filter
$Searcher.SearchRoot = "LDAP://$LDAP"
'Member','Description','groupType' | %{$Searcher.PropertiesToLoad.Add($_)|Out-Null}

$Results=$Searcher.FindAll()

$GroupTypeDef = @{
    1='System'
    2='Global'
    4='Domain Local'
    8='Universal'
    16='APP_BASIC'
    32='APP_QUERY'
    -2147483648='Security'
}

If($Results.Count -gt 0){
    $Group = New-Object PSObject @{
        'DistinguishedName'=[string]$Results.Properties.Item('adspath') -replace "LDAP\:\/\/"
        'Scope'=$GroupTypeDef.Keys|?{$_ -band ($($Results.properties.item('GroupType')))}|%{$GroupTypeDef.get_item($_)}
        'Description'=[string]$Results.Properties.Item('description')
        'Members'=[string[]]$Results.Properties.Item('member')|% -Begin {$Searcher.PropertiesToLoad.Clear();$Searcher.PropertiesToLoad.Add('objectClass')|Out-Null} {$Searcher.Filter = "(distinguishedName=$_)";[PSCustomObject][ordered]@{'MemberType'=$Searcher.FindAll().Properties.Item('objectClass').ToUpper()[-1];'DistinguishedName'=$_}}
    }
    $Group|Select DistinguishedName,Scope,Description
    $Group.Members|FT -AutoSize
}
Else{"Unable to find group '$Group' in '$env:USERDNSDOMAIN'.`nPlease check that you can access that domain from your current domain, and that the group exists."}
dgiusagp

dgiusagp2#

以下是一种不使用AD cmdlet获取AD组直接成员的方法:

param(
  [Parameter(Mandatory)]
  $GroupName
)

$ADS_ESCAPEDMODE_ON   = 2
$ADS_SETTYPE_DN       = 4
$ADS_FORMAT_X500      = 5

function Invoke-Method {
  param(
    [__ComObject]
    $object,

    [String]
    $method,

    $parameters
  )
  $output = $object.GetType().InvokeMember($method,"InvokeMethod",$null,$object,$parameters)
  if ( $output ) { $output }
}

function Set-Property {
  param(
    [__ComObject]
    $object,

    [String]
    $property,

    $parameters
  )
  [Void] $object.GetType().InvokeMember($property,"SetProperty",$null,$object,$parameters)
}

$Pathname = New-Object -ComObject "Pathname"
Set-Property $Pathname "EscapedMode" $ADS_ESCAPEDMODE_ON

$Searcher = [ADSISearcher] "(&(objectClass=group)(name=$GroupName))"
$Searcher.PropertiesToLoad.AddRange(@("distinguishedName"))

$SearchResult = $searcher.FindOne()
if ( $SearchResult ) {
  $GroupDN = $searchResult.Properties["distinguishedname"][0]
  Invoke-Method $Pathname "Set" @($GroupDN,$ADS_SETTYPE_DN)
  $Path = Invoke-Method $Pathname "Retrieve" $ADS_FORMAT_X500
  $Group = [ADSI] $path
  foreach ( $MemberDN in $Group.member ) {
    Invoke-Method $Pathname "Set" @($MemberDN,$ADS_SETTYPE_DN)
    $Path = Invoke-Method $Pathname "Retrieve" $ADS_FORMAT_X500
    $Member = [ADSI] $Path
    "" | Select-Object `
      @{
        Name="group_name"
        Expression={$Group.name[0]}
      },
      @{
        Name="member_objectClass"
        Expression={$member.ObjectClass[$Member.ObjectClass.Count - 1]}
      },
      @{
        Name="member_sAMAccountName";
        Expression={$Member.sAMAccountName[0]}
      }
  }
}
else {
  throw "Group not found"
}

此版本使用Pathname COM对象来处理名称转义,并输出组中每个成员的对象类和sAMAccountName。

相关问题