导出一个包含自定义对象的数组

6

基本上,我想要做的是使用PowerShell脚本从Active Directory中检索所有用户并将它们保存在一个.csv文件中。此外,我只想列出“名称”和“samaccountname”这两个属性。以下是代码:

$strFilter = "somefilter"
$objCollection = @()

$objDomain = New-Object System.DirectoryServices.DirectoryEntry

$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.PageSize = 1000
$objSearcher.Filter = $strFilter
$objSearcher.SearchScope = "Subtree"

$colProplist = "name", "samaccountname"
foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}

$colResults = $objSearcher.FindAll()

foreach ($objResult in $colResults) {
  $objItem = $objResult.Properties

  $object = New-Object PSObject
  $object | Add-Member -MemberType NoteProperty -Name Name -Value $objItem.name
  $object | Add-Member -MemberType NoteProperty -Name SAMAccountname -Value $objItem.samaccountname

  $objCollection+=$object
}

$objCollection # this gives me the output as wished
$objCollection | Export-CSV -NoTypeInformation -Path C:\temp\exportfile.csv # this doesn't work

控制台输出如下:
Name                                SAMAccountname
----                                --------------
{IUSR_PFTT-DC1}                     {IUSR_PFTT-DC1}
{IUSR_PFVM-DC1}                     {IUSR_PFVM-DC1}
{IUSR_PFXX-DC1}                     {IUSR_PFXX-DC1}

但导出的 .csv 文件看起来像这样:
"Name","SAMAccountname"
"System.DirectoryServices.ResultPropertyValueCollection","System.DirectoryServices.ResultPropertyValueCollection"
"System.DirectoryServices.ResultPropertyValueCollection","System.DirectoryServices.ResultPropertyValueCollection"
"System.DirectoryServices.ResultPropertyValueCollection","System.DirectoryServices.ResultPropertyValueCollection"
"System.DirectoryServices.ResultPropertyValueCollection","System.DirectoryServices.ResultPropertyValueCollection"

有关这个问题,您有什么想法或解决方案吗?
2个回答

7
如果你想坚持使用 DirectorySearcher 方法,请将以下代码更改为:
foreach ($objResult in $colResults)
    {$objItem = $objResult.Properties

        $object = New-Object PSObject
        $object | Add-Member –MemberType NoteProperty -Name Name -Value $objItem.name
        $object | Add-Member –MemberType NoteProperty -Name SAMAccountname -Value $objItem.samaccountname

        $objCollection+=$object
    }

转化为以下内容:

$objCollection = $colResults | select -Expand Properties |
    select @{n='Name';e={$_.name}}, @{n='SAMAccountName';e={$_.samaccountname}}

@AnsgarWiechers,有没有办法将属性的“Value”从“ResultPropertyValueCollection”转换为字符串,而不必逐个列出每个属性(就像您所做的那样)? - craig

2

你可以使用Active Directory模块获取您域上的所有用户:

import-module activedirectory
get-ADuser -filter * | select name,SamAccountName | ConvertTo-CSV | ac "C:\yourCSVFile.csv"

这将会给你所有用户的输出结果,就像这样。
"name","SamAccountName"
"MATTHE_G","matthe_g"
"PUTINE_I","putine_i"
"COBB_C","cobb_c"
"BULL_T","bull_t"
"BAYOL_B","bayol_b"
"CAPPON_P","CAPPON_P"
....

注意: 您需要打开Active Directory Windows功能才能使用activedirectory模块。这可以在Windows功能中的“远程服务器管理工具”选项卡下找到。

链接:AD模块中的Cmdlet


我简直不敢相信在寻找解决方案的时候,我竟然没有偶然发现这个...非常感谢。 - Michael

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接