How can I create a PowerShell script to report all servers in Active Directory?

    0
    1
    Asked By Elli Mongillo On

    Hey everyone,

    I'm working on a project where I need to create some PowerShell scripts to list all the servers in our Active Directory domain and format the data for SharePoint so upper management can easily review it. I've run into issues right from the beginning, especially with collecting the information.

    I started with a command I previously used for laptops, but I adjusted it to look for servers:

    ```
    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"
    ```

    Unfortunately, I keep getting this error after six minutes: "Get-ADComputer: The server has returned the following error: invalid enumeration context." From my research, it seems like the command times out after processing the first 256 objects. I'm also trying to use variables to fix this:

    ```
    $servers = 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"
    ```

    Yet, the same issue arises, and the CSV I get still only contains 256 objects. I've even written it as a complete script:

    ```
    # Import the Active Directory module
    Import-Module ActiveDirectory

    # Get all matching computers
    $computers = Get-ADComputer -Filter "OperatingSystem -Like '*server*' -and Enabled -eq '$true'" -Property DNSHostName, IPv4Address, OperatingSystem, OperatingSystemServicePack, OperatingSystemVersion

    # Select properties to export
    $report = $computers | Select-Object DNSHostName, IPv4Address, OperatingSystem, OperatingSystemServicePack, OperatingSystemVersion

    # Export report to CSV
    $outputPath = "\fooServerReport - $((Get-Date).ToString("yyyy-MM-dd - HH_mm_ss")).csv"
    $report | Export-Csv -Path $outputPath -NoTypeInformation
    ```

    But I keep ending up with just 256 entries and the same timeout error. What am I missing here? Can someone help me troubleshoot this?

    0 Answers

    There is no answer to this question yet. If you know the answer or can offer some help, please use the form below.

    Related Questions

    LEAVE A REPLY

    Please enter your comment!
    Please enter your name here

    This site uses Akismet to reduce spam. Learn how your comment data is processed.