我正在尝试开发一个PowerShell脚本来帮助AD Group Membership管理。我们有一些大型组(30k-60k +对象),我们想用另一个系统的数据更新。
该脚本从文本文件加载应该在组中的对象。然后,每个对象必须使用System.DirectoryServices.DirectorySearcher
位于AD中。之后,每个对象都会添加到组成员资格中。
该脚本花费大约80%的时间查找每个对象,是否有一种在PowerShell中使用powershell查找对象的大量方法?
谢谢!
答案 0 :(得分:3)
根据我的经验,这是查询AD的快捷方式,您需要更改查询以查找特定对象,在此代码中,您将在$objRecordSet
中找到所有用户/个人对象。
$Ads_Scope_SubTree = 2
$objConnection = new-Object -com "ADODB.Connection"
$objCommand = new-Object -com "ADODB.Command"
$objConnection.Provider = "ADsDSOObject"
$objConnection.Open( "Active Directory Provider")
$objCommand.ActiveConnection = $objConnection
$objCommand.Properties.Item("Page Size").value = 1000
$objCommand.Properties.item("Searchscope").value = $Ads_Scope_SubTree
$objCommand.CommandText = "Select Name From 'LDAP://DC = int, DC= my, DC = local' Where objectCategory = 'Person'"
$objRecordSet = $objCommand.Execute()
$objRecordSet.RecordCount
答案 1 :(得分:1)
您可以尝试System.DirectoryServices.Protocols (S.DS.P)本机(非托管)版本非常有效。
这是一个PowerShell启动脚本:
# ADDP-Connect.PS1
Clear-Host
# Add the needed assemblies
Add-Type -AssemblyName System.DirectoryServices.Protocols
# Connexion
$serverName = "WM2008R2ENT"
$ADDPConnect = New-Object System.DirectoryServices.Protocols.LdapConnection $serverName
$userName = "JPB"
$pwd = "PWD"
$domain = "Dom"
$ADDPConnect.Credential = New-Object system.Net.NetworkCredential -ArgumentList $userName,$pwd,$domain
# Create a searcher
$searchTargetOU = "dc=dom,dc=fr"
$searchFilter = "(samAccountName=user1)"
$searchScope = [System.DirectoryServices.Protocols.SearchScope]::Subtree
$searchAttrList = $null
foreach($user in "user1","user2","user3")
{
$searchFilter = "(samAccountName=$user)"
$searchRequest = New-Object System.DirectoryServices.Protocols.SearchRequest -ArgumentList $searchTargetOU,$searchFilter,$searchScope,$searchAttrList
$searchResponse = $ADDPConnect.SendRequest($searchRequest)
foreach($searchEntries in $searchResponse.Entries)
{
$searchEntries.DistinguishedName
}
}
答案 2 :(得分:1)
如果您开始看到超时问题,请相应地设置超时参数,如下所示
$ADDPConnect = New-Object System.DirectoryServices.Protocols.LdapConnection $serverName
$ADDPConnect.Timeout = "1000"
答案 3 :(得分:0)
如果您在执行期间看到超时问题,则以下内容可以提供帮助
$ADDPConnect = New-Object System.DirectoryServices.Protocols.LdapConnection $serverName
$ADDPConnect.Timeout = "1000"