gpt4 book ai didi

powershell - 如何使用 Powershell 从电子邮件中获取 samaccountname?

转载 作者:行者123 更新时间:2023-12-03 00:34:31 31 4
gpt4 key购买 nike

我有一个包含一堆电子邮件地址的 .txt 文件,我需要获取相应的登录名并将它们导出到另一个文件中。

我写了一个看起来像这样的脚本:

Import-Module ActiveDirectory

$users = get-content C:\users-input.txt

foreach($users in $users){

Get-ADUser -Filter {EmailAddress -eq "$_"} -properties SamAccountName | Select-Object SamAccountName | Export-CSV -Path "c:\users.csv"

}

由于某种原因,结果我只得到一个空文件。谁能告诉我我做错了什么?

最佳答案

这里有几个问题,

您在 Get-ADUser 命令中使用变量 $_ 作为电子邮件地址,但这并未定义,因为它仅在使用 ForEach 的循环中使用-对象

您在 foreach 中使用相同的变量名称,因此第一个循环会覆盖正在循环的数组。

您正在为每个用户编写 CSV 文件。

试试这个:

Get-Content -Path "c:\users-input.txt" | %{ Get-ADUser -Filter {EmailAddress -eq $_} } | Select-Object SamAccountName | Export-CSV -Path "c:\users.csv"

关于powershell - 如何使用 Powershell 从电子邮件中获取 samaccountname?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47035953/

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