0

我需要将一些属性从活动目录用户帐户导出到包含显示名称、电子邮件、办公电话、移动、办公室和部门的 xml。我得到了这个 powershell 代码

foreach ($User in (get-AdUser -filter * -SearchBase "OU=Path,DC=Domain,DC=Local" -Property *)){
    $DisplayName = $User.DisplayName
    $EmailAddress = $User.EmailAddress
    $OfficePhone = $User.OfficePhone
    $MobilePhone = $User.MobilePhone
    $Office = $User.Office
    $Department = $User.Department

    $Prop=[ordered]@{
        "DisplayName" = $DisplayName
        'EmailAddress' = $EmailAddress
        'OfficePhone' = $OfficePhone
        'MobilePhone' = $MobilePhone
        'Office' = $Office
        'Department' = $Department
    }
    $obj = New-Object -TypeName PSObject -Property $Prop

    $xml += $obj | Export-Clixml W:\skripts\OutFile.xml
}

但是xml文件中的结果只包含一个用户,最后一个。我不知道我在哪里做错了。

问候尼克拉斯}

4

2 回答 2

0

原始脚本的问题在于您的 foreach 循环中有 Export-Clixml。除此之外,你不应该这样做:

$xml += New-Object -TypeName PSObject -Property $Prop

如果您希望将这些对象存储在数组中以供以后使用,这要快得多:

$xml = 
foreach ($User in (get-AdUser -filter * -SearchBase "OU=Path,DC=Domain,DC=Local" -Property *)){
    $DisplayName = $User.DisplayName
    $EmailAddress = $User.EmailAddress
    $OfficePhone = $User.OfficePhone
    $MobilePhone = $User.MobilePhone
    $Office = $User.Office
    $Department = $User.Department

    $Prop=[ordered]@{
        "DisplayName" = $DisplayName
        'EmailAddress' = $EmailAddress
        'OfficePhone' = $OfficePhone
        'MobilePhone' = $MobilePhone
        'Office' = $Office
        'Department' = $Department
    }
    New-Object -TypeName PSObject -Property $Prop
}

$xml | Export-Clixml W:\skripts\OutFile.xml

如果您只需要导出文件,则根本不需要 $xml 变量。只需将整个 foreach 循环包装在一个子表达式中,然后将其直接通过管道传输到您的导出 cmdlet:

$(foreach ($User in (get-AdUser -filter * -SearchBase "OU=Path,DC=Domain,DC=Local" -Property *)){
    $DisplayName = $User.DisplayName
    $EmailAddress = $User.EmailAddress
    $OfficePhone = $User.OfficePhone
    $MobilePhone = $User.MobilePhone
    $Office = $User.Office
    $Department = $User.Department

    $Prop=[ordered]@{
        "DisplayName" = $DisplayName
        'EmailAddress' = $EmailAddress
        'OfficePhone' = $OfficePhone
        'MobilePhone' = $MobilePhone
        'Office' = $Office
        'Department' = $Department
    }
    New-Object -TypeName PSObject -Property $Prop
}) | Export-Clixml W:\skripts\OutFile.xml

如果您不更改任何属性名称,则可以将整个内容替换为:

$props = 'DisplayName','EmailAddress','OfficePhone','MobilePhone','Office','Department'

get-AdUser -filter * -SearchBase "OU=Path,DC=Domain,DC=Local" -Property $props |
 select $props | Export-Clixml W:\skripts\OutFile.xml
于 2014-01-20T15:54:58.657 回答
0

您正在 for 循环中传递结果,应该这样做:

foreach ($User in (get-AdUser -filter * -SearchBase "OU=Path,DC=Domain,DC=Local" -Property *)){
    $DisplayName = $User.DisplayName
    $EmailAddress = $User.EmailAddress
    $OfficePhone = $User.OfficePhone
    $MobilePhone = $User.MobilePhone
    $Office = $User.Office
    $Department = $User.Department

    $Prop=[ordered]@{
        "DisplayName" = $DisplayName
        'EmailAddress' = $EmailAddress
        'OfficePhone' = $OfficePhone
        'MobilePhone' = $MobilePhone
        'Office' = $Office
        'Department' = $Department
    }
    $xml += New-Object -TypeName PSObject -Property $Prop
}

$xml | Export-Clixml W:\skripts\OutFile.xml
于 2014-01-20T15:24:22.883 回答