将所有ADUsers包含在CSV文件中 [英] Wrapping all ADUsers in a CSV File

查看:302
本文介绍了将所有ADUsers包含在CSV文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在CSV档案中撷取所有AD使用者。

I want to retrieve, in a CSV file, all AD users. But every time I run my script the CSV file has a different amount of users.

我知道有多达4000个用户...但有时检索到500到600个结果。

I know there are up to 4000 users... but retrieved sometimes 500 to 600 results.

我在CSV文件中注意到,在最后一行像

I noticed in my CSV file, at the last row something like

"Person Name", "person.name@email.com","person.name","CN=somewhere,OU=USERS,OU=THERE,OU=HERE,OU=SOMEPLACE,OU=ORG,DC= (here is the part where it breaks)



我注意到,总是在最后一行,结果是中断,我的CSV文件有限制吗?

I noticed, always in the final row, the result is break. Is there a limit to my CSV file?

我不知道发生了什么。

$path = ".\Users.csv"

$root = [adsi]''
$searcher = New-Object System.DirectoryServices.DirectorySearcher($root)
$searcher.Filter = "(&(objectClass=user))"
$searcher.PageSize = 5000 #iknow the max is 1000, but when i do it, and count my result, its show up 4000+
$searcher.SizeLimit = 5000


$cols = "cn", "mail", "samaccountname", "distinguishedname"
foreach ($i in $cols) {$searcher.PropertiesToLoad.Add($i)}

$users = $searcher.FindAll() |
  Select-Object @{e={$_.properties.cn};n='DisplayName'},
                @{e={$_.properties.mail};n='Email'},
                @{e={$_.properties.samaccountname};n='sAMAccountName'},
                @{e={$_.properties.distinguishedname};n='distinguishedname'}

$users | Export-Csv $path -Delimiter ";" -Encoding Default #now delimiting using ";" to do have problems with my string with commas


推荐答案

ActiveDirectory模块cmdlet。这么容易。看起来像这样:

Use the ActiveDirectory module cmdlets. So much easier. Looks something like this:

$path = ".\Users.csv"
Get-ADUser -Filter * | 
  Select-Object cn, mail, samaccountname, distinguishedname |
  Export-Csv -Path $Path

根据您的PowerShell版本,您可能需要手动导入模块。

Depending on your version of PowerShell, you may need to manually import the module.

Import-Module ActiveDirectory

这篇关于将所有ADUsers包含在CSV文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆