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 ...