Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Get-ADComputer : The server has returned the following error: invalid enumeration context.

$
0
0

Hello again.

I am trying to get all servers out of AD, ping (Test-Connection) them and if they respond to the ping, I am looking for  ALL servers that have SQL service installed. For now, I am sending output just to the screen.  The interesting part of this script, and the reason for the post, is that when I simply search for ALL  servers in AD and pipe that into a Test-Connection Script, everything runs correctly, no errors and I get the full list of servers that have replied to ICMP. But, When I do a lookup for SQL, half way through, i get this message:

 

---------- Error Message -----------------------

Get-ADComputer : The server has returned the following error: invalid enumeration context.

At line:1 char:15

+ Get-ADComputer <<<<   -Filter * -Properties * | Select CN, DNSHostName, OperatingSystem | Where {$_.OperatingSystem -

like $OS} |

    + CategoryInfo          : NotSpecified: (:) [Get-ADComputer], ADException

    + FullyQualifiedErrorId : The server has returned the following error: invalid enumeration context.,Microsoft.Acti

   veDirectory.Management.Commands.GetADComputer

 

 

------ My Code -----------------------------------

Import-Module active*

 

$Ping = $null

$namespace = "ROOT\CIMV2"

$classname = "Win32_Service"

$OS = '*Server*'

$ServiceName = '%SQL Server (%'

 

# Searches Active Directory for Servers

Get-ADComputer  -Filter * -Properties * | Select CN, DNSHostName, OperatingSystem | Where {$_.OperatingSystem -like $OS} |

# Ping each Server to confirm  that it exists on the physical network

ForEach-Object {

  $Ping = Test-Connection -CN $_.DNSHostname -Count 1 -Quiet

  IF($Ping -match 'True') {

    Try {

        $Computer = $_.CN

        $Service = Get-WmiObject -Class $classname -ComputerName $Computer -Namespace $namespace -filter "DisplayName like '$ServiceName'" -ErrorAction Stop

        #if the query worked, $Service will have a value

        if ($Service) {

           #Load values into a new variable in case of multiple instances of the service 

           Foreach ($ServiceInstance in $Service) {

           write-host -ForegroundColor green $Computer " " $ServiceInstance.Caption " " $ServiceInstance.state

           }

        }

        else {

        #WMI worked but no SQL

        write-host -ForegroundColor yellow $Computer

        }

   }

    Catch {

        #These computers had an error

    write-host -ForegroundColor red $Computer

        }

   } 

}


Viewing all articles
Browse latest Browse all 6937

Trending Articles