iT邦幫忙

0

使用powershell匯出AD組織下的群組資訊

https://ithelp.ithome.com.tw/upload/images/20210701/20095804o5lyTn183l.png

https://ithelp.ithome.com.tw/upload/images/20210701/20095804WGt7scxUXs.png

能否用powershell指令匯出CSV,
在OU下的所有群組的Email資訊及顯示名稱和群組內的成員
(如上圖方框資訊)

謝謝

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

1
runan5678
iT邦研究生 1 級 ‧ 2021-07-01 17:04:15
最佳解答

powershell get-adgroup那個email欄位抓不出來,所以我用ldap query方式處理

$Filter = "(objectclass=group)"
$RootOU = "DC=dc,DC=local"

$Searcher = New-Object DirectoryServices.DirectorySearcher
$Searcher.SearchRoot = New-Object System.DirectoryServices.DirectoryEntry("LDAP://$($RootOU)")
$Searcher.Filter = $Filter
$Searcher.SearchScope = 1 # Either: "0=Base", "1=OneLevel" or "2=Subtree"
$a = $Searcher.FindAll()

然後這個$a可以用陣列的方式讀取裡面的值

 for ($i=0 ; $i -le $a.Count; $i++)
{
    $a[$i].Properties.name    #群組名稱 這邊用sAMAccountName也可以
    $a[$i].Properties.mail    #mail
    
}

然後抓群組裡面的member就直接用get-adgroupmember即可
由於上面的型態($a)不是string,用substring作轉換後就可以給get-adgroupmember
substring裡面的參數要自己試試看哪一個是需要的值

$a[i].Properties.name.substring(0)

CSV就兜一兜應該就行了

0
echochio
iT邦高手 1 級 ‧ 2021-07-01 14:01:11

https://docs.microsoft.com/en-us/previous-versions/system-center/service-manager-2010-sp1/gg232586(v=technet.10)?redirectedfrom=MSDN

這個不知是不是你要的

這邊選 displayname,mail
需要的自己加看看出現的是否符合所需

Import-Module ActiveDirectory
Get-ADUser -Filter * -SearchBase "OU=xxx,DC=domain,DC=local" -properties  displayname,mail | select displayname,mail | export-csv -path c:\test.csv -NoTypeInformation -Encoding "UTF8"

當然直接用 * 就全出來了

Import-Module ActiveDirectory
Get-ADUser -Filter * -Properties * | export-csv -path c:\ADusers.csv -NoTypeInformation -Encoding "UTF8"

我要發表回答

立即登入回答