Avik Chowdhury
Avik Chowdhury

Reputation: 51

Powershell - Conditional Operators

I have written this script to extract the PC's and servers from the AD into two differnet spreadsheets. But whenever I run the script, I'am getting the following error. Can anyone help me by pointing out on where I am going wrong.

This is the error I am getting :

Get-ADComputer : The server has returned the following error: invalid enumeration context.
At line:3 char:1
+ Get-ADComputer -filter { OperatingSystem -ne '*server*' -OR Operating ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-ADComputer], ADException
    + FullyQualifiedErrorId : The server has returned the following error: invalid enumeration context.,Microsoft.ActiveDirectory.Management.Commands.GetADComputer

Get-ADComputer : The server has returned the following error: invalid enumeration context.
At line:6 char:1
+ Get-ADComputer -filter { OperatingSystem -eq '*server*' -OR Operating ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-ADComputer], ADException
    + FullyQualifiedErrorId : The server has returned the following error: invalid enumeration context.,Microsoft.ActiveDirectory.Management.Commands.GetADComputer

Given below is the script :-

import-module ac*

Get-ADComputer -filter { OperatingSystem -ne '*server*' -OR OperatingSystemVersion -Like '*5.1*' -OR OperatingSystemVersion -Like '*6.1*' -and Enabled -eq "true"} -SearchBase 'OU=IMComputers,OU=IM,dc=image,dc=inter' -Properties '*' | Select Name,OperatingSystem,Status, OperatingSystemVersion, `
LastLogonDate,CanonicalName | Export-Csv -NoType "C:\Temp\PC's in AD" -Encoding UTF8

Get-ADComputer -filter { OperatingSystem -eq '*server*' -OR OperatingSystemVersion -Like '*5.0*' -OR OperatingSystemVersion -Like '*5.2*' -OR OperatingSystemVersion -Like '*6.0*' -OR OperatingSystemVersion -Like '*6.1*' -OR OperatingSystemVersion -Like '*6.2*' -OR OperatingSystemVersion -Like '*6.3*' -and Enabled -eq "true"} -SearchBase 'OU=IMComputers,OU=IM,dc=image,dc=inter' -Properties '*' | Select Name,OperatingSystem,Status, OperatingSystemVersion, `
LastLogonDate,CanonicalName | Export-Csv -NoType "C:\Temp\Servers in AD" -Encoding UTF8

Upvotes: 1

Views: 144

Answers (1)

JPBlanc
JPBlanc

Reputation: 72630

Your script works when I adapt it to my organisation (it's a small one) so your are not going so wrong. But I read something about that a few years ago and thanks to G.... my friend i found Invalid Enumeration Context using powershell script to check computer accounts.

Summary : the problem is worsened if you query based on an attribute that is not indexed. The operatingSystem and also the operatingSystemVersion attributes are not indexed. Assuming you have a large number of computer objects in your domain, and you frequently query based on OS, it perhaps makes sense to make operatingSystem indexed. Another solution is to use DirectorySearcher filter with and LDAP filter instead of Get-ADComputer.

Upvotes: 1

Related Questions