【问题标题】:Extract list of properties from AD user objects to XML从 AD 用户对象中提取属性列表到 XML
【发布时间】:2014-11-22 01:24:27
【问题描述】:

我正在使用这个 powershell 脚本 https://gallery.technet.microsoft.com/scriptcenter/Extract-arbitrary-list-of-6f59d3b4 从 AD 用户对象中提取属性。

当我运行这个命令时:

.\Get-UserADProperties2.ps1 givenName,sn,department,mail,telephoneNumber -searchroot 'LDAP://ou=users,ou=EMP,dc=emp,dc=company,dc=com' |导出-Clixml C:\XML\ADInfo.xml

xml 文件中的标签如下所示:

  <Obj RefId="1">
    <TNRef RefId="0" />
    <MS>
      <S N="department">Information Systems</S>
      <S N="mail">jane.doe@company.com</S>
      <S N="sn">Doe</S>
    </MS>
  </Obj>

我怎样才能得到出口,使标签看起来像这样:

<Obj RefId="1">
    <TNRef RefId="0" />
    <MS>
      <department>Information Systems</department>
      <mail>jane.doe@company.com</mail>
      <lname>Doe</lname>
    </MS>
  </Obj>

【问题讨论】:

    标签: xml powershell


    【解决方案1】:

    Export-clixml 用于序列化对象,以便以后可以读回。如果你想要漂亮的 html,请使用 convertto-html 之类的东西。 http://technet.microsoft.com/en-us/library/hh849944.aspx

    【讨论】:

    • ConvertTo-Html 创建 HTML,而不是 XML。
    • @AnsgarWiechers 我的错,没有意识到它需要是 xml。如果您想要自己的自定义 xml 标记,则必须手动完成。这篇文章将为您指明正确的方向:stackoverflow.com/questions/19935102/…
    【解决方案2】:

    这样的事情应该做你想做的事:

    [xml]$xml = Get-Content 'C:\XML\ADInfo.xml'
    
    $xml.SelectNodes('//MS/S') | % {
      $node = $xml.CreateElement($_.N)
      $txt  = $xml.CreateTextNode($_.'#text')
      [void]$node.AppendChild($txt)
    
      $_.ParentNode.ReplaceChild($node, $_)
    }
    
    $xml.Save('C:\XML\ADInfo.xml')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-13
      • 2016-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-15
      • 2019-03-17
      相关资源
      最近更新 更多