gpt4 book ai didi

powershell - 收集 samaccount powershell

转载 作者:行者123 更新时间:2023-12-02 22:33:27 26 4
gpt4 key购买 nike

我有一个 CSV 格式的用户列表,但我需要按广告中的名称从每个用户收集 SamAccount 属性。

CSV 模型

enter image description here

脚本

Get-ADObject -Filter 'ObjectClass -eq "user" -and userAccountControl -eq "512"' -Properties * | Select-Object SamAccountName,CN,DisplayName, | Export-CSV -Path C:\Temp\UserAccounts.csv -Encoding UTF8 -NoTypeInformation

我有点迷茫我不知道如何使用名称做一个foreach

我正在尝试但没有成功。

尝试根据 csv 文件上的名称获取 samaccountname。

Import-Csv -Path C:\Temp\userteste.csv | foreach-Object {Get-ADUser -Filter {Name -like $_.name} -Properties Name | Select-Object samAccountName}

并导出到 csv 文件。

最佳答案

为什么要使用 Get-ADObject 而不是 Get-ADUser?后者为您提供了 CSV 中所需的更多所需属性。

另外,如果您只需要一小部分用户属性,那么执行 -Properties * 是一种浪费。

像这样的东西应该可以工作:

Get-ADUser -Filter "Enabled -eq $true" -Properties DisplayName, CN | 
Select-Object SamAccountName, CN, DisplayName |
Export-Csv -Path C:\Temp\UserAccounts.csv -Encoding UTF8 -NoTypeInformation

根据您的评论,您需要获取 CSV 中列出的用户的一些额外属性,您可以这样做:

Import-Csv -Path C:\Temp\userteste.csv | ForEach-Object {
Get-ADUser -Filter "Name -like '$($_.Name)'" -Properties DisplayName, CN |
Select-Object SamAccountName, CN, DisplayName
} | Export-Csv -Path C:\Temp\UserAccounts.csv -Encoding UTF8 -NoTypeInformation

希望对你有帮助

关于powershell - 收集 samaccount powershell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60852779/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com