【发布时间】:2015-07-29 18:58:23
【问题描述】:
我遇到了一个问题,我的网络上大约 10% 的计算机在处理时抛出了一个非常奇怪的错误。我得到的错误是“Where-Object:找不到与参数名称'Property'匹配的参数”我使用的代码如下。
#Create ADSI Search object to query Active Directory for usernames
#Start-Transcript -Path "$env:userprofile\Desktop\log.txt"
$strFilter = "objectCategory=user"
$objDomain = New-Object System.DirectoryServices.DirectoryEntry("LDAP://OU=SD25;DC=DC;DC=DC")
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.PageSize = 100000
$objSearcher.Filter = $strFilter
$objSearcher.SearchScope = "Subtree"
#Populate ADSI with the extra fields of samaccountname which is the username, and memberof which gives you roughly which groups they are a memberof
$colProplist = "samaccountname", "memberof"
foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}
#Run the Search
$colResults = $objSearcher.FindAll()
#$colResults
$resultsarray = @()
#The way ADSI returns results, it populates all an array of every username listed within the scope, I then use this foreach recursive loop to find the name I need
foreach ($objResult in $colResults)
{
#Here I am taking each of the users, and finding the one which has the samaccountname of the user that is currently logged in
$objItem = $objResult.Properties | Where-Object -Property memberof -like ALL
#$groups = $objItem.memberof
#This is for diagnostics, if you output a logfile it will tell you the name and groups it is a member of
$objitem
}
#This is the beginnings of searching for a computer container in active directory.
$compFilter = "objectCategory=computer"
$compDomain = New-Object System.DirectoryServices.DirectoryEntry("LDAP://OU=OU;DC=DC;DC=DC")
$compSearcher = New-Object System.DirectoryServices.DirectorySearcher
$compSearcher.SearchRoot = $objDomain
$compSearcher.PageSize = 100000
$compSearcher.Filter = $strFilter
$compSearcher.SearchScope = "Subtree"
$compProplist = "name"
foreach ($i in $compPropList){$compSearcher.PropertiesToLoad.Add($i)}
$compResults = $compSearcher.FindAll()
foreach ($compR in $compResults)
{
}
#Stop-Transcript
【问题讨论】:
-
猜想:在抛出错误的主机上,您有一个自定义函数
Where-Object取代了内置 cmdlet?Get-Help Where-Object在这些主机上的输出是什么? -
这是正常的输出,它是一个过滤器。问题在于这些计算机,甚至没有人在它们上打开过 powershell 或 powershell ISE。
-
请注意,AD 的最大 PageSize 为 1000。再高一点也没有意义。
-
我不熟悉 GPO 脚本。这个脚本是否在每个盒子上运行?此行表明您正在尝试查找已登录用户的 samAccountName:“在这里,我将带走每个用户,并查找具有当前登录用户的 samaccountname 的用户”。如果你是,有更好的方法。
-
这个脚本在每个盒子上运行。是的,有更好的方法,如果我要安装 rsat 并让网络中的每个人都可以使用 AD 驱动器和 cmdlet...,但由于我的用户中至少有 4000 名是儿童,我有点不愿意这样做.至于页面大小,我的手指可能只是在编码时多次撞到零
标签: powershell active-directory group-policy