英文:
Add code to indicate no match found for usernames in my file that do not exist
问题
这个PowerShell脚本会从一个文件中获取用户名列表,并将选定的属性导出到一个CSV文件。它工作得很好,但我想要帮助来包括文本文档中的原始输入,以及我不确定如何添加代码来将"NOMATCH"输出到CSV中,以表示文件中不存在的用户名。非常感谢任何帮助!
Import-Module ActiveDirectory
$users = ForEach ($user in $(Get-Content Usernames.txt)) {
$adUser = Get-AdUser $user -Properties Name,displayname,Samaccountname,Surname,Mail,telephoneNumber,Department,Office,DistinguishedName,Enabled,Modified
if ($adUser) {
$adUser
} else {
[PSCustomObject]@{
Name = "NOMATCH"
displayname = "NOMATCH"
SamAccountName = "NOMATCH"
Surname = "NOMATCH"
Mail = "NOMATCH"
telephoneNumber = "NOMATCH"
Department = "NOMATCH"
Office = "NOMATCH"
DistinguishedName = "NOMATCH"
Enabled = "NOMATCH"
Modified = "NOMATCH"
}
}
}
$users |
Select-Object Name,displayname,SamAccountName,Surname,Mail,telephoneNumber,Department,Office,DistinguishedName,Enabled,Modified |
Export-CSV Username_To_DisplayName.csv -NoTypeInformation
上述修改的脚本会在用户名不存在时输出"NOMATCH",以表示没有匹配的用户。
英文:
This PowerShell script will take a list of usernames from a file and export selected properties to a csv. It works great but I would like help to include the raw input from the text document, and I'm not sure how to add code to output NOMATCH into the csv for the usernames in my file that do not exist. Any help is greatly appreciated!
Import-Module ActiveDirectory
$users = ForEach ($user in $(Get-Content Usernames.txt)) {
Get-AdUser $user -Properties Name,displayname,Samaccountname,Surname,Mail,telephoneNumber,Department,Office,DistinguishedName,Enabled,Modified
}
$users |
Select-Object Name,displayname,SamAccountName,Surname,Mail,telephoneNumber,Department,Office,DistinguishedName,mail,Enabled,Modified |
Export-CSV Username_To_DisplayName.csv -NoTypeInformation
答案1
得分: 1
你需要在找不到用户时捕获错误。
尝试这样做:
Import-Module ActiveDirectory
$users = ForEach ($user in $(Get-Content Usernames.txt)) {
Try {
Get-AdUser $user -Properties Name,displayname,Samaccountname,Surname,Mail,telephoneNumber,Department,Office,DistinguishedName,Enabled,Modified
}
catch {
"$($user) 未找到"
}
}
$users |
Select-Object Name,displayname,SamAccountName,Surname,Mail,telephoneNumber,Department,Office,DistinguishedName,mail,Enabled,Modified |
Export-CSV Username_To_DisplayName.csv -NoTypeInformation
英文:
you need to catch error when user not found.
try this :
Import-Module ActiveDirectory
$users = ForEach ($user in $(Get-Content Usernames.txt)) {
Try{
Get-AdUser $user -Properties Name,displayname,Samaccountname,Surname,Mail,telephoneNumber,Department,Office,DistinguishedName,Enabled,Modified
}
catch {
"$($user) not found"
}
}
$users |
Select-Object Name,displayname,SamAccountName,Surname,Mail,telephoneNumber,Department,Office,DistinguishedName,mail,Enabled,Modified |
Export-CSV Username_To_DisplayName.csv -NoTypeInformation
答案2
得分: 1
以下是代码的翻译部分:
Import-Module ActiveDirectory
$users = ForEach ($user in $(Get-Content Usernames.txt)) {
try
{
Get-AdUser $user -Properties Name,displayname,Samaccountname,Surname,Mail,telephoneNumber,Department,Office,DistinguishedName,Enabled,Modified
}
catch
{
[PSCustomObject]@{ Name = $user; displayname = 'NOMATCH' }
}
}
$users |
Select-Object Name,displayname,SamAccountName,Surname,Mail,telephoneNumber,Department,Office,DistinguishedName,Enabled,Modified |
Export-CSV Username_To_DisplayName.csv -NoTypeInformation
请注意,代码中的'
被翻译为单引号 ('
),并且标签 <textitem>
和 <tab>
未被翻译,因为它们是代码中的标记和分隔符,无需翻译。
英文:
Import-Module ActiveDirectory
$users = ForEach ($user in $(Get-Content Usernames.txt)) {
try
{
Get-AdUser $user -Properties Name,displayname,Samaccountname,Surname,Mail,telephoneNumber,Department,Office,DistinguishedName,Enabled,Modified
}
catch
{
[PSCustomObject]@{ Name = $user; displayname = 'NOMATCH' }
}
}
$users |
Select-Object Name,displayname,SamAccountName,Surname,Mail,telephoneNumber,Department,Office,DistinguishedName,Enabled,Modified |
Export-CSV Username_To_DisplayName.csv -NoTypeInformation
There is a bug in your last statement, you reference 'mail' twice.
I personally execute such kind of task quite frequently, but take a slightly different approach. I don't use Export-CSV, but write my target file - which is technically just plain text - myself. This gives a much better flexibility adding exceptional content. Since CSV is somewhat cumbersome with respect to handling blanks and separator-chars within the text items I write a plain text file name.xls using a line format:
<textitem><tab><textitem><tab><textitem>...
<textitem><tab><textitem><tab><textitem>...
Excel complains at opening that this is a questionable format, but after positive confirmation it opens and displays as expected.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论