Powershell – get-adcomputer error: “Invalid Enumeration Context” when running powershell script

active-directorypowershell

We're switching to WDS for deployment, so I'm writing a powershell script that will ping a computer and if it responds, get its MAC address and set the netbootGUID field in Active directory. It runs and works…for a while and then returns:

Get-ADComputer : The server has returned the following error: invalid enumeration
context.
At \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 : The server has returned the following error: invalid enumeration context.,Microsoft.ActiveDirectory.Management.Commands.GetADComputer

This is the script:

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 = ""


     
}

I have no idea why I'm getting that error or what it means. My GoogleFu is failing me today.

Best Answer

NB: I'm not a PS guru

My google fu turned up the following link.

In short, I think it has something to do with your -ResultSetSize $null portion of the script. In the link, the OP used -notlike "*"instead of the -eq "$Null"

Maybe play with that portion of the script and see what happens.