在我的文件中添加代码,以指示不存在的用户名没有找到匹配。

huangapple go评论62阅读模式
英文:

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

请注意,代码中的&#39;被翻译为单引号 ('),并且标签 <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 = &#39;NOMATCH&#39; }
    }
}

$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:

&lt;textitem&gt;&lt;tab&gt;&lt;textitem&gt;&lt;tab&gt;&lt;textitem&gt;...
&lt;textitem&gt;&lt;tab&gt;&lt;textitem&gt;&lt;tab&gt;&lt;textitem&gt;...

Excel complains at opening that this is a questionable format, but after positive confirmation it opens and displays as expected.

huangapple
  • 本文由 发表于 2023年2月23日 19:20:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75544130.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定