小编Mic*_*ael的帖子

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

基本上,我正在尝试做的是从Active Directory检索所有用户并使用PowerShell脚本将它们保存在.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 …
Run Code Online (Sandbox Code Playgroud)

powershell active-directory export-to-csv

5
推荐指数
2
解决办法
3448
查看次数