我正在使用此脚本将一大堆信息提取到另一个程序使用的制表符分隔的平面文本文件中。该脚本运行良好,创建文件并用数据填充它,但当它达到大约250MB的数据时,我得到下面列出的错误。我认为这是因为我只是想收集太多数据,但我不确定如何修复它!
脚本:
Import-module ActiveDirectory
$Domain = 'DomainName'
Get-ADGroup -Filter * -Properties * | ForEach-Object {
$Group = $_
Get-ADGroup $Group -Properties Members | Select-Object -ExpandProperty Members | Get-ADObject | ?{$_.ObjectClass -eq "user"} |
Get-ADUser -Properties * | Select-Object @{
Name = 'Domain'
Expression = { $Domain }
}, @{
Name = 'Group Name'
Expression = { $Group.Name }
}, @{
Name = 'Type'
Expression = { $Group.GroupCategory }
}, @{
Name = 'Description'
Expression = { $_.Description }
}, @{
Name = 'Distinguished Name'
Expression = { $_.DistinguishedName }
}, @{
Name = 'Managed By'
Expression = { $Group.ManagedBy }
}, @{
Name = 'Members'
Expression = { $_.MemberOf }
}, @{
Name = 'Full Name'
Expression = { $_.Name }
}, @{
Name = 'User Name'
Expression = { $_.SamAccountName }
}, @{
Name = 'Display Name'
Expression = { $_.DisplayName }
}
} | Export-Csv -delimiter "`t" -path C:\Test.txt –nti
编辑:我已经尝试使用directorysearcher工具,因为我知道这可以解决索引问题,但是我无法正确获取代码。我需要获取用户和组信息。我收到了用户信息,但没有为所有用户提取数据,如何搜索域中的所有用户?
Import-module ACtiveDirectory
$Domain = 'domain'
$strFilter = "(&(objectCategory=person)(objectClass=user))"
$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", "description", "distinguishedname", "memberof", "displayname"
foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}
$colResults = $objSearcher.FindAll()
$objCollection = $colResults | select -Expand Properties |
select @{
n = 'Domain'
e = { $Domain }
}, @{
n = 'Group Name'
e = { $Group.Name }
}, @{
n = 'Type'
e = { $Group.GroupCategory }
}, @{
n = 'Description'
e = { $_.description }
}, @{
n = 'Distinguished Name'
e = { $_.distinguishedname }
}, @{
n = 'Managed By'
e = { $Group.ManagedBy }
}, @{
n = 'Members'
e = { $_.memberof }
}, @{
n = 'Full Name'
e = { $_.name }
}, @{
n = 'User Name'
e = { $_.samaccountname }
}, @{
n = 'Display Name'
e = { $_.displayname }
}
$objCollection | Export-Csv -delimiter "`t" -path C:\Test9.txt -nti
答案 0 :(得分:0)
我想提供此替代脚本。它使用AD SnapIn,并根据需要构建用户列表。
Import-module ActiveDirectory
$Domain = 'DomainName'
$Groups = Get-ADGroup -Filter * -Properties Name,GroupCategory,ManagedBy,Members | ForEach-Object {
$Users = @()
$Results = @()
ForEach($Group in $Groups){
$Group.Members | ForEach{
$UserDN = $_
If($Users.DistinguishedName -contains $UserDN){
$User = $Users | Where{$_.DistinguishedName -eq $UserDN}
}Else{
$User = Get-ADUser $UserDN -Properties DisplayName,Description,DistinguishedName,MemberOf,Name,SamAccountName
$Users += $User
}
$Results += [PSCustomObject]@{
'Domain' = $Domain
'Group Name'= $Group.Name
'Type' = $Group.GroupCategory
'Description' = $User.Description
'Distinguished Name' = $User.DistinguishedName
'Managed By' = $Group.ManagedBy
'Members' = $User.MemberOf
'Full Name' = $User.Name
'User Name' = $User.SamAccountName
'Display Name' = $User.DisplayName
}
}
}
$Results | Export-Csv -delimiter "`t" -path C:\Test.txt –nti
现在,如果这会给您索引错误或内存问题,您可以按组收集用户,并将每个组输出到随后附加的CSV。为此,您需要在ForEach($Group in $Groups){
之后立即添加一个新行:
$GroupMembers = @()
将$Results += [PSCustomObject]@{
更改为:
$GroupMembers += [PSCustomObject]@{
最后将最后两行更改为:
$GroupMembers | Export-Csv -delimiter "`t" -path C:\Test.txt –nti -append
}