【问题标题】:Export an array with custom objects导出包含自定义对象的数组
【发布时间】:2013-08-10 00:49:43
【问题描述】:

基本上,我要做的是使用 PowerShell 脚本从 Active Directory 中检索所有用户并将它们保存在 .csv 文件中。另外,我只想列出属性“name”和“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"

对此有何想法/解决方案?

【问题讨论】:

    标签: powershell active-directory export-to-csv


    【解决方案1】:

    如果您想坚持使用 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 转换为字符串,而不必单独列出每个属性(就像您所做的那样)?
    【解决方案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"
    ....
    

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

    链接:For Cmdlet's in the AD module

    【讨论】:

    • 我不敢相信我在网上搜索解决方案时没有偶然发现这一点......非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-23
    • 2017-05-30
    相关资源
    最近更新 更多