【问题标题】:Read txt line by line and query AD逐行读取txt并查询AD
【发布时间】:2014-03-17 09:36:13
【问题描述】:

我有一个包含 samids 的文本文件:

XXXXXXX
YYYYYYY
ZZZZZZZ

对于每一个我都需要阅读它来查询广告:

dsquery user forestroot -samid XXXXXXX | dsget user -email

并将响应写入另一个文件。请帮帮我:)

【问题讨论】:

    标签: powershell active-directory


    【解决方案1】:

    使用ActiveDirectory PowerShell 模块中的Get-ADUser 而不是 ds 工具:

    Import-Module ActiveDirectory
    
    Get-Content 'C:\path\to\input.txt' |
      Get-ADUser -SearchBase 'DC=example,DC=org' -Property mail |
      select -Expand mail | Out-File 'C:\path\to\output.txt'
    

    其中DC=example,DC=org 是林根域的专有名称 (DN)。

    要以编程方式确定林根域的 DN,您可以使用以下命令:

    ([ADSI]"LDAP://RootDSE").RootDomainNamingContext
    

    【讨论】:

    • 酷,这正是我需要的,抱歉重复的帖子,我认为拆分步骤是必要的。但它只适用于当前域用户,我需要搜索整个域。 @Ansgar Wiechers
    【解决方案2】:

    最后,我设法解决了另一个帖子中提出的这种方式:

    $a = Get-Content .\input.txt 
    for ($i=0 ; $i -lt $a.Length; $i++){
     dsquery user forestroot -samid $a[$i] | dsget user -email | Select-String '@' | select -Expand Line >> output.txt
    }
    

    谢谢

    【讨论】:

      猜你喜欢
      • 2012-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-19
      • 2017-08-29
      • 2011-07-07
      • 1970-01-01
      相关资源
      最近更新 更多