【问题标题】:Some computers on a powershell GPO script not recognizing parameterpowershell GPO脚本上的某些计算机无法识别参数
【发布时间】: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


【解决方案1】:

IIRC -Property 通过 PowerShell 3.0 引入 Where-Object。您可以在 PowerShell 2.0 上运行脚本吗?

回复评论

您需要以scriptblock 的形式创建一个过滤器脚本(即一组大括号中的 PowerShell 代码),而不是使用他们为 3.0 添加的比较运算符参数。

尝试使用

Where-Object { $_.memberof -like "ALL" }

或类似的东西。 $_ 指管道中的当前对象。我找不到 2.0 版的文档,但我找到了 1.0 版的 Using the Where-Object Cmdlet,它与 2.0 AFAIK 相关,应该对您有所帮助。

【讨论】:

  • 你说得对,他们运行的是 Powershell v 2.0,不过我确实需要那个参数,有什么方法可以在 2.0 中模拟它吗?
猜你喜欢
  • 1970-01-01
  • 2015-05-26
  • 1970-01-01
  • 2022-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-23
  • 1970-01-01
相关资源
最近更新 更多