写一个捕获以显示不在广告中的用户名

发布于 2025-01-26 03:13:38 字数 370 浏览 3 评论 0原文

我想编写一条线,让我知道无法找到的所有用户名,以及它们在脚本中删除的.txt文档中的显示名称。

从本质上讲,我只希望一行添加到现有脚本中。

关于如何这样做的任何提示或指导都将是很多措施。

Get-Content C:\Users\User\Desktop\findusername.txt | Foreach-Object {get-aduser -filter "displayName -like '$($_)'" | Select-Object SamAccountName} | Export-Csv -Path C:\Users\User\Documents\ADGroupusernames.csv -NoTypeInformation

I would like to write a line that lets me know any usernames that could not be found and what their display name was within the .txt document that the script is pulling from.

Essentially I just want a line to add onto the existing script.

Any tips or guidance on how to do so would be much appriciated.

Get-Content C:\Users\User\Desktop\findusername.txt | Foreach-Object {get-aduser -filter "displayName -like '$($_)'" | Select-Object SamAccountName} | Export-Csv -Path C:\Users\User\Documents\ADGroupusernames.csv -NoTypeInformation

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨夜星沙 2025-02-02 03:13:38

正如圣地亚哥指出的那样,当找不到匹配对象时,过滤器不会丢失错误。相反,您可以检查是否返回任何对象并对此做出反应。

Get-Content C:\Users\User\Desktop\findusername.txt | 
    ForEach-Object { 
        if ($found = Get-ADUser -Filter "displayName -like '$($_)'") {
            $found | Select-Object SamAccountName
        }
        else {
            [PSCustomObject]@{
                SamAccountName = "Did not find '$_'"
            }
        } 
    } | Export-Csv -Path C:\Users\User\Documents\ADGroupusernames.csv -NoTypeInformation

As Santiago points out, filter does not throw an error when no matching objects are found. You can instead check to see if any objects are returned and react to that.

Get-Content C:\Users\User\Desktop\findusername.txt | 
    ForEach-Object { 
        if ($found = Get-ADUser -Filter "displayName -like '$($_)'") {
            $found | Select-Object SamAccountName
        }
        else {
            [PSCustomObject]@{
                SamAccountName = "Did not find '$_'"
            }
        } 
    } | Export-Csv -Path C:\Users\User\Documents\ADGroupusernames.csv -NoTypeInformation
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文