【问题标题】:Mix found values and error msg in csv - Get-ADUser search在 csv 中混合找到的值和错误消息 - Get-ADUser 搜索
【发布时间】:2019-02-27 01:20:10
【问题描述】:

我想获取电子邮件地址的 csv 并查找与这些地址匹配的用户。输出应该是找到的用户信息,或者如果找不到匹配的用户,则将搜索到的电子邮件地址放在一行中,然后是“未找到”

$base_path = "C:\scripts\validate_users\"
$source_file = "input_emails.csv"
$out_file = "results.csv"

#read the file, look them up
$users = Import-csv -Path ($base_path + $source_file) -delimiter ";" | ForEach {

    try {
        Get-ADUser -Filter "EmailAddress -eq '$($_.email)'" -Properties EmailAddress
        }
    catch { 
        "No user for" + '$_.email' 
        }
    }

# Output the resultant collection to a csv file
$users | Export-csv -Path ($base_path + $out_file)

这给了我所有找到的记录并且没有错误消息。

我想避免将 $users 放入一个数组并在那里添加一个值。有没有办法在我现在得到的结果中添加内嵌“searchedforuser@fakedomain.com NOT FOUND”。

输入类似于

joesmith@ourdomain.com
janejones@ourdomain.com
freddielee@ourdomain.com
guywhoquit@ourdomain.com <== won't find this one
realuser@ourdomain.com

现在输出只是找到的四个用户的结果,没有迹象表明“guywhoquit@ourdomain.com”曾经在原始列表中

对不起,如果这是一个新手问题。我是一个ps新手,但是我搜索了很多,如果已经回答了类似的问题,我就错过了。

【问题讨论】:

    标签: powershell export-to-csv


    【解决方案1】:

    由于您使用 Get-AdUser-Filter 参数,它会简单地返回 $null 如果没有找到匹配的用户(假设 @987654324 @ 参数格式正确)-它不会报告错误

    因此,检查Get-ADUser输出 以查看是否找到了用户。 -ov (-OutVariable) 通用参数允许您在变量中捕获 cmdlet 的输出(与其输出行为无关),您可以稍后对其进行检查:

    $base_path = "C:\scripts\validate_users"
    $source_file = "input_emails.csv"
    $out_file = "results.csv"
    
    Import-csv -Path (Join-Path $base_path $source_file) -delimiter ";" | ForEach {
    
      # Get and output the user for the email address at hand;
      # also store the output in variable $user, via `-ov user`    
      Get-ADUser -Filter "EmailAddress -eq '$($_.email)'" -Properties EmailAddress -ov user
    
      if ($user.Count -eq 0) { # User not found?
        # Emit a dummy object with an .EmailAddress property
        # whose value indicates that the user wasn't found.
        # This will show up in the CSV file as a row with all columns
        # except the "EmailAddress" one empty.
        [pscustomobject] @{ EmailAddress = "No user for $($_.email)" }
      }
    
    } | Export-csv -Path (Join-Path $base_path $out_file)
    

    注意:仅将 string "No user for" + '$_.email' 发送到输出流的原因是 Export-Csv 锁定在列中 它输出 基于第一个输入对象

    [string] 实例与 AD 用户对象没有共同的属性,因此您将获得一个 CSV 行没有任何值

    通过使用 .EmailAddress 属性 ([pscustomobject] @{ EmailAddress = "..." }) 构造一个虚拟自定义对象,该属性值显示在文件中(尽管所有其他列值都将为空)。

    【讨论】:

    • 谢谢,@BarrettBuss。
    【解决方案2】:

    您的问题是 Powershell 仅捕获“终止异常”来解决此问题,您可以尝试以下两种修改中的任何一种:

    Get-ADUser -Filter "EmailAddress -eq '$($_.email)'" -Properties EmailAddress -ErrorAction Stop #This will only affect this cmdlet.
    

    或者

    $ErrorActionPreference = 'Stop' #This affects every cmdlet execution you have after this line.
    

    【讨论】:

    • 由于使用了-Filter,因此不会报告(任何类型的)错误 - 没有简单地(安静地)找到匹配项会导致没有输出。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-21
    • 1970-01-01
    • 2019-09-02
    • 2015-03-22
    相关资源
    最近更新 更多