2017-10-26 9 views
0

特定のADグループのメンバーを掴んでアレイに追加する必要があります。 net groupを使って、私は簡単にADグループのメンバーを得ることができます。 しかし、私はWindows上のフィルタには心配していません。私はちょうど出力からユーザー名を取得したい。Windows "net group/domain"出力フィルタ

 
Group name  test 
Comment 

Members 

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

PowerShellを使用してGet-ADGroupMemberコマンドを使用することはできません。 PowerShellを使用してデータとフィルタを取得する方法があれば、それもOKです。

+1

'net group'コマンドから正規表現を使用してユーザ名を取得する必要があります。 'get-adgroupmember'コマンドレットだけの場合、使用できません。常にQuestコマンドレットを試すことができます。 – Itchydon

+3

*' Get-ADGroupMember'を使用できません。なぜですか? –

+0

@AnsgarWiechersシステムはITチームによって所有されているため、私はそこに広告モジュールをインストールすることはできません。 ITチームを扱ってADモジュールをインストールする方が簡単だと思われます。 – Mike

答えて

1

うわー、よく知られていることは、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."} 
0

ここで取得するための一つの方法ですADコマンドレットを使用せずにADグループの直接のメンバー:

param(
    [parameter(Mandatory=$true)] 
    $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" 
} 

このバージョンでは、エスケープ名を処理するために、パス名COMオブジェクトを使用し、グループの各メンバーのためのオブジェクトクラスとのsAMAccountNameを出力します。

関連する問題