Get-ADComputer The server has returned the following error Invalid enumeration context

I am trying to run this script to get all the Windows computers (Desktops & Servers) powershell version to fix it. This is the script

Get-ADComputer -Filter ‘operatingsystem -notlike “server” -and enabled -eq “true”’ `
-Properties Name,Operatingsystem,OperatingSystemVersion,IPv4Address |
Sort-Object -Property Operatingsystem |
Select-Object -Property Name,Operatingsystem,OperatingSystemVersion,IPv4Address


when you get errors and you don’t know what to do with it you should post the complete error message. AND … when you post code, error messages, console output or sample data, please format it as code using the preformatted text button ( </> ).

Thanks in advance.

I’d recommend to provide a SearchBase top put less stress on your DC when querying a big amount of objects.

Try it this way:

$Searchbase = 'OU=Computers,OU=Berlin,OU=Germany,OU=Europe,DC=contoso,DC=com'
Get-ADComputer -SearchBase $Searchbase  -Filter "OperatingSystem -notlike '*server*' -and Enabled -eq '$true'" -Properties Name, Operatingsystem, OperatingSystemVersion, IPv4Address 
    Select-Object -Property Name, Operatingsystem, OperatingSystemVersion, IPv4Address |
        Sort-Object -Property Operatingsystem

Thanks Olaf. I would do that when I post it next time.

Why not now? It will help others looking for the same or a similar problem. :wink: … you can edit your existing post. You don’t have to create a new one.

And what’s with your issue? Is it solved?

Your good Olaf. Well no it is still an issue.

Preformatted text</$file_data = Get-Content c:\adcomputerlist.txt
Invoke-Command -ComputerName $file_data -Scriptblock { $PSVersionTable.PSVersion } -ErrorAction SilentlyContinue |
Select-Object -Property PSComputerName,Major,Minor,Build,Revision |
Export-CSV -Path c:\PSVersionsresult.csv -NoTypeInformation -Delimiter ‘,’/>Preformatted text

i get this following error

Preformatted text</Invoke-Command : Cannot validate argument on parameter ‘ComputerName’. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At C:\Users\User123\Documents\Powershell Scripts\GetPowershellversions7122021.ps1:12 char:30

  • Invoke-Command -ComputerName $adcomputerList -Scriptblock { $PSVersi …
  • CategoryInfo : InvalidData: (:slight_smile: [Invoke-Command], ParameterBindingValidationException
  • FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.InvokeCommandCommand/>Preformatted text

Please format your code and error message as code. You actually promised it. :wink:

Just hit the preformatted text button ( </> ) and paste your code where you’ve been told.

Thanks in advance.

And BTW:

the error is pretty obvious:

You should check the content of the variables you use. Do you have empty lines in your input file?

1 Like