2017-04-19 76 views
1

我與我的腳本中提取我的活動目錄CSV導出腳本對Active Directory用戶

Import-Module ActiveDirectory 
Get-ADUser -Filter * -properties * | Select SAMaccountname, givenname, surname, DistinguishedName | Export-Csv c: \ users \ administrator \ desktop \ users.csv -notypeinformation -Encoding UTF8 

的用戶在這個劇本我使用它返回的CN,該OU和DC中的distinguishedName命令。我的目標是在創建.csv時僅保留第一個OU並刪除其餘部分。的腳本返回什麼

示例:我想要什麼

"SAMaccountname","givenname","surname","DistinguishedName" 
"Jean-Yves.R","Jean-Yves","Raymond","CN=Jean-Yves Raymond,OU=Communication,OU=Direction Générale,OU=Elan & Co,OU=Domain Controllers,DC=ELAN-G1,DC=local" 

例子:

"SAMaccountname","givenname","surname","DistinguishedName" 
"Jean-Yves.R","Jean-Yves","Raymond","Communication" 

TY :)

+0

使用濾波器參數,並指定要查詢(通信)的OU。 – DragonZero

+0

...或者只是拆分DN屬性? – Clijsters

+0

你有一個例子可以給我提供過濾器嗎? ty ^^ – Chrys

回答

1

這會給你想要的OU財產。

Get-ADUser -Filter * -properties * | Select SAMaccountname, givenname, surname,@{Name='OU';Expression={$($_.DistinguishedName).Split(",")[1].Replace("OU=","")}} 

編輯:

爲了應對用戶OU:

Get-ADUser -Filter * -properties * | Select SAMaccountname, givenname, surname,@{Name='OU';Expression={$($_.DistinguishedName).Split(",")[1].Replace("OU=","").Replace("CN=","")}} 
+0

太棒了!如果用戶在OU創建中,它可以完美工作,但如果用戶只是在「用戶」中,它會向我顯示:「Juny.R」,「Juny」,「Rab」,「CN = Users」應該刪除它「 CN =「 – Chrys

+0

@Chrys在這種情況下,只需添加另一個替換。你可以將它們鏈接在一起。 – Matt

+0

@Chrys查看編輯。 –

0
Get-ADUser -Filter * -properties * | Select SAMaccountname, givenname, surname,@{l='DistinguishedName';e={([adsi]"LDAP://$($_.DistinguishedName)").Parent}} 

它使用Select語句中計算屬性通過LDAP獲取DistinguishedName的Parent屬性。

+0

Hello :)它回報我:「Jean-Yves.R」,「Jean-Yves」,「Raymond」,「LDAP:// OU =通信,OU =方向Générale,OU = Elan& Co,OU =域控制器,DC = ELAN-G1,DC =本地「 – Chrys

+0

您確定您完全按照上述方式複製它嗎? –

相關問題