2017-09-15 32 views
1

我無權訪問管理權限,因此我無法安裝AD模塊。從其他域中檢索用戶組而不使用AD CmdLets?

如何在不使用Active Directory的情況下檢索不同域上的用戶用戶組?有任何想法嗎?我有權訪問其他域,但我只能使用此腳本訪問我自己域中的用戶,但不能訪問其他域。

$filedirectory = "C:\Users\x\Desktop\z\Project\test.txt" 
$outputdirectory = "C:\Users\x\Desktop\Project\Export.csv" 
$allusernames = Get-Content $filedirectory 
$groups = "" 

$resultarray [email protected]() 

foreach ($allusernames in $allusernames) { 

$groupObject = new-object PSObject 

$currentusername = $allusernames 
$groups = ([ADSISEARCHER]"samaccountname=$($currentusername)").Findone().Properties.memberof -replace '^CN=([^,]+).+$','$1' | out-string 

$groupObject | add-member -MemberType NoteProperty -name "User" -Value $currentusername 
$groupObject | Add-Member -MemberType NoteProperty -name "Groups" -Value $groups 

$resultarray +=$groupObject 

} 

$resultarray | export-csv -Path $outputdirectory -NoTypeInformation 
+1

與您的經理交談並解釋您需要IT部門將AD Cmdlet添加到您的計算機,以便您可以完成這項工作。 –

回答

0

您可以使用ADSI指定任何域,並建立了ADSIsearcher從它,就像這樣:

$Searcher = New-Object System.DirectoryServices.DirectorySearcher([ADSI]"LDAP://$domain") 

注意,你也可以使用GC://查詢全局編錄和[System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()動態獲取當前的森林及其領域。

因爲您在查詢組成員身份,請注意組成員身份是組的成員屬性,而不是用戶。 memberof屬性僅顯示同一個域的域成員組,如果該組是域本地的。

0
$ForestName = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest().Name 
$root = [ADSI]"GC://$ForestName" 
$Searcher = [ADSISEARCHER]$root 

這就是我用過的。

相關問題