Hey folks, I'm working on a project where I need to generate a report of all the servers in our Active Directory domain using PowerShell. My goal is to format this data for SharePoint so that management can review it easily. I've started with a command to query server information, but I keep running into an error after six minutes saying 'invalid enumeration context.' It seems like the command is timing out after processing the first set of 256 objects. I've done some research and found that I should use variables to address this, but even when I try that, I still get the same outcome: a CSV file with only 256 entries. Below is the original command I'm using:
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"
I'd really appreciate any suggestions on how to debug this or alternative methods to get a complete list of server objects. Thanks!
2 Answers
This might sound a bit basic, but how about using an ‘all’ filter and then filtering for servers afterward? Like so:
$all = Get-AdComputer -Filter *
$servers = $all | where-object {$_.OperatingSystem -like '*server*'}
I’ve got tons of AD objects and this method works pretty quickly for me!
Have you checked if reducing the page size might help? You could try this command:
Get-ADComputer -Filter .. -Property .. -ResultPageSize 50
Adjusting the page size often helps mitigate timeout issues!
Related Questions
How To: Running Codex CLI on Windows with Azure OpenAI
Set Wordpress Featured Image Using Javascript
How To Fix PHP Random Being The Same
Why no WebP Support with Wordpress
Replace Wordpress Cron With Linux Cron
Customize Yoast Canonical URL Programmatically