get-adcomputer错误:运行powershell脚本时出现“Enumeration Context无效”

我们正在切换到WDS进行部署,所以我正在编写一个PowerShell脚本来ping一台计算机,如果它响应,获取它的MAC地址,并在Active Directory中设置netbootGUID字段。 它运行和工作…一段时间,然后返回:

Get-ADComputer:服务器返回了以下错误:枚举上下文无效。 \ Path \ To \ Scripts \ setNetbootGUIDremoteComputers.ps1:3 char:15

  • get-adcomputer <<<< -Filter * -searchbase“OU = Somehwere,DC = My,DC = AD,DC = TLD”-ResultSetSize $ null | foreach-object {
    • CategoryInfo:NotSpecified:(:) [Get-ADComputer],ADException
    • FullyQualifiedErrorId:服务器返回了以下错误:枚举上下文无效。,Microsoft.ActiveDirectory.Management.Commands.GetADComputer

这是脚本:

import-module ActiveDirectory get-adcomputer -Filter * -searchbase "OU=Somewhere,DC=MY,DC=AD,DC=TLD" -ResultSetSize $null | foreach-object { $strComputer = $_.Name $ping = new-object System.Net.NetworkInformation.Ping $Reply = $ping.send($strComputer) if ($Reply.status –eq “Success”){ $colItems = GWMI -cl "Win32_NetworkAdapterConfiguration" -name "root\CimV2" -comp $strComputer -filter "IpEnabled = TRUE" ForEach ($objItem in $colItems) { $MAC = $objItem.MacAddress.Replace(":", "") Write-Host "Machine Name: " $strComputer Write-Host "MAC Address:" $MAC [guid]$nbGUID = "00000000-0000-0000-0000-$MAC" $comp = get-adcomputer $strComputer -Properties netbootGUID $comp.netbootGUID = $nbGUID.ToByteArray() set-adcomputer -Instance $comp write-output "$strComputer" | out-file -filePath c:\somewhere\guidSet.txt -append } } else { write-output "$strComputer" | out-file -filePath c:\somewhere\offline.txt -append } $Reply = "" } 

我不知道为什么我得到这个错误或者是什么意思。 我的GoogleFu今天失败了。

注意:我不是PS大师

我的谷歌fu打开了以下链接 。

总之,我认为这与脚本的-ResultSetSize $null部分有关。 在链接中,OP使用-notlike "*"代替-eq "$Null"

也许玩脚本的这一部分,看看会发生什么。

我喜欢get-adcomputer和quest-active目录命令,以便在服务器上需要很多信息的情况下,但是要坚持使用active-directory命令dsquerydsget ,因为我find了get-adcomputer ,特别是quest命令不必要的慢,尽pipe有些东西可能要求你不要使用这些ds命令。 如果你确实可以访问这些命令,这可能是值得一试的,即使它只是给你一个不同的错误信息,因为它通过使用get-adcomputer和现有的确定ping能力的方法(types米奇 – 小鼠,但有时这种方式提供了额外的信息) –

 dsquery computer ou=Somewhere,dc=My,dc=AD,dc=TLD | ?{$_ -imatch "cn=([^,]+,),"} | % { $your_computer = $Matches[1] $cannot_ping_computer = $false # similarly for the ping command, but should be it's own little function ping $your_computer | ?{$_ -imatch "\s*Packets: Sent = (\d+), Received = (\d+)" }|% { # or whatever conditions you find satisfactory if ($Matches[1] -ne $Matches[2]) $cannot_ping_computer = $true } if ( $cannot_ping_computer ) { #command to jump to next element in pipe, I cannot recall >.< } # rest of your code... } 

过去几个月没有工作了,没有Windows机器,所以代码不在我头顶,但我希望它适合你。 似乎是对的。

我希望你解决了这个问题,但是如果没有的话,我希望这能够以某种方式提供帮助。

祝你好运! 🙂

我会尝试使用基于这个职位的ResultPageSize参数。 也许一次把它设置成几百个结果。

我从来没有解决这个。 我最终把OU放在较大的容器下面,并一次对着一千个账户运行。

不幸的是,这将是一个谜,因为这个环境不复存在。