Powershell 在csv中混合找到值和错误消息-获取ADUser搜索
我希望获取电子邮件地址的csv,并查找与这些地址匹配的用户。输出应该是已找到的用户信息,或者如果未找到匹配的用户,则输出一行,该行显示搜索到的电子邮件地址,然后显示“未找到” 这会给我所有找到的记录,没有错误消息 我希望避免将$users放入一个数组并在其中添加值。是否有一种方法可以添加内嵌“searchedforuser@fakedomain.com“未找到”与我现在得到的结果一致 输入是沿着Powershell 在csv中混合找到值和错误消息-获取ADUser搜索,powershell,export-to-csv,Powershell,Export To Csv,我希望获取电子邮件地址的csv,并查找与这些地址匹配的用户。输出应该是已找到的用户信息,或者如果未找到匹配的用户,则输出一行,该行显示搜索到的电子邮件地址,然后显示“未找到” 这会给我所有找到的记录,没有错误消息 我希望避免将$users放入一个数组并在其中添加值。是否有一种方法可以添加内嵌“searchedforuser@fakedomain.com“未找到”与我现在得到的结果一致 输入是沿着 joesmith@ourdomain.com janejones@ourdomain.com fre
joesmith@ourdomain.com
janejones@ourdomain.com
freddielee@ourdomain.com
guywhoquit@ourdomain.com <== won't find this one
realuser@ourdomain.com
joesmith@ourdomain.com
janejones@ourdomain.com
freddielee@ourdomain.com
guywhoquit@ourdomain.com您的问题是Powershell只捕获“终止异常”来解决此问题。您可以尝试以下两种修改之一:
Get-ADUser -Filter "EmailAddress -eq '$($_.email)'" -Properties EmailAddress -ErrorAction Stop #This will only affect this cmdlet.
或
您的问题是Powershell只捕获“终止异常”来解决此问题。您可以尝试以下两种修改之一:
Get-ADUser -Filter "EmailAddress -eq '$($_.email)'" -Properties EmailAddress -ErrorAction Stop #This will only affect this cmdlet.
或
由于将Get AdUser
与-Filter
参数一起使用,如果没有找到匹配的用户(假设-Filter
参数格式正确),它将简单地返回$null
,而不会报告错误
因此,检查获取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)
注意:仅向输出流发送“+”$\u0.email“
的字符串“No user”是不够的,因为导出Csv
会根据第一个输入对象锁定它输出的列
[string]
实例与AD users对象没有共同的属性,因此您将获得一个没有任何值的CSV行
通过使用.EmailAddress
属性([pscustomobject]@{EmailAddress=“…”}
)构造一个虚拟自定义对象,该属性值将显示在文件中(尽管所有其他列值都将为空)。因为您正在使用Get AdUser
和-Filter
参数,如果没有找到匹配的用户,它将返回$null
(假设-Filter
参数格式正确),它不会报告错误
因此,检查获取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)
注意:仅向输出流发送“+”$\u0.email“
的字符串“No user”是不够的,因为导出Csv
会根据第一个输入对象锁定它输出的列
[string]
实例与AD users对象没有共同的属性,因此您将获得一个没有任何值的CSV行
通过使用.EmailAddress
属性([pscustomobject]@{EmailAddress=“…”}
)构造虚拟自定义对象,该属性值将显示在文件中(尽管所有其他列值都将为空)。由于使用了-Filter
,因此不会出现任何类型的错误将被报告-不简单地(安静地)查找匹配将导致没有输出。由于使用了-Filter
,因此不会报告任何类型的错误-不简单地(安静地)查找匹配将导致没有输出。谢谢,@BarrettBuss.Thank,@BarrettBuss。