Hi everyone! I'm working on a project where I need some help with a PowerShell script. The goal is to generate a report of all servers in our Active Directory domain and format that information for SharePoint, so management can easily review it.
I've started with a command that previously worked for laptops, but I've adjusted it for servers. Here's the command I'm using:
```powershell
Get-ADComputer -Filter "OperatingSystem -Like '*server*' -and Enabled -eq '$true'" -Property DNSHostName,IPv4Address,OperatingSystem,OperatingSystemServicePack,OperatingSystemVersion | Select-Object DNSHostName,IPv4Address,OperatingSystem,OperatingSystemServicePack,OperatingSystemVersion | Export-Csv "\fooServerReport - $((Get-Date).ToString("yyyy-MM-dd - HH_mm_ss")).csv"
```
However, I keep getting an error after six minutes stating: `Get-ADComputer: The server has returned the following error: invalid enumeration context.` It seems to be timing out while processing the data.
I found some tips online suggesting to pass the command through variables, but that didn't help either, and I still just get a CSV with 256 objects before it fails. Is there something I'm missing or a different approach I should try to get all the server information? Any suggestions would be appreciated!
2 Answers
Have you thought about reducing the page size for your results? Something like this might help:
```powershell
Get-ADComputer -Filter .. -Property .. -ResultPageSize 50
```
This often can prevent timeouts by processing fewer objects at once, so definitely give it a shot!
This might sound a bit silly, but you could try using a general filter and then looping through the results to isolate servers. For example:
```powershell
$all = Get-AdComputer -Filter *
$servers = $all | where-object {$_.OperatingSystem -like '*server*'}
```
In my experience, even with over 300k AD objects, this method completes pretty quickly—around 15 seconds for me, so it might work for you too!
That wouldn’t work, though, because you wouldn't be pulling the specific properties that you need.
Hey, I tried adjusting the page size as you suggested, but it ended up reporting back fewer items—about 150 instead of the 256 I was getting before.