1

I'm trying to run a script to get a list of PCs and exporting it to a csv file. When I run it, it returns a "The server has returned the following error: invalid enumeration context." error.

Below is my script:

Get-ADComputer -Filter * -Property * | Select-Object Created,Name,OperatingSystem,ipv4Address,DistinguishedName | Export-CSV test.csv -NoTypeInformation -Encoding UTF8

I made some changes:

$properties = 'Created','Name','OperatingSystem','OperatingSystemVersion','ipv4Address','DistinguishedName'
Get-ADComputer -Filter * -Properties $properties | Export-CSV test.csv -NoTypeInformation -Encoding UTF8

But I'm still getting the enumeration error.


Another update:

The code was giving an error so I thought maybe I needed to set the execution policy to Unrestricted, but that also didn't fix the issue. I have a new error now along with the enumeration error.

Set-ExecutionPolicy -ExecutionPolicy Unrestricted

$properties = @(
    'Created'
    'Name'
    'OperatingSystem'
    'ipv4Address'
    'DistinguishedName'
)

Get-ADOrganizationalUnit -Filter * | ForEach-Object {
    Get-ADComputer -Filter * -Properties $properties -SearchBase $_.DistinguishedName
} | Select-Object $properties | Export-CSV test.csv NoTypeInformation -Encoding UTF8

Here are the errors (odd because AD is not down):

Get-ADComputer : Unable to contact the server. This may be because this server does not exist, it is currently down, or it does not have the Active Directory Web Services running.
At \\nasinfrastructure.wakefern.com\Infrastructure\Security\Powershell\Susan_Scripts\AD_User_PCs\Get-ADComputer_4All.ps1:12 char:5
+     Get-ADComputer -Filter * -Properties $properties -SearchBase $_.D ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (:) [Get-ADComputer], ADServerDownException
    + FullyQualifiedErrorId : ActiveDirectoryServer:0,Microsoft.ActiveDirectory.Management.Commands.GetADComputer
 
Get-ADComputer : The server has returned the following error: invalid enumeration context.
At \\nasinfrastructure.wakefern.com\Infrastructure\Security\Powershell\Susan_Scripts\AD_User_PCs\Get-ADComputer_4All.ps1:12 char:5
+     Get-ADComputer -Filter * -Properties $properties -SearchBase $_.D ...
noobCoder
  • 89
  • 7
  • See this: https://stackoverflow.com/questions/70264166/get-aduser-the-server-has-returned-the-following-error-invalid-enumeration-con/70264449#70264449 you need to refine your query and query only the properties of interest (do not use `-Properties *` if you dont need ALL objects attributes) – Santiago Squarzon Dec 07 '22 at 19:31
  • Hi @SantiagoSquarzon I edited my post above and added some changes I made to my script per what I saw in the link you sent me. I'm still getting the enumeration error despite not using `-Properties *`. Am I on the right track? – noobCoder Dec 07 '22 at 19:53

1 Answers1

1

This can happen in a big environment with many objects, I see you have updated your code to query only a specific set of attributes as opposed to querying all (-Properties *) which is a good start. Your best bet to workaround this issue is to query all computers per Organizational Unit.

$properties = @(
    'Created'
    'Name'
    'OperatingSystem'
    'ipv4Address'
    'DistinguishedName'
)

Get-ADOrgnizationalUnit -Filter * | ForEach-Object {
    $getADComputerSplat = @{
        Filter      = '*'
        Properties  = $properties
        SearchBase  = $_.DistinguishedName
        SearchScope = 'OneLevel'
    }

    Get-ADComputer @getADComputerSplat
} | Select-Object $properties |
    Export-Csv test.csv -NoTypeInformation -Encoding UTF8
Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37
  • I'm still getting the same error, but now I have a new error with `Get-ADOrganizationalUnit` I edited my post above and show part of the error message. – noobCoder Dec 09 '22 at 16:23
  • 1
    To prevent getting duplicates by iterating over the same OUs more than once you need to add `-SearchScope onelevel` to either `Get-ADOrgnizationalUnit -Filter *` or to the `Get-ADComputer ` command – Itchydon Aug 25 '23 at 09:23
  • 1
    @Itchydon excellent point. i mistake on my side. thanks – Santiago Squarzon Aug 25 '23 at 13:05