powershell - 将所有 ADUsers 封装在 CSV 文件中

标签 powershell csv active-directory

我想在 CSV 文件中检索所有 AD 用户。但每次我运行脚本时,CSV 文件都有不同数量的用户。

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

我注意到在我的 CSV 文件中,最后一行类似

"Person Name", "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="95e5f0e7e6fafbbbfbf4f8f0d5f0f8f4fcf9bbf6faf8" rel="noreferrer noopener nofollow">[email protected]</a>","person.name","CN=somewhere,OU=USERS,OU=THERE,OU=HERE,OU=SOMEPLACE,OU=ORG,DC= (here is the part where it breaks)

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

我不知道发生了什么。

$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。容易多了。看起来像这样:

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

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

Import-Module ActiveDirectory

关于powershell - 将所有 ADUsers 封装在 CSV 文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31415233/

相关文章:

ruby-on-rails-3 - 通过回形针4附加时在S3中设置内容类型?

javascript - 使用 jquery 中的 data.split 函数从 csv 文件返回特定行

windows - Outlook 联系人导出中的 CSV header 是什么?

powershell - Hyper-V脚本无法识别远程虚拟机

java - 更改密码时如何在 Java 客户端中收到 Active Directory 密码策略错误

c# - 如何在 ASP.NET 中使用 AD 身份验证?

windows - 事件目录自定义身份验证

arrays - PowerShell:两个以上数组的交集

regex - Powershell - 拆分字符串

powershell - 安装 TFS PowerShell Cmdlet